Java Reference
In-Depth Information
// Create a Box with horizontal alignment
Box box
=
Box.createHorizontalBox ();
// Add the components to the Box
box.add (new JButton ("One"));
box.add (new JButton ("Two"));
box.add (new JButton ("Three"));
box.add (new JButton ("Four"));
box.add (new JButton ("Five"));
// And add the Box to the JApplet panel.
Content
-
pane.add (box);
} // init
} //class BoxApplet
In Figure 7.9(a) the components bunch toward the top and leave a gap at
the bottom and in Figure 7.9(b) the components bunch to the left. Whenever the
individual components have a maximum size in vertical or horizontal dimensions,
this unattractive bunching will occur. To allow for arranging the components with
spacing between them, the
Box
provides methods to create three types of invisible
elements:
Glue - surplus space in between components or between a component and container
side.
Strut-afixedwidth or height spacing for horizontal or vertical alignments.
RigidArea - both width and height dimensions are fixed values.
Figure 7.9(c) shows the buttons arranged in a
Box
according to the following code
where we use some horizontal glue and struts to specify the spacing between the
components:
import javax.swing.*;
import java.awt.*;
/** Demo of Box with glue and struts. **/
public class
BoxSpacingApplet
extends JApplet
{
public void init () {
Container content
-
pane = getContentPane ();
// Create a Box with horizontal alignment.
Box box = Box.createHorizontalBox ();
// Add the buttons plus spacing components.
box.add (Box.createHorizontalGlue ());
Search WWH ::
Custom Search