Java examples for Swing:SpringLayout
Using the putConstraint() Method of the SpringLayout Class
import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; 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"); // Add two JButtons without using any constraints contentPane.add(b1);/* w w w .ja v a2 s .c o m*/ contentPane.add(b2); springLayout.putConstraint(SpringLayout.WEST, b1, 10, SpringLayout.WEST,contentPane); springLayout.putConstraint(SpringLayout.NORTH, b1, 20, SpringLayout.NORTH,contentPane); springLayout.putConstraint(SpringLayout.WEST, b2, 10, SpringLayout.EAST, b1); springLayout.putConstraint(SpringLayout.NORTH, b2, 20, SpringLayout.NORTH,contentPane); springLayout.putConstraint(SpringLayout.SOUTH, contentPane, 10,SpringLayout.SOUTH, b1); springLayout.putConstraint(SpringLayout.EAST, contentPane, 10,SpringLayout.EAST, b2); frame.pack(); frame.setVisible(true); } }