title: Java - Swapping multiple JFrames on button press
body: <p>this is the first time I've had a look at JFrames and JPannels and I've come a little stuck.</p>
  
  <p>What I am trying to do is this, I wish to have an starting screen then based on the users button choice it swaps to another screen. To start I have only 2 screens, however once I've moved on there will be multiple screens. I've looked at CardLayout and while that is good it's not the way I wish to go I want to be able to do this first. Here is what I have.</p>
  
  <p>Main.java</p>
  
  <pre><code>import java.awt.BorderLayout;
  public class Main extends JFrame {
  
  private JPanel contentPane;
  protected boolean someCondition = false;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  Main frame = new Main();
                  frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the frame.
   */
  public Main() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 450, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      contentPane.setLayout(new BorderLayout(0, 0));
      setContentPane(contentPane);
  
      if( someCondition == false ){
          showTest();
          someCondition = test.needToReg();
      }else{
          showTest2();
      }
  }
  
  private void showTest(){
      contentPane.removeAll();
      contentPane.add(new test());
      setContentPane(contentPane);
      revalidate();
      repaint();
  }
  
  private void showTest2(){
      contentPane.removeAll();
      contentPane.add(new test2());
      setContentPane(contentPane);
      revalidate();
      repaint();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>test.java</p>
  
  <pre><code>import javax.swing.JPanel;
  
  
  public class test extends JPanel {
      private JTextField textField;
      protected static boolean toReg = false;
  
      /**
       * Create the panel.
       */
      public test() {
          setLayout(null);
  
          JButton btnNewButton = new JButton("New button");
          btnNewButton.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseClicked(MouseEvent e) {
                  System.out.println("Mouse Clicked");
                  System.out.println("Before " + toReg);
                  toReg = true;
                  System.out.println("After " + toReg);
              }
          });
          btnNewButton.setBounds(188, 166, 89, 23);
          add(btnNewButton);
  
          textField = new JTextField();
          textField.setBounds(150, 135, 86, 20);
          add(textField);
          textField.setColumns(10);
  
          JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
          rdbtnNewRadioButton.setBounds(6, 166, 109, 23);
          add(rdbtnNewRadioButton);
      }
  
      public static boolean needToReg(){
          return toReg;
      }
  }
  </code></pre>
  
  <p>test2.java</p>
  
  <pre><code>import javax.swing.JPanel;
  import javax.swing.JButton;
  import javax.swing.JLabel;
  
  
  public class test2 extends JPanel {
  
      /**
       * Create the panel.
       */
      public test2() {
          setLayout(null);
  
          JButton btnNewButton = new JButton("New button");
          btnNewButton.setBounds(56, 59, 89, 23);
          add(btnNewButton);
  
          JLabel lblNewLabel = new JLabel("New label");
          lblNewLabel.setBounds(122, 165, 46, 14);
          add(lblNewLabel);
  
      }
  }
  </code></pre>
  
  <p>Running the program with the outputs I included I get this.</p>
  
  <pre><code>Mouse Clicked
  Before false
  After true
  Mouse Clicked
  Before true
  After true
  Mouse Clicked
  Before true
  After true
  Mouse Clicked
  Before true
  After true
  Mouse Clicked
  Before true
  After true
  </code></pre>
  
  <p>I hope it's clear what I am trying to do and I hope you can lend a hand with this. Thanks</p>