title: How can i add a keylistener to my Gui in java
body: <p>I have a simple Gui</p>
  
  <pre><code>public Gui(){
      ablak = new JFrame("Snake game");
      ablak.setVisible(true);
      ablak.setSize(new Dimension(600,600));
      ablak.setFocusable(true);
      ablak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ablak.add(new Grid());
      ablak.add(new Key());
  }
  </code></pre>
  
  <p>And a key class to listner to keyevents</p>
  
  <pre><code>package snake;
  
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  public class Key implements KeyListener {
  
      @Override
      public void keyPressed(KeyEvent e) {
          if(e.getKeyCode() == KeyEvent.VK_UP){
              System.out.println("Hi");
          }
      }
  
      @Override
      public void keyTyped(KeyEvent e) {
          throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
          throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
      }
  
  
  }
  </code></pre>
  
  <p>As you can see pressing the Up arrow key should say something like Hi, but nothing happens.
  If i try to add(new Key()) in Gui class it gives an error.
  What am i doing wrong?</p>
  
  <p>Thank you</p>