Java Reference
In-Depth Information
box.add (new JButton ( " One " ));
box.add (Box.createHorizontalStrut (5));
box.add (new JButton (" Two " ));
box.add (Box.createHorizontalStrut (5));
box.add (new JButton ("Three"));
box.add (Box.createHorizontalGlue ());
box.add (new JButton ("Four"));
box.add (Box.createHorizontalGlue ());
box.add (new JButton ("Five"));
box.add (Box.createHorizontalGlue ());
Figure 7.10 Arranging
components with
BorderLayout .
// And add box to the Applet's panel.
content - pane.add (box);
} // init
} // class BoxSpacingApplet
We see that this technique produces a more attractive arrangement of the com-
ponents. Similar code can be used for vertical layouts.
7.4.3 BorderLayout
If you have two to five components to group together then a BorderLayout is
often a convenient layout mananger. Figure 7.10 shows the result of the following
code that uses a BorderLayout :
public class BorderApplet extends JApplet
{
...init() builds the interface ...
} // class BorderApplet
/** Arrange five buttons using a BorderLayout. **/
class BorderPanel extends JPanel
{
BorderPanel () {
setLayout (new BorderLayout ());
add (BorderLayout.EAST, new JButton ("East"));
add (BorderLayout.WEST, new JButton ("West"));
add (BorderLayout.NORTH, new JButton ("North"));
add (BorderLayout.SOUTH, new JButton ("South"));
add (BorderLayout.CENTER, new JButton ("Center"));
} //ctor
} //class BorderPanel
Search WWH ::




Custom Search