Java Reference
In-Depth Information
public static void main(final String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Adornment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Adding Components to a JFrame
Because
JFrame
implements the
RootPaneContainer
interface and uses a
JRootPane
, you don't
add components directly to the
JFrame
. Instead, you add them to the
JRootPane
contained
within the
JFrame
. Prior to J2SE 5.0, you needed to add components like this:
JRootPane rootPane = aJFrame.getRootPane();
Container contentPane = rootPane.getContentPane();
contentPane.add(...);
This can be shortened to the following form:
aJFrame.getContentPane().add(...);
If you tried to add components directly to the
JFrame
, it resulted in a runtime error being
thrown.
Due to many suggestions (complaints?), Sun finally decided to change the
add()
method
into a proxy:
// J2SE 5.0
aJFrame.add(...);
With J2SE 5.0, when you add components to the
JFrame
, they actually are added to the
content pane of the
RootPaneContainer
.
Handling JFrame Events
The
JFrame
class supports the registration of eleven different listeners:
•
ComponentListener
: To find out when the frame moves or is resized.
•
ContainerListener
: Normally not added to a
JFrame
because you add components to the
content pane of its
JRootPane
.
•
FocusListener
: To find out when the frame gets or loses input focus.