Java Reference
In-Depth Information
Figure 7.9
Arranging
components with (a) the
BoxLayout
, (b) the
Box
container, and (c) the
Box
plus glue and struts.
(a)
(b)
(c)
{
BoxPanel () {
setLayout (new BoxLayout (this,BoxLayout.Y
-
AXIS));
// Default for JPanel is BoxLayout
add (new JButton (
"
One
"
));
add (new JButton (
"
Two
"
));
add (new JButton (
"
Three
"
));
add (new JButton (
"
Four
"
));
add (new JButton (
"
Five
"
));
}
// ctor
} // class BoxPanel
The
Box
is a container like
JPanel
except that it uses
BoxLayout
as a
default instead of the
FlowLayout
used by
JPanel
.Inaddition,
Box
provides
for three special
invisible
elements that insert spacing between components. You
can create a
Box
object with its constructor or you can use two static methods to
produce
Box
instances with either horizontal or vertical alignment:
Box horizontalBox
=
Box.createHorizontalBox();
Box verticalBox
=
Box.createVerticalBox();
Figure 7.9(b) shows the buttons arranged according to the following code:
public class
BoxApplet
extends JApplet
{
public void init () {
Container content
-
pane = getContentPane ();
Search WWH ::
Custom Search