Java Reference
In-Depth Information
/** A simple example of a Panel with five buttons. **/
class
FlowPanel
extends JPanel
{
FlowPanel () {
// Default for JPanel is FlowLayout
add (new JButton ("One"));
add (new JButton ("Two"));
add (new JButton ("Three"));
add (new JButton ("Four"));
add (new JButton ("Five"));
} // ctor
} // class FlowPanel
The
FlowPanel
is a subclass of
JPanel
,which uses
FlowLayout
as its
default layout. Each component added to the panel will be inserted from left to
right until there is no room and then the components will go to the next row
down and continue left to right. By default,
FlowLayout
attempts to center
the group of components. Left- or right-alignment can be specified with one
of the overloaded constructors or with the
setAlignment()
method and the
constants
FlowLayout.LEFT
or
FlowLayout.RIGHT
.
7.4.2
BoxLayout
and
Box
FlowLayout
arranges components horizontally until it runs out of space and
then shifts down vertically to the next row. Before Swing, there was no good way
to arrange components in a vertical manner. The
javax.swing.BoxLayout
solved that omission.
BoxLayout
arranges components sequentially like the
FlowLayout
manager but it will set them either horizontally or vertically as
instructed. Unlike
FlowLayout
,however, it will not continue the components
on the next line or column when there is insufficient room. Components out of
range will not be shown. The following code produces the arrangement shown
in Figure 7.9(a):
public class
BoxLayoutApplet
extends JApplet
{
. ..init()
builds the interface
...
} // class BoxLayoutApplet
/** Arrange components with a BoxLayout manager. **/
class
BoxPanel
extends JPanel
Search WWH ::
Custom Search