Java Reference
In-Depth Information
36
37
// create horizontal glue and add buttons to Box horizontal2
38
for
(
int
count =
0
; count <
SIZE
; count++)
39
{
40
horizontal2.add(Box.createHorizontalGlue());
horizontal2.add(
new
JButton(
"Button "
+ count));
41
42
}
43
44
// create rigid area and add buttons to Box vertical2
45
for
(
int
count =
0
; count <
SIZE
; count++)
46
{
47
vertical2.add(Box.createRigidArea(
new
Dimension(
12
,
8
)));
vertical2.add(
new
JButton(
"Button "
+ count));
48
49
}
50
51
// create vertical glue and add buttons to panel
52
JPanel panel =
new
JPanel();
53
panel.setLayout
new
BoxLayout(panel,
BoxLayout.Y_AXIS
)
();
54
55
for
(
int
count =
0
; count <
SIZE
; count++)
56
{
57
panel.add(Box.createGlue());
panel.add(
new
JButton(
"Button "
+ count));
58
59
}
60
61
// create a JTabbedPane
62
JTabbedPane tabs =
new
JTabbedPane(
JTabbedPane.TOP
,
JTabbedPane.SCROLL_TAB_LAYOUT
);
63
64
65
// place each container on tabbed pane
66
tabs.addTab(
"Horizontal Box"
, horizontal1);
67
tabs.addTab(
"Vertical Box with Struts"
, vertical1);
68
tabs.addTab(
"Horizontal Box with Glue"
, horizontal2);
69
tabs.addTab(
"Vertical Box with Rigid Areas"
, vertical2);
70
tabs.addTab(
"Vertical Box with Glue"
, panel);
71
72
add(tabs);
// place tabbed pane on frame
73
}
// end BoxLayoutFrame constructor
74
}
// end class BoxLayoutFrame
Fig. 22.16
|
BoxLayout
layout manager. (Part 2 of 2.)
1
// Fig. 22.17: BoxLayoutDemo.java
2
// Demonstrating BoxLayout.
3
import
javax.swing.JFrame;
4
5
public
class
BoxLayoutDemo
6
{
7
public
static
void
main(String[] args)
8
{
9
BoxLayoutFrame boxLayoutFrame =
new
BoxLayoutFrame();
Fig. 22.17
|
Test class for
BoxLayoutFrame
. (Part 1 of 2.)