Java Reference
In-Depth Information
EXAMPLE:
(continued)
In this case, the argument
JFrame.DO_NOTHING_ON_CLOSE
is a defined constant
named
DO_NOTHING_ON_CLOSE
, which is defined in the
JFrame
class. This sets
the close-window button so that when it is clicked, nothing happens (unless we
programmed something to happen, which we have not done). Other possible
arguments are given in Display 17.3 .
The method
setDefaultCloseOperation
takes a single
int
argument; each of
the constants described in Display 17.3 is an
int
constant. However, do not think of
them as
int
values. Think of them as policies for what happens when the user clicks
the close-window button. It was convenient to name these policies by
int
values.
However, they could just as well have been named by
char
values or
String
values
or something else. The fact that they are
int
values is an incidental detail of no real
importance.
Descriptions of some of the most important methods in the class
JFrame
are
given in Display 17.3. Some of these methods will not be explained until later in this
chapter. A more complete list of methods for the class
JFrame
is given in Appendix 5.
A
JFrame
can have components added, such as buttons, menus, and text labels.
For example, the following line from Display 17.2 adds the
JButton
object named
endButton
to the
JFrame
named
firstWindow:
firstWindow.add(endButton);
The description of how the
JButton
named
endButton
is created and
programmed will be given in the two subsections entitled “Buttons” and “Action
Listeners and Action Events” a little later in this section.
We end this subsection by jumping ahead to the last line of the program, which is
firstWindow.setVisible(
true
);
This makes the
JFrame
window visible on the screen. At first glance, this may
seem strange. Why not have windows automatically become visible? Why would
you create a window if you did not want it to be visible? The answer is that you may
not want it to be visible at all times. You have certainly experienced windows that
disappear and reappear. To hide the window, which is not desirable in this example,
you would replace the argument
true
with
false
.
JFrame
An object of the class
JFrame
is what you think of as a window. It automatically has a
border and some basic buttons for minimizing the window and similar actions. As you
will see, a
JFrame
object can have buttons and many other components added to the
window and programmed for action.