2014-02-16 159 views
0

所以我正在實現鍵盤輸入來移動一個精靈,但我遇到了麻煩,因爲由於某種未知的原因,我無法獲得鍵盤輸入來註冊。我已經能夠追蹤我的問題,因爲它沒有調用TAdapter方法,但我不知道爲什麼。關鍵事件監聽器不工作

這裏是我的代碼:

public class Game extends JPanel implements ActionListener 
{ 
    private static final long serialVersionUID = 1L; 

    private static Action upKeyAction; 

    //Frame Width and Height 
    static int WIDTH = 600; 
    static int HEIGHT = 400; 

    Timer timer; 
    Magnus magnus; 

    //Get Background 
    BufferedImage background = null;{ 
     try { 
      background = ImageIO.read(new URL("http://i.imgur.com/YXlnj1g.png")); 
      System.out.println("Gotten Background"); 
     } catch (IOException e) { 
      System.out.println("Incorrect Background"); 
     }} 

    public Game() 
    { 

     addKeyListener(new TAdapter()); 
     setFocusable(true); 
     setDoubleBuffered(true); 
     requestFocusInWindow(); 

     magnus = new Magnus(); 

     timer = new Timer(5, this); 
     timer.start(); 

     System.out.println("Game Intialized"); 
    } 

    public void paint(Graphics g) 
    { 
     super.paint(g); 
     //System.out.println("Graphics Painted"); 
     g.drawImage(background, 0, 0, null); 
     g.drawImage(Magnus.magnus, magnus.getX(), magnus.getY(), this); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     magnus.move(); 
     repaint(); 
    } 

    private class TAdapter extends KeyAdapter 
    { 
     public void keyReleased(KeyEvent e) 
     { 
      magnus.keyReleased(e); 
     } 

     public void keyPressed(KeyEvent e) 
     { 
      magnus.keyPressed(e); 
      System.out.println("Key Pressed"); 
     } 
    } 

什麼當按下按鍵應該發生:

public class Magnus 
{ 
    private int dx; 
    private int dy; 
    private int x; 
    private int y; 

    public static BufferedImage magnus; 

    public Magnus() 
    { 
     try { 
      magnus = ImageIO.read(new URL("http://i.imgur.com/eoz7j06.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("Gotten Magnus"); 

     //Set Position 
     x = 40; 
     y = 60; 
    } 

    public void move() 
    { 
     x += dx; 
     y += dy; 
    } 

    public int getX() 
    { 
     return x; 
    } 

    public int getY() 
    { 
     return y; 
    } 

    public BufferedImage getImage() 
    { 
     return magnus; 
    } 

    public void keyPressed(KeyEvent e) 
    { 
     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_LEFT) 
     { 
      dx = -1; 
      System.out.println("Left button pressed"); 
     } 

     if (key == KeyEvent.VK_RIGHT) 
     { 
      dx = 1; 
      System.out.println("Right button pressed"); 
     } 

     if (key == KeyEvent.VK_UP) 
     { 
      dy = -1; 
      System.out.println("Up button pressed"); 
     } 

     if (key == KeyEvent.VK_DOWN) 
     { 
      dy = 1; 
      System.out.println("Down button pressed"); 
     } 
    } 

    public void keyReleased(KeyEvent e) 
    { 
     System.out.print("Key event initialized"); 
     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_LEFT) 
     { 
      dx = 0; 
     } 

     if (key == KeyEvent.VK_RIGHT) 
     { 
      dx = 0; 
     } 

     if (key == KeyEvent.VK_UP) 
     { 
      dy = 0; 
     } 

     if (key == KeyEvent.VK_DOWN) 
     { 
      dy = 0; 
     } 
    } 
} 
+0

你得到任何錯誤或其他。該代碼適用於我。告訴我究竟是什麼問題。 – Aarav

回答

0

嘗試的implements KeyListener代替implements ActionListener

+0

'ActionListener'包含'KeyListener'。沒有必要。這就是說,使用'KeyListener'將會提高性能。 –