Why is this button resized

in this testcase, why is the first button resized, i am trying to have the content pane be as wide as the widest component but not affect them.
package gaillard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.SpringLayout.Constraints;
* @author gaillard
public class Test
     SpringLayout layout = new SpringLayout();
     JFrame frame = new JFrame("Test");
     JPanel contentPane = new JPanel();
     JButton button = new JButton("TEST");
     JButton button2 = new JButton("Test2Andsome more");
     /** Creates a new instance of Test */
     public Test()
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setContentPane(contentPane);
          contentPane.setLayout(layout);
          contentPane.add(button);
          contentPane.add(button2);
          Constraints buttonCon = layout.getConstraints(button);
          Constraints buttonCon2 = layout.getConstraints(button2);
          Constraints contentPaneCon = layout.getConstraints(contentPane);
          buttonCon.setConstraint(SpringLayout.NORTH,
                    Spring.sum(contentPaneCon.getConstraint(SpringLayout.NORTH),
                                   Spring.constant(5)));
          buttonCon.setConstraint(SpringLayout.WEST,
                    Spring.sum(contentPaneCon.getConstraint(SpringLayout.WEST),
                                        Spring.constant(5)));
          buttonCon2.setConstraint(SpringLayout.NORTH,
                    Spring.sum(buttonCon.getConstraint(SpringLayout.SOUTH),
                         Spring.constant(5)));
          buttonCon2.setConstraint(SpringLayout.WEST,
                    buttonCon.getConstraint(SpringLayout.WEST));
          contentPaneCon.setConstraint(SpringLayout.EAST,
                    Spring.sum(Spring.constant(5),
                         Spring.max(buttonCon.getConstraint(SpringLayout.EAST),
          buttonCon2.getConstraint(SpringLayout.EAST))));
          contentPaneCon.setConstraint(SpringLayout.SOUTH,
                    Spring.sum(Spring.constant(5),
                         buttonCon2.getConstraint(SpringLayout.SOUTH)));
          frame.pack();
          frame.setVisible(true);
     public static void main(String[] args)
          EventQueue.invokeLater(new Runnable()
                                   @Override
                                   public void run()
                                        new Test();
}

sorry here is the code formated
package gaillard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.SpringLayout.Constraints;
* @author gaillard
public class Test
     SpringLayout layout = new SpringLayout();
     JFrame frame = new JFrame("Test");
     JPanel contentPane = new JPanel();
     JButton button = new JButton("TEST");
     JButton button2 = new JButton("Test2Andsome more");
     /** Creates a new instance of Test */
     public Test()
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setContentPane(contentPane);
          contentPane.setLayout(layout);
          contentPane.add(button);
          contentPane.add(button2);
          Constraints buttonCon = layout.getConstraints(button);
          Constraints buttonCon2 = layout.getConstraints(button2);
          Constraints contentPaneCon = layout.getConstraints(contentPane);
          buttonCon.setConstraint(SpringLayout.NORTH,
                                        Spring.sum(contentPaneCon.getConstraint(SpringLayout.NORTH),
                                                     Spring.constant(5)));
          buttonCon.setConstraint(SpringLayout.WEST,
                                        Spring.sum(contentPaneCon.getConstraint(SpringLayout.WEST),
                                                     Spring.constant(5)));
          buttonCon2.setConstraint(SpringLayout.NORTH,
                                         Spring.sum(buttonCon.getConstraint(SpringLayout.SOUTH),
                                                       Spring.constant(5)));
          buttonCon2.setConstraint(SpringLayout.WEST,
                                         buttonCon.getConstraint(SpringLayout.WEST));
          contentPaneCon.setConstraint(SpringLayout.EAST,
                                              Spring.sum(Spring.constant(5),
                                                            Spring.max(buttonCon.getConstraint(SpringLayout.EAST),
                                                                         buttonCon2.getConstraint(SpringLayout.EAST))));
          contentPaneCon.setConstraint(SpringLayout.SOUTH,
                                              Spring.sum(Spring.constant(5),
                                                            buttonCon2.getConstraint(SpringLayout.SOUTH)));
          frame.pack();
          frame.setVisible(true);
     public static void main(String[] args)
          EventQueue.invokeLater(new Runnable()
                                      @Override
                                      public void run()
                                           new Test();
}

Similar Messages

Maybe you are looking for