Java Reference
In-Depth Information
Figure 7.12 Arranging
components with the
SpringLayout .
constraint can be placed on the distance between any two edges. This includes
the distance between the edge of one component and another, between the side
of a container and a component edge, and also between two edges of the same
component. That is, the width of a component can be constrained by setting the
distance between the left and right edges, while its height can be constrained by
setting the distance between its top and bottom edges.
When a container uses a SpringLayout you must put constraints explicitly
on each component. Otherwise, each component will put its top left corner at
the (0, 0) point of the container and the arrangement will become an overlapping
mess.
The layout specifies the edges of a component with a compass nomenclature
similar to that for BorderLayout . That is, the left edge is SpringLayout.
WEST , the right edge is SpringLayout.EAST , the bottom edge is Spring-
Layout.SOUTH , and the top is SpringLayout.NORTH . The Spring-
Layout relies on two helper classes called Spring and SpringLayout.
Constraints . The spring tries to keep a component at its preferred dimen-
sion while it resists stretching to its maximum and compressing to its minimum.
Yo u can just use the putConstraint() method to set the distance in pixels
between the edges. The following code produces the layout shown in Figure 7.12:
public class SpringApplet extends JApplet
{
...init () builds the interface...
} // class SpringApplet
/** Arrange five buttons using a SpringLayout. **/
class SpringPanel extends JPanel
{
/**
* Constructor creates 5 button interface with
* SpringLayout and constrains each to a particular
* position relative to the panel frame and to its
* neighbors.
**/
SpringPanel () {
 
Search WWH ::




Custom Search