SpringLayout: putConstraint(String e1, Component c1, Spring s, String e2, Component c2) : SpringLayout « javax.swing « Java by API






SpringLayout: putConstraint(String e1, Component c1, Spring s, String e2, Component c2)

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;

public class MainClass {
   

  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500,500);
    SpringLayout layout = new SpringLayout();      
    Container content = aWindow.getContentPane();  
    content.setLayout(layout);                     

    JButton[] buttons = new JButton[6];            
    SpringLayout.Constraints constr = null;
    for(int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i+1));
      content.add(buttons[i]);                     
   }

    Spring xSpring = Spring.constant(5,15,25);    
    Spring ySpring = Spring.constant(10,30, 50);  

    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring); 
    constr.setY(ySpring); 


    for(int i = 1 ; i< buttons.length ; i++) {
      constr = layout.getConstraints(buttons[i]);
      layout.putConstraint(SpringLayout.WEST, buttons[i],
                             xSpring,SpringLayout.EAST, buttons[i-1]);    
      layout.putConstraint(SpringLayout.NORTH, buttons[i],
                             ySpring,SpringLayout.SOUTH, buttons[i-1]);
    }
    aWindow.setVisible(true);                      // Display the window
  }
}

           
       








Related examples in the same category

1.SpringLayout.EAST
2.SpringLayout.NORTH
3.SpringLayout.WEST
4.SpringLayout.Constraints: setX(Spring x)
5.SpringLayout.Constraints: setY(Spring y)
6.SpringLayout: getConstraints(Component c)