title: I'm having issues with CardLayout and one of the JPanels not receiving Keyboard events
body: <p>I will only supply the relevant parts of the code as it is quite large. Help me figure out why I never get keyboard events to the GameBoard JPanel. The game code runs just fine until I added the new feature where I wanted it to have a welcome screen.</p>
  <p><strong>Edit:</strong> If you would like to view the entire project (it's an eclipse project), here is the link to download it <a href="https://www107.zippyshare.com/v/j8cWhwJQ/file.html" rel="nofollow noreferrer">Eclipse project</a>, I honestly can't format almost 1000 lines of code here on StackOverflow, that'll be painful even for you.</p>
  <p>Main.java</p>
  <pre><code>private void initUI() throws IOException {
      cardLayout = new CardLayout();
      mainPanel = new JPanel(cardLayout);
      
      welcomeMenu = new Welcome(cardLayout, mainPanel);
      mainPanel.add(welcomeMenu, &quot;welcome&quot;);
      game = new GameBoard();
      mainPanel.add(game, &quot;game&quot;);
  
      add(mainPanel);        
      setTitle(&quot;Pacman&quot;);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setSize(380, 420);
      setLocationRelativeTo(null);
  }
  </code></pre>
  <p>This is the welcome/menu panel (some section of it).
  Welcome.java (servers as a menu)</p>
  <pre><code>@Override
  public void actionPerformed(ActionEvent evt) {
      if (evt.getActionCommand() == Actions.EXIT.name()) {
          System.exit(0);
      } else if (evt.getActionCommand() == Actions.CONFIG.name()) {
          JOptionPane.showMessageDialog(null, &quot;Not yet implemented&quot;);
      } else if (evt.getActionCommand() == Actions.PLAY.name()) {
          // The part that switches to game, works as expected.
          cl.show(mp, &quot;game&quot;);
      }
  }
  </code></pre>
  <p>GameBoard class has a KeyAdapter class within it that is used to listen to key events, however, in the addition of the new feature using CardLayout, I could not get the key events to the panel so my PacMan is just stuck in one place opening and closing its mouth like a fish.</p>
  <p>GameBoard.java</p>
  <pre><code>private void initBoard() {
  
      addKeyListener(new TAdapter());
  
      setFocusable(true);
  
      setBackground(Color.black);
  
      initGame();
  }
  ...
  class TAdapter extends KeyAdapter {
      
      @Override
      public void keyPressed(KeyEvent e) {
  
          int key = e.getKeyCode();
  
          if (inGame) {
              if (key == KeyEvent.VK_LEFT) {
                  req_dx = -1;
                  req_dy = 0;
              } else if (key == KeyEvent.VK_RIGHT) {
                  req_dx = 1;
                  req_dy = 0;
              } else if (key == KeyEvent.VK_UP) {
                  req_dx = 0;
                  req_dy = -1;
              } else if (key == KeyEvent.VK_DOWN) {
                  req_dx = 0;
                  req_dy = 1;
              } else if (key == KeyEvent.VK_ESCAPE &amp;&amp; timer.isRunning()) {
                  inGame = false;
              } else if (key == KeyEvent.VK_PAUSE) {
                  if (timer.isRunning()) {
                      timer.stop();
                  } else {
                      timer.start();
                  }
              }
          } /*
               * else { if (key == 's' || key == 'S') { inGame = true; initGame(); } }
               */
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
  
          int key = e.getKeyCode();
  
          if (key == Event.LEFT || key == Event.RIGHT || key == Event.UP || key == Event.DOWN) {
              req_dx = 0;
              req_dy = 0;
          }
      }
  }
  </code></pre>