Java Reference
In-Depth Information
10
boxLayoutFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
boxLayoutFrame.setSize(
400
,
220
);
12
boxLayoutFrame.setVisible(
true
);
13
}
14
}
// end class BoxLayoutDemo
Arrows for cycling
through tabs
Fig. 22.17
|
Test class for
BoxLayoutFrame
. (Part 2 of 2.)
Creating
Box
Containers
Lines 19-22 create
Box
containers. References
horizontal1
and
horizontal2
are initial-
ized with
static
Box
method
createHorizontalBox
, which returns a
Box
container with
a horizontal
BoxLayout
in which GUI components are arranged left-to-right. Variables
vertical1
and
vertical2
are initialized with
static
Box
method
createVerticalBox
,
which returns references to
Box
containers with a vertical
BoxLayout
in which GUI com-
ponents are arranged top-to-bottom.
Struts
The loop at lines 27-28 adds three
JButton
s to
horizontal1
. The
for
statement at lines
31-35 adds three
JButton
s to
vertical1
. Before adding each button, line 33 adds a
ver-
tical strut
to the container with
static
Box
method
createVerticalStrut
. A vertical
strut is an invisible GUI component that has a fixed pixel height and is used to guarantee
a fixed amount of space between GUI components. The
int
argument to method
create-
VerticalStrut
determines the height of the strut in pixels. When the container is resized,
the distance between GUI components separated by struts does not change. Class
Box
also
declares method
createHorizontalStrut
for horizontal
BoxLayout
s.