JFrame
,
JDialog
, and
JApplet
.
When using these classes,
you should keep these facts in mind:
|
|
You can find the entire source for this example in
TopLevelDemo.java
.
Although the example uses a JFrame
in
a standalone application, the same concepts apply
to JApplet
s and JDialog
s.
Here's the containment hierarchy for this example's GUI:
As the ellipses imply, we left some details out of this diagram. We reveal the missing details a bit later. Here are the topics this section discusses:
As a rule, a standalone application with a Swing-based GUI
has at least one containment
hierarchy with a JFrame
as its root.
For example, if an application has one main window and two dialogs,
then the application has three containment hierarchies,
and thus three top-level containers.
One containment hierarchy has a JFrame
as its root,
and each of the other two has a JDialog
object
as its root.
A Swing-based applet has at least one containment hierarchy,
exactly one of which is rooted by a JApplet
object.
For example, an applet that brings up a dialog
has two containment hierarchies.
The components in the browser window
are in a containment hierarchy
rooted by
a JApplet
object.
The dialog has a containment hierarchy
rooted by a JDialog
object.
Adding Components to the Content Pane
Here's the code that the preceding example
uses to get a frame's content pane and add the yellow
label to it:
frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);
getContentPane
method.
The default content pane is
a simple intermediate container
that inherits from JComponent
,
and that uses a BorderLayout
as its layout manager.
It's easy to customize the content pane —
setting the layout manager
or adding a border, for example.
However, there is one tiny gotcha.
The getContentPane
method
returns a Container
object,
not a JComponent
object.
This means that if you want
to take advantage of the content pane's JComponent
features,
you need to either typecast the return value
or create your own component to be the content pane.
Our examples generally take the second approach,
since it's a little cleaner.
Another approach
we sometimes take is to simply add a customized component
to the content pane,
covering the content pane completely.
Note that the default layout manager for JPanel
is FlowLayout
;
you'll probably want to change it.
To make a component the content pane,
use the top-level container's
setContentPane
method.
For example:
//Create a panel and add components to it. JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(someBorder); contentPane.add(someComponent, BorderLayout.CENTER); contentPane.add(anotherComponent, BorderLayout.PAGE_END); topLevelContainer.setContentPane(contentPane);
add
method and its variants,
remove
and setLayout
have been overridden
to forward to the contentPane
as necessary.
This means you can write
frame.add(child);
contentPane.
getLayout()
will not return the layout set with
setLayout()
.
JMenuBar
object,
populate it with menus,
and then call setJMenuBar
.
The TopLevelDemo
adds a menu bar
to its frame with this code:
frame.setJMenuBar(greenMenuBar);
Here's a list of the components that a root pane provides to a frame (and to every other top-level container):
For more details, see How to Use Root Panes.