Java Reference
In-Depth Information
Exercise 11.17
Using the
layouts
project from this chapter, experiment with the examples
illustrated in this section. Add and remove components from the existing classes to get a
proper feel for the key characteristics of the different layout styles. What happens if there is no
CENTER component with
BorderLayout
, for instance?
11.5.4
Nested containers
All the layout strategies discussed above are fairly simple. The key to building good-looking
and well-behaved interfaces lies in one last detail: layouts can be nested. Many of the Swing
components are
containers.
Containers appear to the outside as a single component, but they
can contain multiple other components. Each container has its own layout manager attached.
The most-used container is the class
JPanel
. A
JPanel
can be inserted as a component into the
frame's content pane, and then more components can be laid out inside the
JPanel
. Figure 11.10,
for example, shows an interface arrangement similar to that of the BlueJ main window. The con-
tent pane of this frame uses a
BorderLayout
, where the EAST position is unused. The NORTH
area of this
BorderLayout
contains a
JPanel
with a horizontal
FlowLayout
that arranges its
components (say toolbar buttons) in a row. The SOUTH area is similar: another
JPanel
with a
FlowLayout
.
Figure 11.10
Building an interface
using nested
containers
Search WWH ::
Custom Search