title: How can I switch focus to a canvas?
body: <p>I have this class and I want to switch focus to the Game class right after it was invoked. I might've not understand the purpose of focus but when I press start I have to click on the game canvas itself so I can use the keyboard . In other words: How can I make it so I don't have to click on it to use the keyboard?</p>
  
  <pre><code>package com.runner.panels;
  
  import java.awt.CardLayout;
  import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JPanel;
  
  import com.runner.main.Game;
  import com.runner.main.Main;
  
  public class PlayPanel extends JPanel{
  
      private static final long serialVersionUID = 1L;
  
      public PlayPanel(){
          //setting the layout of the playpanel to null
          setLayout(null);
  
          //setting up the info panel : high score, meters ran, pause button etc...
          JPanel info = new JPanel();
          info.setBounds(0,0,1200,50);
          add(info);
  
          //back button
          JButton back = new JButton("Back");
          info.add(back);
  
          Game game = new Game();
          game.setBounds(0,50,1200,521);
          game.setBackground(Color.black);
          add(game);
  
          back.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e) {
                  CardLayout cl = (CardLayout) Main.mainp.getLayout();
                  cl.show(Main.mainp, "Menu");
              }
          });
      }
  }
  </code></pre>