javacodex.com
Java Examples
Java Examples
memu home questions

SpringLayout

The SpringLayout class was added in JDK version 1.4 to support layout in GUI builders. SpringLayout is a very flexible layout manager that can emulate many of the features of other layout managers. SpringLayout is, however, very low-level and as such you really should only use it with a GUI builder, rather than attempting to code a spring layout manager by hand.

Source: (SpringLayoutExample.java)

import java.awt.*;
import javax.swing.*;
 
public class SpringLayoutExample {
 
  public static void main(String[] arguments) {
 
    // main window
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("SpringLayoutExample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,200); 
 
    Container content = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    frame.setLayout(layout);
 
 
    Component b1 = new JButton("Button 1");
    Component b2 = new JButton("Button 2");
    Component b3 = new JButton("Button 3");
    Component b4 = new JButton("Button 4");
    Component b5 = new JButton("Button 5");
 
    frame.add(b1);
    frame.add(b2);
    frame.add(b3);
    frame.add(b4);
    frame.add(b5);
 
    layout.putConstraint(SpringLayout.WEST, b1, 25, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, b1, 10, SpringLayout.NORTH, content);
 
    layout.putConstraint(SpringLayout.WEST, b2, 50, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, b2, 10, SpringLayout.SOUTH, b1);
 
    layout.putConstraint(SpringLayout.WEST, b3, 75, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, b3, 10, SpringLayout.SOUTH, b2);
 
    layout.putConstraint(SpringLayout.WEST, b4, 15, SpringLayout.EAST, b1);
    layout.putConstraint(SpringLayout.NORTH, b4, 10, SpringLayout.NORTH, content);
 
    layout.putConstraint(SpringLayout.WEST, b5, 15, SpringLayout.EAST, b1);
    layout.putConstraint(SpringLayout.NORTH, b5, 100, SpringLayout.SOUTH, b1);
 
    frame.setVisible(true);
  }
}
 

Output:

$ java SpringLayoutExample
Output



Contact: javacodex@yahoo.com