2012-10-06 38 views
1

我建立的一個使用KeyListenerActionListener移動的盒子,這取決於按下 箭頭鍵(類似蛇)方向,在JFrame一個簡單的動畫節目。但我注意到,如果我啓動應用程序並移動鼠標,應用程序將不會繼續檢查按下哪個箭頭鍵以及要移動到哪個方向。的MouseListener和的keyPressed

有人可以向我解釋這是否是因爲我需要禁用某些涉及鼠標事件?

繼承人的代碼:

public class gui extends JPanel implements ActionListener, KeyListener{ 
    Timer tm = new Timer(5, this); 
    int x = 300, y = 178, velx = 0, vely = 0; 


    public gui() { 
     tm.start(); 
     addKeyListener(this); 
     setFocusable(true); 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(x, y, 50, 30); 

    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     keys(e); 
    } 

    public void keys(KeyEvent e) { 
     int c = e.getKeyCode(); 
     if (c == KeyEvent.VK_LEFT) 

     { 
      velx = -1; 
      vely = 0; 
     } 
     if (c == KeyEvent.VK_UP) 

     { 
      velx = 0; 
      vely = -1; 
     } 
     if (c == KeyEvent.VK_RIGHT) 

     { 
      velx = 1; 
      vely = 0; 
     } 
     if (c == KeyEvent.VK_DOWN) 

     { 
      velx = 0; 
      vely = 1; 

     } 
    } 

    public void borders(ActionEvent e) { 
     if (x < 0) { 
      velx = 0; 
      x = 0; 
      JOptionPane 
        .showMessageDialog(null, "you hit the borders you lost!"); 
      System.exit(0); 
     } 
     if (x > 530) { 
      velx = 0; 
      x = 530; 
      JOptionPane 
        .showMessageDialog(null, "you hit the borders you lost!"); 
      System.exit(0); 
     } 
     if (y < 0) { 
      velx = 0; 
      y = 0; 
      JOptionPane 
        .showMessageDialog(null, "you hit the borders you lost!"); 
      System.exit(0); 
     } 
     if (y > 330) { 
      velx = 0; 
      y = 330; 
      JOptionPane 
        .showMessageDialog(null, "you hit the borders you lost!"); 
      System.exit(0); 

     } 

    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 


    public void keyTyped(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 


    public void actionPerformed(ActionEvent e) { 
     x += velx; 
     y += vely; 
     repaint(); 
     borders(e); 

    } 

}

+2

不是沒有看到任何代碼,最好作爲SSCCE :-)順便說一句:不要使用KeyListeners,而是使用KeyBindings。 – kleopatra

+1

這是[示例](http://stackoverflow.com/a/5797965/230513)。 – trashgod

+0

確定繼承人的代碼,我希望它可以幫助您找到答案: – david1597

回答

0

我不知道你的問題是什麼。我用你的代碼,沒有問題。

這是我使用的:

 public gui() 
     { 
      addKeyListener(this); 
      setFocusable(true); 
     } 

     public void keyPressed(KeyEvent e) 
     { 
      int c = e.getKeyCode(); 
      if (c == KeyEvent.VK_LEFT) 
      { 
       velx = -1; 
       vely = 0; 
      } 
      if (c == KeyEvent.VK_UP) 
      { 
       velx = 0; 
       vely = -1; 
      } 
      if (c == KeyEvent.VK_RIGHT) 
      { 
       velx = 1; 
       vely = 0; 
      } 
      if (c == KeyEvent.VK_DOWN) 
      { 
       velx = 0; 
       vely = 1; 
      } 
     } 

     public void actionPerformed(ActionEvent e) 
     { 
      x += velx; 
      y += vely; 
      repaint(); 
      borders(e); 
     } 

     public static void main(String[] args) 
     { 
      JFrame frame = new JFrame("gui"); 
      frame.add(new gui()); 
      frame.setVisible(true); 
      frame.setSize(600, 400); 
     } 
    } 

我點擊鼠標和移動它之前,我啓動該程序,然後我用的鑰匙;它工作正常。但我會切換到KeyBindings。