Java Reference
In-Depth Information
34
String[] names = { "zero" , "one" , "two" , "three" , "four" };
35
JButton[] buttons = new JButton[names.length];
36
37
for ( int count = 0 ; count < buttons.length; count++)
38
buttons[count] = new JButton(names[count]);
39
40
// define GUI component constraints for textField
41
constraints.weightx = 1 ;
constraints.weighty = 1 ;
constraints.fill = GridBagConstraints.BOTH ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
addComponent(textField);
42
43
44
45
46
47
// buttons[0] -- weightx and weighty are 1: fill is BOTH
48
constraints.gridwidth = 1 ;
addComponent(buttons[ 0 ]);
49
50
51
// buttons[1] -- weightx and weighty are 1: fill is BOTH
52
constraints.gridwidth = GridBagConstraints.RELATIVE ;
addComponent(buttons[ 1 ]);
53
54
55
// buttons[2] -- weightx and weighty are 1: fill is BOTH
56
constraints.gridwidth = GridBagConstraints.REMAINDER ;
addComponent(buttons[ 2 ]);
57
58
59
// comboBox -- weightx is 1: fill is BOTH
60
constraints.weighty = 0 ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
addComponent(comboBox);
61
62
63
64
// buttons[3] -- weightx is 1: fill is BOTH
65
constraints.weighty = 1 ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
addComponent(buttons[ 3 ]);
66
67
68
69
// buttons[4] -- weightx and weighty are 1: fill is BOTH
70
constraints.gridwidth = GridBagConstraints.RELATIVE ;
addComponent(buttons[ 4 ]);
71
72
73
// list -- weightx and weighty are 1: fill is BOTH
74
constraints.gridwidth = GridBagConstraints.REMAINDER ;
addComponent(list);
75
76
} // end GridBagFrame2 constructor
77
78
// add a component to the container
79
private void addComponent(Component component)
80
{
81
layout.setConstraints(component, constraints);
add(component); // add component
82
83
}
84
} // end class GridBagFrame2
Fig. 22.23 | GridBagConstraints constants RELATIVE and REMAINDER . (Part 2 of 2.)
Search WWH ::




Custom Search