|
Example 1 Example 2 Example 3 Example 4 Example 5 Example 6
| |
BoxLayout is a layout manager provided by Swing (unlike the other layout
managers, it is located in the javax.swing package).
BoxLayout arranges a sequence of components horizontally or
vertically.
For vertical layouts, here is what the BoxLayout manager does:
- It computes the maximum width of the widest component.
- It tries to grow all components horizontally to that width.
- If a component does not actually grow to that width when requested, its
x-alignment is queried by calling its
getAlignmentX()
method. This method returns a floating-point number between 0.0
(align left) and 1.0 (align right). The default in the Component
class is 0.5 (center). The value is used to align the component
horizontally.
- The preferred height of each component is obtained. All preferred
heights are added up.
- If the total preferred height is less than the box height, then the
components are expanded, by letting them grow to their maximum
height. Components are then placed, from top to bottom, with no
additional space between them.
If the total preferred height is greater than the box height, the
components are shrunk, potentially down to their minimum height, but no
further.
If the components don't all fit at their minimum height, some of them will
not be shown.
For horizontal layouts, the process is analogous.
|