Java Reference
In-Depth Information
Although most properties are the result of implementing the
RootPaneContainer
interface,
two properties are special:
defaultCloseOperation
and
layout
. (You first looked at the
defaultCloseOperation
property in Chapter 2.) By default, a
JFrame
hides itself when the user
closes the window. To change the setting, you can use one of the constants listed in Table 8-6
as arguments when setting the default close operation. The first comes from
JFrame
directly;
the others are part of the
WindowConstants
interface.
aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Table 8-6.
Close Operation Constants
Constant
Description
EXIT_ON_CLOSE
Call
System.exit(0).
DISPOSE_ON_CLOSE
Call
dispose()
on the frame.
DO_NOTHING_ON_CLOSE
Ignore the request.
HIDE_ON_CLOSE
Call
setVisible(false)
on the frame; this is the default.
The
layout
property is odd. By default, setting the layout manager of the
JFrame
passes the
call along to the content pane. You can't change the default layout manager of the
JFrame
.
■
Tip
You can use the
state
property (inherited from
Frame
) to say whether the
JFrame
is currently icon-
ified. When using the property, be sure to use one of the additional
Frame
constants of
NORMAL
or
ICONIFIED
to
set its state.
There is an additional static property of
JFrame
:
defaultLookAndFeelDecorated
. This works
with the
windowDecorationStyle
property of
JRootPane
. When set to
true
, newly created frames
will be adorned with decorations from the look and feel instead of the window manager. Of
course, this happens only if the current look and feel supports window decorations. Listing 8-2
shows an alternate way to generate the same screen (with the window adornments provided by
the Metal look and feel) as the one shown earlier in Figure 8-2.
Listing 8-2.
Alternative Way of Setting the Window Decoration Style
import java.awt.*;
import javax.swing.*;
public class AdornSample2 {