Java Reference
In-Depth Information
the components into rows and columns. The initial row and column numbers should be
0, so that the
GridBagLayout
layout manager can use the row and column numbers to
properly place the components in the grid. Figure 22.18 demonstrates drawing the lines
for the rows and columns over a GUI.
Column
0
1
2
0
1
2
3
Row
Fig. 22.18
|
Designing a GUI that will use
GridBagLayout
.
GridBagConstraints
A
GridBagConstraints
object describes how a component is placed in a
GridBagLayout
.
Several
GridBagConstraints
fields are summarized in Fig. 22.19.
Field
Description
Specifies the relative position (
NORTH
,
NORTHEAST
,
EAST
,
SOUTHEAST
,
SOUTH
,
SOUTHWEST
,
WEST
,
NORTHWEST
,
CENTER
) of the component in an area that it
does not fill.
anchor
Resizes the component in the specified direction (
NONE
,
HORIZONTAL
,
VER-
TICAL
,
BOTH
) when the display area is larger than the component.
fill
The column in which the component will be placed.
gridx
The row in which the component will be placed.
gridy
The number of columns the component occupies.
gridwidth
The number of rows the component occupies.
gridheight
The amount of extra space to allocate horizontally. The grid slot can
become wider when extra space is available.
weightx
The amount of extra space to allocate vertically. The grid slot can become
taller when extra space is available.
weighty
Fig. 22.19
|
GridBagConstraints
fields.
GridBagConstraints
Field
anchor
GridBagConstraints
field
anchor
specifies the relative position of the component in an
area that it does not fill. The variable
anchor
is assigned one of the following
GridBagCon-
straints
constants:
NORTH
,
NORTHEAST
,
EAST
,
SOUTHEAST
,
SOUTH
,
SOUTHWEST
,
WEST
,
NORTHWEST
or
CENTER
. The default value is
CENTER
.