title: How to make swing windows switch between content?
body: <p>Sorry if my title is a bit vague, but what I'm trying to do is make it so a button leads to another screen. I'm making a basic game so there will be several of these. The one I'm currently trying to do is the options screen. I have found a basic way of doing it using revalidate(), repaint() and multiple panels, but this opens another window each time and doesn't seem very efficient.</p>
  
  <p>This is my code for the options screen, which handles the changing of screens from the main menu to itself.</p>
  
  <p><code>
  package menu;</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import main.Window;
  
  public class Options 
  {
  JPanel pnlMain = Window.pnlMain;
  JFrame frame = new main.Window();
      JPanel pnlOptions = new JPanel();
  
  public static Button graphics = new Button("Graphics");
  
  public Options()
  {
      Listener listener = new Listener();
  
      frame.add(pnlOptions);
      frame.remove(pnlMain);
      pnlMain.revalidate();
      pnlMain.repaint();
  
      pnlMain.setVisible(false);
      pnlOptions.setVisible(true);
  
      pnlOptions.add(graphics);
  
      pnlOptions.revalidate();
      pnlOptions.repaint();
  
      graphics.addActionListener(listener);
  }
  </code></pre>
  
  <p>}
  </code></p>