Java Reference
In-Depth Information
// third button and 5 pixels below it
layout.putConstraint (SpringLayout.WEST, five,
25, SpringLayout.EAST, three);
layout.putConstraint (SpringLayout.NORTH, five,
5, SpringLayout.SOUTH, three);
} // ctor
} // class SpringPanel
The program uses the
putConstraint()
method to arrange the locations of
five buttons. The following code, for example, puts five pixels between the left
side of the first button and the left side of the container:
layout.putConstraint (SpringLayout.WEST, one, 5,
SpringLayout.WEST, this);
Forafew components this layout can be quite convenient, but the coding becomes
tedious to deal with a large number of components. The
SpringLayout
is
intended primarily for use with so-called
GUI builder
programs with which a user
interactively builds an interface and the program takes care of all the code details.
The Sun tutorial
Creating a GUI with JFC/Swing
[3] provides a
SpringUtil-
ities
class that assists with building interfaces with the
SpringLayout
.
7.4.6
GridBagLayout
The most powerful layout manager is the
GridBagLayout
.You can arrange the
components in virtually any possible manner with it. However, it can be rather
tricky and often requires several iterations of adjusting the settings to get the
display just as you want it. In fact, if your interface has grown to the point you
need a
GridBagLayout
,you may find it a lot easier to use a GUI builder. We
will have room here only to give a brief introduction to this layout along with an
example to illustrate its capabilities. See the Web Course and the references for
more details.
As its name indicates, the
GridBagLayout
manager uses a grid like
Grid-
Layout
does but it works more like the hypertext table in the HTML language.
That is, instead of a fixed number of rows and cells with fixed cell dimensions,
this layout lets components span more than one cell vertically and horizontally.
Also, the relative widths of columns and the heights of rows can vary.
The
GridBagLayout
class does not provide an extensive set of methods to
specify the layout. Instead, each component is paired with an instance of the helper
class
GridBagConstraints
that specifies several parameters for the layout to
set the location and size of the component. For example, unlike
GridLayout
,you
do not set explicit dimensions for the grid in the constructor. For each component
Search WWH ::
Custom Search