title: Working with multiple panels in one frame
body: <p>I have a problem where i want a program to switch between multiple panels in a same frame. The problems I am encountering are that i can't set the layout when the panels switch and after the switch content is lowering pixel by pixel. Here is my code.</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.EventQueue;
  import java.awt.FlowLayout;
  import javax.swing.JFrame;
  
  public class Main {
  
      public static boolean logged_in = false;
  
      public static int width = 200, height = 400;
  
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      Dimension d = new Dimension(width, height);
                      First frame = new First();
                           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setSize(d);
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                      frame.setResizable(true);
                      frame.setLayout(new FlowLayout());  
                  } catch (Exception e) { 
                      e.printStackTrace();    
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>and here are the two classes that hold frames.</p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  
  public class First extends JFrame {
  
      JPanel contentPane;
      private JButton button_1;
      private JTextField text;
  
      public First(){
  
          contentPane = new JPanel();
          setContentPane(contentPane);
  
          button_1 = new JButton("Second frame");
          button_1.setVisible(true);
          text = new JTextField(20);
          text.setVisible(true);
  
          contentPane.add(button_1);
          contentPane.add(text);
  
          thehandler handler = new thehandler();
  
          button_1.addActionListener(handler);
      }
  
      private class thehandler implements ActionListener {
  
          public void actionPerformed(ActionEvent event) {
  
              if (event.getSource() == button_1) {
                  contentPane.removeAll();
                  contentPane.invalidate();
                  Second frame = new Second();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationRelativeTo(null);
                  frame.setResizable(true);
                  frame.setLayout(new FlowLayout());
                  frame.contentPane.setVisible(true);
                  contentPane.add(frame.contentPane);
                  ((JPanel) contentPane).revalidate();
                  contentPane.setSize(200, 400);
                  contentPane.repaint();
              }
          }
      }
  }
  </code></pre>
  
  <p>and the second one</p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  
  public class Second extends JFrame {
  
      JPanel contentPane;
      private JButton button_1;
      private JTextField text;
  
      public Second(){
  
          contentPane = new JPanel();
          setContentPane(contentPane);
  
          button_1 = new JButton("First frame");
          button_1.setVisible(true);
          text = new JTextField(20);
          text.setVisible(true);
  
          contentPane.add(button_1);
          contentPane.add(text);
  
          thehandler handler = new thehandler();
  
          button_1.addActionListener(handler);
      }
  
      private class thehandler implements ActionListener {
  
          public void actionPerformed(ActionEvent event) {
  
              if (event.getSource() == button_1) {
                  contentPane.removeAll();
                  contentPane.invalidate();
                  First frame = new First();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationRelativeTo(null);
                  frame.setResizable(true);
                  frame.setLayout(new FlowLayout());
                  frame.contentPane.setVisible(true);
                  contentPane.add(frame.contentPane);
                  ((JPanel) contentPane).revalidate();
                  contentPane.setSize(200, 400);
                  contentPane.repaint();
              }
          }
      }
  }
  </code></pre>
  
  <p>Any help is appreciated. Please keep in mind I am not so good with java GUI. Ty.</p>
  
  <p><strong>EDIT</strong> After a lot of time searching for an answer i got one. It probably isn't perfect but i will post it for future reference or if anyone else needs this solution. Here is the code.</p>
  
  <p>Main frame that holds panels:</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class MainFrame extends JFrame {
  
      public static JPanel main_panel;
      private static FirstFrame first;
  
      public MainFrame(){
  
          setLayout(null);    
          setSize(400, 400);
          setLocationRelativeTo(null);
          setResizable(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
  
          main_panel = new JPanel();
          main_panel.setBounds(0, 0, 400, 400);
          add(main_panel);
  
          main_panel.invalidate();
          main_panel.removeAll();
  
          first = new FirstFrame();
          main_panel.add(first);
          main_panel.revalidate();
          main_panel.repaint();
      }
  
      public static void main(String[] args) {
          MainFrame frame = new MainFrame();
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>First panel:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JPanel;
  
  public class FirstFrame extends JPanel {
  
      private JButton button;
  
      public FirstFrame() {
  
          JPanel panel = new JPanel();
  
          panel.setPreferredSize(new Dimension(400, 400));
          panel.setLayout(null);
  
          button = new JButton("First");
          button.setBounds(20, 20, 200, 40);
          button.setVisible(true);
  
          thehandler handler = new thehandler();
  
          button.addActionListener(handler);
  
          panel.add(button);
  
          add(panel);
          revalidate();
          repaint();
      }
      private class thehandler implements ActionListener {
  
          public void actionPerformed(ActionEvent event) {
              if (event.getSource() == button) {
                  MainFrame.main_panel.invalidate();
                  MainFrame.main_panel.removeAll();
                  SecondFrame frame = new SecondFrame();
                  MainFrame.main_panel.add(frame);
                  MainFrame.main_panel.revalidate();
                  MainFrame.main_panel.repaint();
              }
          }
      }
  }
  </code></pre>
  
  <p>Second:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JPanel;
  
  public class SecondFrame extends JPanel {
  
      private JButton button;
  
      public SecondFrame() {
          JPanel panel = new JPanel();
  
          panel.setPreferredSize(new Dimension(400, 400));
          panel.setLayout(null);
  
          button = new JButton("Second");
          button.setBounds(20, 20, 200, 40);
          button.setVisible(true);
  
          thehandler handler = new thehandler();
  
          button.addActionListener(handler);
  
          panel.add(button);
  
          add(panel);
          revalidate();
          repaint();
      }
      private class thehandler implements ActionListener {
  
          public void actionPerformed(ActionEvent event) {
              if (event.getSource() == button) {
                  MainFrame.main_panel.invalidate();
                  MainFrame.main_panel.removeAll();
                  ThirdFrame frame = new ThirdFrame();
                  MainFrame.main_panel.add(frame);
                  MainFrame.main_panel.revalidate();
                  MainFrame.main_panel.repaint();
              }
          }
      }
  }
  </code></pre>
  
  <p>And third:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JPanel;
  
  public class ThirdFrame extends JPanel {
  
      private JButton button;
  
      public ThirdFrame() {
          JPanel panel = new JPanel();
  
          panel.setPreferredSize(new Dimension(400, 400));
          panel.setLayout(null);
  
          button = new JButton("Third");
          button.setBounds(20, 20, 200, 40);
          button.setVisible(true);
  
          thehandler handler = new thehandler();
  
          button.addActionListener(handler);
  
          panel.add(button);
  
          add(panel);
          revalidate();
          repaint();
      }
      private class thehandler implements ActionListener {
  
          public void actionPerformed(ActionEvent event) {
              if (event.getSource() == button) {
                  MainFrame.main_panel.invalidate();
                  MainFrame.main_panel.removeAll();
                  FirstFrame frame = new FirstFrame();
                  MainFrame.main_panel.add(frame);
                  MainFrame.main_panel.revalidate();
                  MainFrame.main_panel.repaint();
              }
          }
      }
  }
  </code></pre>
  
  <p>As you can see you can switch from panel 1 ->2 ->3 and back to 1 but not the second one.
  Ty all for your answers they were helpful. Any further suggestions are welcome.</p>