Java Reference
In-Depth Information
As
Figure 17-33
shows, the position of a component is determined by a horizontal constraint on the
x
co-
ordinate of the component and a vertical constraint on the
y
coordinate. These obviously also determine the
location of the
WEST
and
NORTH
edges of the component because the position determines where the top-left
corner is located. The width and height are determined by horizontal constraints that relate the position of
the
EAST
and
SOUTH
edges to the positions of the
WEST
and
NORTH
edges, respectively. Thus, the constraints
on the positions of the
EAST
and
SOUTH
edges are determined by constraints that are derived from the others,
as follows:
EAST_constraint = X_constraint + width_constraint
SOUTH_constraint = Y_constraint + height_constraint
You can set the
X
,
Y
,
width
, and
height
constraints independently, as you see in a moment, and you can
set a constraint explicitly for any edge. Obviously, it is possible to set constraints such that the preceding
relationships between the constraints may be violated. In this case, the layout manager adjusts one or other
of the constraints so that the preceding relationships still apply. The constraint that is adjusted depends on
which constraint you set to potentially cause the violation, as shown in
Table 17-7
:
THE LAYOUT MANAGER MAKES THE
ADJUSTMENT:
X
or the
WEST
edge
width
value set to
EAST
−
X
The
width
EAST
edge is set to
X + width
EAST
edge
X
is set to
EAST
−
width
Y
or the
NORTH
edge
height
is set to
SOUTH
−
Y
The
height
SOUTH
edge is set to
Y + height
The
SOUTH
edge
Y
is set to
SOUTH
−
height
The
SpringLayout
manager automatically adds
Spring
constraints that control the width and height of
a component based on the component's minimum, maximum, and preferred sizes. The width or height is set
to a value between the maximum and minimum for the component. These sizes are obtained dynamically
by calling the
getMinimumSize()
,
getMaximumSize()
, and
getPreferredSize()
methods for the com-