|
|
|
|||
|
Swing Components and the Containment HierarchyHere, again, is a picture of theSwingApplication program presented in
A Quick Tour of a Swing Application's Code![]() SwingApplication creates four commonly used Swing components:
The frame is a top-level container.
It exists mainly to provide a place for
other Swing components to paint themselves.
The other commonly used top-level containers
are dialogs (
The panel is an intermediate container.
Its only purpose is to simplify
the positioning of the button and label.
Other intermediate Swing containers,
such as scroll panes (
The button and label are
atomic components --
components that exist not to hold random Swing components,
but as self-sufficient entities
that present bits of information
to the user.
Often,
atomic components also get input from the user.
The Swing API provides many atomic components,
including
combo boxes (
Here is a diagram of the containment hierarchy
for the window shown by
JFrame (a top-level container)
|
...
|
content pane
|
JPanel
|
+----------------+
| |
JButton JLabel
As the figure shows,
even the simplest Swing program
has multiple levels
in its containment hierarchy.
The root of the containment hierarchy
is always a top-level container.
The top-level container
provides a place for its descendent Swing components
to paint themselves.
Every top-level container indirectly contains
an intermediate container known as a content pane.
For most programs,
you don't need to know what's between a top-level container
and its content pane.
(If you really want to know, see
How to Use Root Panes As a rule, the content pane contains, directly or indirectly, all of the visible components in the window's GUI. The big exception to the rule is that if the top-level container has a menu bar, then by convention the menu bar goes in a special place outside of the content pane.
To add a component to a container,
you use one of the various forms of the frame = new JFrame(...); button = new JButton(...); label = new JLabel(...); pane = new JPanel(); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER);
To see all the Swing components,
go to
A Visual Index to the Swing Components | |||
|