Java Reference
In-Depth Information
The example creates a very
nice
window, but it's not overly useful. All you can do with it is move it,
resize it, and changes its width and or height. You can drag the borders and maximize and minimize it. The
Close icon works because you elected to dispose of the window and exit the program when the
close
op-
eration is selected by setting the appropriate option through the
setDefaultCloseOperation()
method. If
you omitted this method call, you would get the default action,
HIDE_ON_CLOSE
, whereby the window would
be hidden, but the program would not terminate.
The
setBounds()
and
setVisible()
methods are members of the
JFrame
class inherited from the
Com-
ponent
class, so these are available for any component. However, you don't normally set the size and po-
sition of other components, as you see later. The
setDefaultCloseOperation()
method is defined in the
JFrame
class so this method only applies to
JFrame
window objects.
Before you expand the example, you need to look a little deeper into the makeup of the component
classes.
COMPONENTS AND CONTAINERS
A component represents a graphical entity of one kind or another that can be displayed on the screen. A
component is any object of a class that is a subclass of
Component
. As you have seen, a
JFrame
window
is a component, but there are many others. Before getting into specifics, let's first get a feel for the general
relationship between the groups of classes that represent components. Part of the class hierarchy with
Com-
ponent
as a base is shown in
Figure 17-6
. The arrows in the diagram point toward the superclass.
This shows some of the subclasses of
Component
— the ones that are important to you at the moment.
I discussed the chain through to
JFrame
earlier, but the other branches are new. The classes that you use
directly are all the most commonly derived classes.
Table 17-2
summarizes how you typically use the key classes in this hierarchy:
TABLE 17-2
:
Using Component subclasses