title: KeyListener not responding in Java swing
body: <p>I'm making a game, and I have a Main Menu that works perfectly.  When I select one of the options, it brings up another Menu in a new window.  However in this new window, the KeyListener is not responding.  If I click back to the Main Menu window, the KeyListener is still working there.  Here is the code:</p>
  
  <p>MainMenu:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import javax.swing.*;
  import java.io.*;
  import javax.imageio.*;
  
  public class DisplayMainMenu extends JFrame implements KeyListener{
  
    static int width = 799, height = 463;
    int arrowPos = 310;
    boolean clear = true;
    BufferedImage menu = null;
    BufferedImage arrow = null;
    LevelSkip test = new LevelSkip();
    boolean done = false;
    static DisplayMainMenu main;
  
    public static void main(String[] args){
      main = new DisplayMainMenu();
      main.setResizable(false);
      main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      main.setVisible(true);
      main.init();
    }
  
    public void init() {
      try{
        menu = ImageIO.read(new File("Main Menu.png"));
        arrow = ImageIO.read(new File("arrow.png"));
      }catch(IOException ie) {
        System.out.println(ie.getMessage());
      }
      this.setSize(width, height);
      this.addKeyListener(this);
      clear = true;
      paint(getGraphics());
    }
  
    public void paint (Graphics g){
      if(clear==true){
        g.drawImage(menu,0,0,null);
        clear = false;
      }
      g.drawImage(arrow,275,arrowPos,null);
    }
    public void keyPressed(KeyEvent e){
      String key = e.getKeyText(e.getKeyCode());
      if(key == "Up"){
        clear = true;
        if (arrowPos &gt; 310)
          arrowPos -= 30;
        else
          arrowPos = 370;
        paint(getGraphics());
      }
      if(key == "Down"){
        clear = true;
        if (arrowPos &lt; 370)
          arrowPos += 30;
        else
          arrowPos = 310;
        paint(getGraphics());
      }
      if(key == "Space"){
        done = true;
        switch(arrowPos){
          case 310:  System.out.println("RUN NEW GAME"); test.init();
            break;
          case 340:  System.out.println("RUN HIGH SCORES");
            break;
          case 370:  System.exit(0);
        }
      }
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
  }
  </code></pre>
  
  <p>LevelSkip:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import javax.swing.*;
  import java.io.*;
  import javax.imageio.*;
  
  public class LevelSkip extends JFrame implements KeyListener {
  
    static int width = 799, height = 463;
    int arrowPos = 109;
    boolean clear = true;
    BufferedImage menu = null;
    BufferedImage arrow = null;
  
    public void init() {
      LevelSkip main = new LevelSkip();
      main.setSize(width, height);
      main.requestFocusInWindow();
      main.addKeyListener(main);
      main.setResizable(false);
      main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      main.setVisible(true);
      try{
        menu = ImageIO.read(new File("level skip.png"));
        arrow = ImageIO.read(new File("arrow2.png"));
      }catch(IOException ie) {
        System.out.println(ie.getMessage());
      }
      clear = true;
      paint(main.getGraphics());
    }
  
    public void paint (Graphics g){
      if(clear==true){
        g.drawImage(menu,0,0,null);
        clear = false;
      }
      g.drawImage(arrow,arrowPos,355,null);
    }
    public void keyPressed(KeyEvent e){
      String key = e.getKeyText(e.getKeyCode());
      if(key == "Left"){
        clear = true;
        if (arrowPos &gt; 109)
          arrowPos -= 260;
        else
          arrowPos = 629;
        paint(getGraphics());
      }
      if(key == "Right"){
        clear = true;
        if (arrowPos &lt; 629)
          arrowPos += 260;
        else
          arrowPos = 109;
        paint(getGraphics());
      }
      if(key == "Space"){
        switch(arrowPos){
          case 109:  System.out.println("ADD 1 TO LEVEL AND RUN BATTLE");
          break;
          case 369:  System.out.println("ADD 5 TO LEVEL AND RUN BATTLE");
          break;
          case 629:  System.out.println("ADD 10 TO LEVEL AND RUN BATTLE");
        }
      }
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
  
  }
  </code></pre>
  
  <p>I'm not exactly sure what the problem is, the Level Skip window displays fine, it just doesn't register any key presses.</p>