Java Reference
In-Depth Information
Argument
Description
HIDE
_
ON
_
CLOSE
This just hides the window by calling its
setVisible()
method with an argument of
false
. This is the default action if
you don't call the
setDefaultCloseOperation()
method
with a different argument value. When a window is hidden, you
could always display the window again later by calling
setVisible()
with an argument of
true
.
Of course, you may want to take some action beyond the options we have discussed here when the user
chooses to close the window. If the program involves entering a lot of data for instance, you may want to
ensure that the user is prompted to save the data before the program ends. This involves handling an event
associated with the close menu item or the close button, and we will be investigating this in the next chapter.
The method
setVisible()
with the argument set to
true
, displays our application window on top of
any other windows currently visible on the screen. If you wanted to hide the window somewhere else in
the program, you would call
setVisible()
with the argument set to
false
.
It's a very
nice
window, but not overly useful. All you can do with it is move, resize, and reshape it. You
can drag the borders and maximize and minimize it. The
close
icon works because our program elected
to dispose of the window and exit the program when the
close
operation is selected by setting the
appropriate option through the
setDefaultCloseOperation()
method. If we omitted this method
call, the window would close but the program would not terminate.
The
setBounds()
and
setVisible()
methods are members of the
JFrame
class inherited from the
Component
class, so these are available for any component. However, you don't normally set the size
and position of other components, as we will see. The
setDefaultCloseOperation()
method
is
defined in the
JFrame
class so this method only applies to
JFrame
window objects.
Before we expand our
JFrame
example we need to look a little deeper into the make-up 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 we 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
Component
as a base is shown overleaf. The arrows in the diagram point towards the superclass.