Java Reference
In-Depth Information
If the components are aligned vertically, the BoxLayout manager
attempts to give them all the same width, based on the component with
the largest width. Components in this axis get their preferred height.
■■
In most situations, the Box class is used when working with BoxLayout,
as opposed to creating a BoxLayout manager directly. You can think of
a Box as a Panel with BoxLayout.
■■
The BoxLayout class has one constructor:
public BoxLayout(Container target, int axis). Creates a new BoxLayout
manager for the specified target and given axis.
The possible values of axis are as follows:
BoxLayout.X_AXIS.
The components are displayed horizontally from
left to right.
BoxLayout.Y_AXIS.
The components are displayed vertically from top to
bottom.
BoxLayout.LINE_AXIS. Similar to X_AXIS, the components are laid out
horizontally. The order they are displayed is different, depending on the
container's ComponentOrientation property, which allows components
to be displayed from right to left or from left to right.
BoxLayout.PAGE_AXIS. Components are laid out like words on a page
based on the container's ComponentOrientation property, which allows
the components to be laid out from right to left, from left to right, from
top to bottom, or from bottom to top.
You can assign BoxLayout to a Panel, JFrame, or any other container, but the
simplest way to use BoxLayout is to instantiate a Box object, place your com-
ponents in the Box, and place the Box into your container. The constructor of
the Box class takes in one of these axis values as well:
public Box(int axis)
The following BoxLayoutDemo program demonstrates how to use the Box
class to align a collection of components vertically within a JFrame. Study the
program and try to determine its output, which is shown in Figure 12.9.
Figure 12.9
Output of the BoxLayoutDemo program.
Search WWH ::




Custom Search