Java Reference
In-Depth Information
object needs to be told about (i.e., given a reference to) the container (here,
retval , a JPanel ) whose objects it will manage, but that the container also
needs to be told (via setLayout() ) about the BoxLayout object. A bit
confusing, perhaps.
Another handy part of BoxLayout is the uses of rigid areas , invisible ob-
jects that do nothing except putting some space between objects. These rigid
areas are defined in pixels; for our GUI we create them with no height and a
width of ten pixels. They are held together using “horizontal glue” (see line 226)
226 retval.add(Box.createHorizontalGlue());
so that if the window is stretched, the extra space doesn't get added between
the buttons, but only to the “glue” component, which absorbs all extra space.
This keeps all the buttons to the right hand side of the window.
16.7.2.4
The method named createButtons() actually packs up the buttons into a
JPanel to return to the caller. It begins like this:
JButton s
218 private Component
219 createButtons(JRootPane root)
220 {
221 JPanel retval = new JPanel(); // default: flow layout
222
223 //Lay out the buttons from left to right.
224 retval.setLayout(new BoxLayout(retval, BoxLayout.X_AXIS));
225 retval.setBorder
(BorderFactory.createEmptyBorder(10, 10, 10, 10));
226 retval.add(Box.createHorizontalGlue());
227 retval.add(creat);
228 retval.add(Box.createRigidArea(new Dimension(10, 0)));
229 retval.add(view);
230 retval.add(Box.createRigidArea(new Dimension(10, 0)));
231 retval.add(clos);
The buttons themselves were created at the beginning of this class, in
lines 27-29, thus:
27 private JButton creat = new JButton("New Subaccount");
28 private JButton view = new JButton("View Subaccount");
29 private JButton clos = new JButton("Quit");
Search WWH ::




Custom Search