Example usage for javax.swing Spring constant

List of usage examples for javax.swing Spring constant

Introduction

In this page you can find the example usage for javax.swing Spring constant.

Prototype

public static Spring constant(int min, int pref, int max) 

Source Link

Document

Returns a spring whose minimum, preferred, and maximum values have the values: min, pref, and max respectively.

Usage

From source file:MainClass.java

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);/* www.  j  a  v  a 2 s .c om*/
    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
}

From source file:TrySpringLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout layout = new SpringLayout();
    Container content = aWindow.getContentPane();
    content.setLayout(layout);//  www .  j a va2 s.co m
    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);
    // Hook buttons together with springs
    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
}

From source file:MainClass.java

public SpringLayoutPanel() {

    SpringLayout layout = new SpringLayout();
    setLayout(layout);// ww  w  .  j  a  va 2s  . c o m

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

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

    SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
    buttonConstr.setX(xSpring);
    buttonConstr.setY(ySpring);

    for (int i = 0; i < buttons.length; i++) {
        buttonConstr = layout.getConstraints(buttons[i]);
        buttonConstr.setHeight(ySpring);
        buttonConstr.setWidth(wSpring);

        if (i > 0) {
            layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring, SpringLayout.EAST, buttons[i - 1]);
            layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring, SpringLayout.SOUTH, buttons[i - 1]);
        }
    }

    SpringLayout.Constraints constr = layout.getConstraints(this);
    constr.setConstraint(SpringLayout.EAST,
            Spring.sum(buttonConstr.getConstraint(SpringLayout.EAST), Spring.constant(15)));
    constr.setConstraint(SpringLayout.SOUTH,
            Spring.sum(buttonConstr.getConstraint(SpringLayout.SOUTH), Spring.constant(10)));

}