title: Closing off a JPanel window from a JFrame [Java]
body: <p>So, i want to make a Menu screen with a JPanel, and I got this to work, but when I press the Start button, it does not close the Menu Window, and it just makes a new window, how do I either, keep it on the same window, without closing/opening the menu window, OR I would like to close the menu window and open up the Game window(JPanel), when i press the start button.</p>
  
  <p>Here is the MainClass.java</p>
  
  <pre><code>    package bombermangame;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class MainClass extends JFrame{
      private static final long serialVersionUID = 1L;
  
      public static int WIDTH = 870, HEIGHT = 800;
      public static JPanel menu = new Menu();
      public static Listener keys = new Listener();
  
      public MainClass(){
          setContentPane(menu);
          pack();
          setResizable(false);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setTitle("BomberMan V0.3");
          setSize(WIDTH, HEIGHT);
          setLocationRelativeTo(null);
          setVisible(true);
      }
  
      public static void main(String[] args) {
          new MainClass();
      }
  }
  </code></pre>
  
  <p>Here is the Menu.java class</p>
  
  <pre><code> package bombermangame;
  
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.Timer;
      import java.util.TimerTask;
  
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
  
      public class Menu extends JPanel implements ActionListener {
          private static final long serialVersionUID = 1L;
  
          private JButton startButton = new JButton("Play");
  
          private int x = 0, y = 500;
  
          private boolean down = false;
          private boolean up = true;
  
          private Timer timer = new Timer();
  
          public Menu() {
              setBackground(Color.blue);
              startButton = new JButton("Start");
              startButton.setBounds(0,0, 100, 40);
              startButton.setPreferredSize(new Dimension(100, 40));
              startButton.addActionListener(this);
              startButton.setFocusPainted(true);
              this.add(startButton);
  
  
          public void actionPerformed(ActionEvent ae) {
              Object a = ae.getSource();
              Game game = new Game();
              MainClass frm = new MainClass();
              Listener keys = new Listener();
  
              if (a == startButton) {
                  timer.cancel();
                  frm.getContentPane().remove(new Menu());
                  frm.addKeyListener(keys);
                  frm.setContentPane(game);
                  frm.revalidate();
                  frm.repaint();
                  game.setBackground(Color.BLACK);
                  game.setDoubleBuffered(true);
                  game.setBounds(0, 0, WIDTH, HEIGHT);
                  Game.running = true;
              }
          }
  
      }
  </code></pre>
  
  <p>EDIT: Thanks to the help of @whiskeyspider I learned that I made 2 frames and did not reference them right. But now that I got that solved, there is a problem with my Listener, when I fixed this, my Jpanel won't work with my Listener. I have tried adding the Listener straight to my Game JPanel and to my MainClass JFrame, but neither will work.</p>
  
  <p>Here is some of my Menu class, </p>
  
  <pre><code>    public void actionPerformed(ActionEvent ae) {
      Object a = ae.getSource();
      JPanel game = new Game();
      Listener keys = new Listener();
  
      if (a == startButton) {
          timer.cancel();
          MainClass.frame.getContentPane().remove(this);
          MainClass.frame.setContentPane(game);
          MainClass.frame.addKeyListener(keys);
          game.addKeyListener(keys);
          game.setBackground(Color.BLACK);
          game.setDoubleBuffered(true);
          game.setBounds(0, 0, WIDTH, HEIGHT);
          Game.running = true;
      }
  }
  </code></pre>