Java Reference
In-Depth Information
41
42
// weightx and weighty for button1 are both 0: the default
43
constraints.fill =
GridBagConstraints.HORIZONTAL
;
addComponent(button1,
0
,
1
,
2
,
1
);
44
45
46
// weightx and weighty for comboBox are both 0: the default
47
// fill is HORIZONTAL
48
addComponent(comboBox,
2
,
1
,
2
,
1
);
49
50
// button2
51
constraints.weightx =
1000
;
// can grow wider
constraints.weighty =
1
;
// can grow taller
constraints.fill =
GridBagConstraints.BOTH
;
addComponent(button2,
1
,
1
,
1
,
1
);
52
53
54
55
56
// fill is BOTH for button3
57
constraints.weightx =
0
;
constraints.weighty =
0
;
addComponent(button3,
1
,
2
,
1
,
1
);
58
59
60
61
// weightx and weighty for textField are both 0, fill is BOTH
62
addComponent(textField,
3
,
0
,
2
,
1
);
63
64
// weightx and weighty for textArea2 are both 0, fill is BOTH
65
addComponent(textArea2,
3
,
2
,
1
,
1
);
66
}
// end GridBagFrame constructor
67
68
// method to set constraints on
69
private
void
addComponent(Component component,
70
int
row,
int
column,
int
width,
int
height)
71
{
72
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
// set constraints
add(component);
// add component
73
74
75
76
77
78
}
79
}
// end class GridBagFrame
Fig. 22.21
|
GridBagLayout
layout manager. (Part 2 of 2.)
1
// Fig. 22.22: GridBagDemo.java
2
// Demonstrating GridBagLayout.
3
import
javax.swing.JFrame;
4
5
public
class
GridBagDemo
6
{
7
public static void
main(String[] args)
8
{
9
GridBagFrame gridBagFrame =
new
GridBagFrame();
Fig. 22.22
|
Test class for
GridBagFrame
. (Part 1 of 2.)