Java Reference
In-Depth Information
Component
1
Comp
2
Comp
2
Component
1
Comp 3
Comp 3
FIGURE 7.10 Box layout organizes components either vertically or horizontally
first parameter the component that it will govern. Therefore a new BoxLayout
object must be created for each component. Listing 7.23 shows the panel used by
the LayoutDemo program to demonstrate the box layout.
Components in containers governed by a box layout are organized (top to bot-
tom or left to right) in the order in which they are added to the container.
There are no gaps between the components in a box layout. Unlike previous
layout managers we've explored, a box layout does not have a specific vertical or
horizontal gap that can be specified for the entire container. Instead, we can add
invisible components to the container that take up space between other compo-
nents. The Box class, which is also part of the Java standard class library, contains
static methods that can be used to create these invisible components.
The two types of invisible components used in the BoxPanel class are rigid
areas, which have a fixed size, and glue, which specifies where excess space in a
container should go. A rigid area is created using the createRigidArea method
of the Box class, and takes a Dimension object as a parameter to define the size
of the invisible area. Glue is created using the createHorizontalGlue method or
createVerticalGlue method, as appropriate.
Note that in our example, the space between buttons separated by a rigid area
remains constant even when the container is resized. Glue, on the other hand,
expands or contracts as needed to fill the space.
A box layout—more than most of the other layout managers—respects the
alignments and the minimum, maximum, and preferred sizes of the components
it governs. Therefore, setting the characteristics of the components that go into
the container is another way to tailor the visual effect.
 
Search WWH ::




Custom Search