Java Reference
In-Depth Information
Figure 12.2
JFrame created by the JFrameDemo program.
Clicking the X in the title bar of a JFrame causes the window to be hidden
by default, but this does not cause your program to stop executing. You
need to press Ctrl+c at the command prompt to stop the JVM, even though
your JFrame is no longer visible on the screen.
As with Frame objects, you can handle the WindowEvent of the JFrame
and use the System.exit() method to stop execution of the program.
However, unlike the Frame class, the JFrame class contains a setDefault-
CloseOperation() method that allows you to decide what action to take
when the user closes the JFrame. The method takes in an int, which can
be one of the following values:
WindowConstants.HIDE_ON_CLOSE.
Hides the JFrame. This is the
default behavior.
WindowConstants.DO_NOTHING_ON_CLOSE.
Does nothing, which
is similar to the behavior of Frame objects.
WindowConstants.DISPOSE_ON_CLOSE. Hides and disposes the
JFrame, but does not terminate the program.
WindowConstants.EXIT_ON_CLOSE.
Causes the program to stop
executing.
Notice that the JFrameDemo program contains the statement:
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
The JFrameDemo program terminates when the window is closed, as
opposed to the FrameDemo program, which did nothing when the
window was closed.
Search WWH ::




Custom Search