title: need help taking my key press listeners out of main
body: <p>I have started to create a game where you can move freely. you move by using your keyboard I have it working but all I want to do is to take it out of main so I can call if multiply times from different classes. I have tried to put it in a <em>public static void</em> and call that method from main but it doesn't work.
  my code is provided below. I have been stuck on this for a long time know. </p>
  
  <p>I have a picture in the frame to make it visible the picture is named 1.</p>
  
  <pre><code>import java.awt.event.KeyEvent;
  import java.util.Timer;
  import java.util.TimerTask;
  import javax.swing.*;
  public class StartGame {
      public static void main(String[] argv) throws Exception {
          JFrame MainFrame = new JFrame();
          MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          MainFrame.setSize(1210, 700);
          MainFrame.setLocation(new java.awt.Point(150, 30));
          MainFrame.setLayout(null);
          MainFrame.setFocusable(true);
          MainFrame.setFocusTraversalKeysEnabled(true);
          MainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
          JLabel thing = new JLabel();
          thing.setIcon(new ImageIcon("image\\1.gif"));
          thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);
  
          MainFrame.addKeyListener(new java.awt.event.KeyListener() {
              public void keyPressed(KeyEvent e) {
                  if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                      if (0 &lt; thing.getY()) { 
                          thing.setLocation(thing.getX(), thing.getY() - 10);
                      }
                  }
                  if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
                      if (thing.getY() &lt; 570) {
                          thing.setLocation(thing.getX(), thing.getY() + 10);
                      }
                  }
                  if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
                      if (thing.getX() &lt; 1110) {
                          thing.setLocation(thing.getX()+ 10, thing.getY());
                      }
                  }
                  if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {        
                      if (0 &lt; thing.getX()) {
                          thing.setLocation(thing.getX() - 10, thing.getY());
                      }
                  }
  
              }
  
              public void keyTyped(KeyEvent e) {
  
              }
  
              public void keyReleased(KeyEvent e) {
  
  
              }
          });
  
          MainFrame.add(thing);
          MainFrame.setVisible(true);
      }
  }
  </code></pre>