Java Reference
In-Depth Information
Laying Out Components
Once you've set the layout manager of a container to BoxLayout , that's really all you do directly
with the layout manager. Adding components to the container is done with either the
add(Component component) or add(Component component, int index) method. Although
BoxLayout implements the LayoutManager2 interface, implying the use of constraints, it currently
uses none. Therefore, it isn't necessary to use add(Component component, Object constraints) .
When it comes time to lay out the container, BoxLayout does its work. The BoxLayout manager
tries to satisfy the minimum and maximum sizes of the components within the container, as
well as their x-axis and y-axis alignments. Alignment values range from 0.0f to 1.0f. (Alignment
settings are floating-point constants, not doubles, hence the need for the f .)
By default, all Component subclasses have an x-axis alignment of Component.CENTER_ALIGNMENT
and a y-axis alignment of Component.CENTER_ALIGNMENT . However, all AbstractButton subclasses
and JLabel have a default x-axis alignment of Component.LEFT_ALIGNMENT . Table 10-4 shows
the constants available from Component for these component properties, settable with either
setAlignmentX(float newValue) or setAlignmentY(float newValue) . The different alignments
work identically, except in different directions. In the case of horizontal alignments, this is
similar to left-, center-, or right-justifying a paragraph.
Table 10-4. Component Alignments
Setting
Value
Vertical Alignment
Component.TOP_ALIGNMENT
0.0f
Component.CENTER_ALIGNMENT
0.5f
Component.BOTTOM_ALIGNMENT
1.0f
Horizontal Alignment
Component.LEFT_ALIGNMENT
0.0f
Component.CENTER_ALIGNMENT
0.5f
Component.RIGHT_ALIGNMENT
1.0f
Laying Out Components with the Same Alignments
The BoxLayout manager acts differently depending on the alignment of the components within
the container being managed. If all the alignments are the same, those components whose
maximum size is smaller than the container will be aligned based on the alignment setting. For
instance, if you have a wide area with a vertical BoxLayout and small buttons within it, the
horizontal alignment will serve to left-, center-, or right-justify the buttons. Figure 10-9 shows
how this looks.
Search WWH ::




Custom Search