Sets the x and y constraints for the two buttons for SpringLayout - Java Swing

Java examples for Swing:SpringLayout

Description

Sets the x and y constraints for the two buttons for SpringLayout

Demo Code

import java.awt.Container;

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

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("this is a test 2");

    // Create Constraints objects for b1 and b2
    SpringLayout.Constraints b1c = new SpringLayout.Constraints();
    SpringLayout.Constraints b2c = new SpringLayout.Constraints();

    // Create a Spring object for y value for b1 and b2
    Spring yPadding = Spring.constant(20);

    // Set (10, 20) for (x, y) for b1
    b1c.setX(Spring.constant(10));
    b1c.setY(yPadding);//from  w ww  .java 2  s.c om

    // Set (150, 20) for (x, y) for b2
    b2c.setX(Spring.constant(150));
    b2c.setY(yPadding);

    contentPane.add(b1, b1c);
    contentPane.add(b2, b2c);

    frame.pack();
    frame.setVisible(true);
  }
}

Related Tutorials