excerpted from Http://tech.it168.com/a2009/0211/265/000000265087_all.shtml
Excerpted from http://cache.baiducontent.com/c?m= 9f65cb4a8c8507ed4fece763105392230e54f7386c88c7150885ce178e320c051038bef970625559939c3d7a50f35404b9b4656f6f1420c1c49fc848d 6b89729329c6269304a8900509342f39e5124b17ec311b4e85f&p=cb759a46d7c059fc57efcd605753c1&newp= 836cc54ad5c242e019bac7710f528d231610db2151d7da1f6b82c825d7331b001c3bbfb423231406d1c47b620baf425debf23c7637012ba3dda5c91d9 Fb4c57479c76663&user=baidu&fm=sc&query=springlayout&qid=c854ccd90002264b&p1=5
Merlin's Magic: Springlayout Manager
When GridBagLayout is not enough
When designing a complex interface, many developers always avoid using the powerful GridBagLayout manager. Instead of fiddling with gridbagconstraints, they use other layout managers like BorderLayout to embed panels in the panel. The newly introduced Springlayout Manager provides an alternative way for components to be placed relative to other components. In this article, Merlin Insider John Zukowski shows you how to use this new layout manager. Share your thoughts on this article with the author and other readers on the discussion forums (or click the discussion at the top or bottom of the article to access the Forum).
In the Java Layout manager, the latest addition to the Springlayout manager, which was published with Java 1.4. This layout manager allows you to attach "Spring" to a component so that the component can be laid out relative to other components. For example, with Springlayout you can have a button attached to the right border for display, regardless of how large the user sets the screen width.
Getting Started with Springlayout
The simplest way to use is shown in the bottom, yellow Listing 4. Complete Springlayout Instances
As with all layout managers, the Springlayout manager is responsible for arranging the location of the components. The location of the component is controlled by providing it with the associated constraints. For Springlayout-controlled components, there is a constraint with 4 set values-each setting value corresponds to each edge of the component. The Springlayout manager relies on a Springlayout.constraints object to provide these component constraints. This is somewhat similar to how the Gridbagconstraints class works, and the Gridbagconstraints class complements the GridBagLayout Manager: Each component added to a container can have one attached to it The Springlayout.constraints object. However, the similarities between the two are just that.
When using GridBagLayout, it is common practice to use constraints to add components to the container. With the Springlayout Manager, you typically do not have to use constraints to add components. Instead, you can add components directly, and then attach constraints to them separately. In addition to springlayout, there is nothing to prevent you from adding constraints to your component. Constraints is not a simple class. It is a collection of Spring objects (for each edge). When you use the Springlayout.constraints class, you need to add each Spring constraint to the springlayout.constraints separately. You "add" constraints to springlayout.constraints by setting specific constraints on one edge of the component. By using the 4 Springlayout constants of EAST, WEST, north, and South, you can call Springlayout.constraints's setcontraints (String edge, Spring Spring) method, where a parameter of type String is one of 4 constants. For example, if you want to add a component to the top-left position of a container, you can create two fixed-size spring, combine them, and then add the component to a container with a spring set of such a combination, as shown in Listing 1:
Listing 1. Using Springlayout
1Component Left=...;
2springlayout Layout=Newspringlayout ();
3JPanel Panel=NewJPanel (layout);
4Spring Xpad=Spring.constant (5);
5Spring Ypad=Spring.constant ( -);
6springlayout.constraints Constraint=Newspringlayout.constraints ();
7 Constraint.setconstraint (Springlayout.west, Xpad);
8Constraint.setconstraint (Springlayout.north, Ypad);
9Contentpane.add (left, constraint);
Ten
This may not seem particularly difficult, but when you need to add the next component, whether it's to the right or the bottom of the first component, things are a little trickier. You cannot simply add components outside of n pixels; in fact, you must add padding (filler) for the previous component. In order to find the edges of the previous component, you can use the Getconstraint () method to request the layout manager to pass the edges and components that you target for the method, such as Layout.getconstraint (Springlayout.east, left), This is where the correct edge of the first component is obtained. From this location, you can add the required padding and attach it to the edges of other components, as shown in Listing 2:
Listing 2. Add a second component using Springlayout
1Component Right= ...;
2Spring Rightsideofleft=Layout.getconstraint (Springlayout.east, left);
3Spring Pad=Spring.constant ( -);
4 Spring leftedgeofright=spring.sum (rightsideofleft, pad);
5constraint=Newspringlayout.constraints ();
6Constraint.setconstraint (Springlayout.west, leftedgeofright);
7Constraint.setconstraint (Springlayout.north, Ypad);
8 Contentpane.add (right, constraint);
9
This is a very effective approach, but it is tedious if the number of components is more. Conversely, another way to avoid intermediate steps is to add the components directly without constraints, then add the constraints separately, and use the Springlayout Putconstraint () method to connect the constraints to the component, as shown in Listing 3:
Listing 3. Add a second component with Springlayout
1 PublicvoidPutconstraint (String E1, Component C1,intPad,
2String E2, Component C2)
3 PublicvoidPutconstraint (String E1, Component C1, Spring S,
4String E2, Component C2)
5
Here, you do not need to request the side of the component and add yourself to the padding, and the Putconstraint () method call handles both tasks for you at the same time. To demonstrate this, listing 4 adds the same constraints to the correct component as in Listing 3, but uses Putconstraint () instead of using springlayout.constraints directly:
Listing 4. Add a second component using Putconstraint ()
1Component Left= ...;
2Component Right= ...;
3springlayout Layout=Newspringlayout ();
4JPanel Panel=NewJPanel (layout);
5Panel.add (left);
6Panel.add (right);
7 Layout.putconstraint (Springlayout.west, left,5,
8springlayout.west, panel);
9Layout.putconstraint (Springlayout.north, left, -,
TenSpringlayout.north, panel);
OneLayout.putconstraint (Springlayout.north, right, -,
ASpringlayout.north, panel);
-Layout.putconstraint (Springlayout.west, right, -,
-Springlayout.east, left);
the
The String type parameter in the Putconstraint () method is 4 Springlayout constants EAST, WEST, north, and south. When using Putconstraint (), be sure to specify the location of the unknown component and connect it to something that can be computed or fixed, such as the bounds of the screen.
Test Springlayout with Beanbuilder
To help you experience the use of Springlayout, Sun provides a tool called beanbuilder (see Resources). The tool is more useful when using the JavaBeans component, but it also provides an easy way to study springlayout. Figure 1 shows how the tool looks when it starts:
Figure 1. Beanbuilder Start Screen
While we are not going to discuss the details of the Beanbuilder tool, there is one place to talk about this tool, which is to use the Springlayout connection component. There are 4 small squares on each side of each component, corresponding to North (northern), South (southern), East (east), and West (western). You can drag an arrow from a small box to connect it to any other small box. If the tool is a bit more advanced, it will allow you to specify the gap width, but figure 2 shows a screen in the development phase:
Figure 2. Beanbuilder using the screen
As illustrated in Figure 2, you can visually connect the arrows to a specified Putconstraint () call.
Pasting
In order to demonstrate the use of springlayout, listing 4 shows the Springformtest program, which spliced together the code snippets that were previously explained using Putconstraint ().
Listing 4. A complete Springlayout instance
1Importjava.awt.*;
2Importjavax.swing.*;
3 Public classSpringformtest {
4 Public Static voidMain (String args[]) {
5JFrame Frame= NewJFrame ("Spring Form");
6frame.setdefaultcloseoperation (jframe.exit_on_close);
7Container ContentPane=Frame.getcontentpane ();
8 springlayout Layout= Newspringlayout ();
9contentpane.setlayout (layout);
TenComponent Left= NewJLabel (" Left");
OneComponent Right= NewJTextField ( the);
AContentpane.add (left);
-Contentpane.add (right);
-Layout.putconstraint (Springlayout.west, left,Ten,
thespringlayout.west, ContentPane);
-Layout.putconstraint (Springlayout.north, left, -,
- Springlayout.north, ContentPane);
-Layout.putconstraint (Springlayout.north, right, -,
+Springlayout.north, ContentPane);
-Layout.putconstraint (Springlayout.west, right, -,
+ Springlayout.east, left);
AFrame.setsize ( -, -);
atframe.show ();
- }
-}
-
Figure 3 shows the result:
Figure 3. Springformtest Instance Screen
After stretching
Merlin's Magic: Springlayout Manager