title: How can I align my Jframe labels and buttons
body: <p>I am trying to edit a piece of sample code that I found to create a form. The sample code included 4 labels and text fields aligned perfectly. I am trying to add in a button at the end but in stead the button is overlapping the labels in the top left hand corner of the screen. How can I fix this?</p>
  
  <pre><code>public class SpringDemo {
  private static void createAndShowGUI() {
      String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
      int labelsLength = labels.length;
  
      //Create and populate the panel.
      JPanel p = new JPanel(new SpringLayout());
      for (int i = 0; i &lt; labelsLength; i++) {
          JLabel l = new JLabel(labels[i], JLabel.TRAILING);
          p.add(l);
          JTextField textField = new JTextField(10);
          l.setLabelFor(textField);
          p.add(textField);
      }
      JButton l = new JButton("Submit");
      p.add(l);
  
      //Lay out the panel.
      SpringUtilities.makeCompactGrid(p,
                                      labelsLength, 2, //rows, cols
                                      7, 7,        //initX, initY
                                      7, 7);       //xPad, yPad
  
      //Create and set up the window.
      JFrame frame = new JFrame("SpringForm");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      //Set up the content pane.
      p.setOpaque(true);  //content panes must be opaque
      frame.setContentPane(p);
  
      //Display the window.
      frame.pack();
      frame.setVisible(true);
  }
  
  public static void main(String[] args) {
      //Schedule a job for the event-dispatching thread:
      //creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              createAndShowGUI();
          }
      });
  }
  </code></pre>
  
  <p>}</p>