Java Reference
In-Depth Information
The static
constant()
method creates a
Spring
object from the three arguments that are the
minimum, preferred, and maximum values for the object. If all three values are equal, the object is
called a strut because its value is fixed at the common value you set for all three. There's an overloaded
version of the
constant()
method for creating struts:
Spring strut = Spring.constant(40); // min, pref, and max all set to 40
The
Spring
class also defines static methods that operate on
Spring
objects:
sum(Spring spr1, Spring spr2)
Returns a reference to a new
Spring
object that has
minimum, preferred, and maximum values that are the
sum of the corresponding values of the arguments.
minus (Spring spr)
Returns a reference to a new
Spring
object with
minimum, preferred, and maximum values that are the
same magnitude as those of the argument but with
opposite signs.
max(Spring spr1, Spring spr2)
Returns a reference to a new
Spring
object that has
minimum, preferred, and maximum values that are the
maximum of the corresponding values of the
arguments.
Setting Constraints for a Component
The
setX()
and
setY()
methods for a
SpringLayout.Constraints
object set the constraints for
the
WEST
and
NORTH
edges of the component respectively. For example:
Spring xSpring = Spring.constant(5,10,20); // Spring we'll use for X
Spring ySpring = Spring.constant(3,5,8); // Spring we'll use for Y
buttonConstr.setX(xSpring); // Set the WEST edge constraint
buttonConstr.setY(xSpring); // Set the NORTH edge constraint
The
setX()
method defines a constraint between the
WEST
edge of the container, and the
WEST
edge
of the component. Similarly, the
setY()
method defines a constraint between the
NORTH
edge of the
container and the
NORTH
edge of the component. This fixes the location of the component in relation to
the origin of the container.