2013-10-20 45 views
0

我真的很絕望。我已經嘗試了所有我能找到的提示,在互聯網上搜索了幾天,我仍然不明白爲什麼我的代碼無法工作。它只是不響應我的鍵盤輸入,沒有錯誤信息。keylistener無法正常工作

package drawLine; 

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.*; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class SnakeJPanel extends JPanel implements KeyListener { 
    private static final long serialVersionUID = 7526472295622776147L; 


JPanel panelForSnake, snake; 

final int WID = 10; 
final int HEI = 10; 
public static int x1 = 50; 
public static int y1 = 50; 
public static boolean right = true, left = false, down = false, up = false; 
static long millis =System.currentTimeMillis(); 
static long millisn =System.currentTimeMillis(); 


public class MyGraphics extends JComponent { 

    private static final long serialVersionUID = 1L; 

    MyGraphics() { 
     setPreferredSize(new Dimension(1000,700)); 
    } 


    public void paintComponent(Graphics g) { 
     super.paintComponents(g); 
     g.setColor(Color.red); 
     g.fillOval(x1, y1, WID, HEI); 
    } 
} 

public JPanel createContentPane(){ 
    JPanel totalGUI = new JPanel(); 
    totalGUI.setLayout(null); 


    panelForSnake = new JPanel(); 
    panelForSnake.setBackground(Color.black); 
    panelForSnake.setLocation(1,1); 
    panelForSnake.setSize(1000,700); 
    totalGUI.add(panelForSnake); 

    MyGraphics tr = new MyGraphics(); 
    tr.setLocation(1,50); 
    tr.setSize(1000,1000); 
    panelForSnake.add(tr); 

    return totalGUI; 

} 
private static void createAndShowGUI() { 

    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("Snake Harel"); 
    //Create and set up the content pane. 
    SnakeJPanel demo = new SnakeJPanel(); 
    frame.setContentPane(demo.createContentPane()); 

    // The other bits and pieces that make our program a bit more stable. 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(1000, 1000); 
    frame.setVisible(true); 


} 
public void keyPressed(KeyEvent e) { 
    requestFocusInWindow(); 
    requestFocus(); 
    addKeyListener((KeyListener) this); 
    setFocusable(true); 
    setFocusTraversalKeysEnabled(false); 
    int key = e.getKeyCode(); 

    if ((key == KeyEvent.VK_LEFT) && (!right)) { 
     left = true; 
     up = false; 
     down = false; 
    } 

    if ((key == KeyEvent.VK_RIGHT) && (!left)) { 
     right = true; 
     up = false; 
     down = false; 
    } 

    if ((key == KeyEvent.VK_UP) && (!down)) { 
     up = true; 
     right = false; 
     left = false; 
    } 

    if ((key == KeyEvent.VK_DOWN) && (!up)) { 
     down = true; 
     right = false; 
     left = false; 
    } 
} 
public static void move(boolean l,boolean r,boolean u,boolean d){ 
    if (r = true){ 
     millis =System.currentTimeMillis(); 
     millisn =System.currentTimeMillis(); 
      while (millisn<millis+20){ 
       millisn=System.currentTimeMillis(); 
      } 
     ++x1; 
    } 
    if (l = true){ 
     millis =System.currentTimeMillis(); 
     millisn =System.currentTimeMillis(); 
      while (millisn<millis+20){ 
       millisn=System.currentTimeMillis(); 
      } 
     --x1; 
    } 
    if (u = true){ 
     millis =System.currentTimeMillis(); 
     millisn =System.currentTimeMillis(); 
      while (millisn<millis+20){ 
       millisn=System.currentTimeMillis(); 
      } 
     ++y1; 
    } 
    if (d = true){ 
     millis =System.currentTimeMillis(); 
     millisn =System.currentTimeMillis(); 
      while (millisn<millis+20){ 
       millisn=System.currentTimeMillis(); 
      } 
     --y1; 
    } 
} 

public static void main(String[] args) { 
    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     move(left, right, up, down); 
     } 
    }); 
} 

public void keyReleased(KeyEvent e) {} 
public void keyTyped(KeyEvent e) {} 

}

回答

1

你添加的keyPressed方法裏面的KeyListener。但是,keyPressed方法在添加keyListener之前不會被調用。只需將其添加到主方法中或創建GUI時。

private static void createAndShowGUI() { 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("Snake Harel"); 
    //Create and set up the content pane. 
    SnakeJPanel demo = new SnakeJPanel(); 
    frame.setContentPane(demo.createContentPane()); 
    addKeyListener(this); 

    // The other bits and pieces that make our program a bit more stable. 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(1000, 1000); 
    frame.setVisible(true); 
} 

既然你不使用所有的KeyListener接口的我建議創建一個嵌套類,而不是和擴展KeyAdapter類。

public class ActionListener extends KeyAdapter { 
    public void keyPressed(KeyEvent e) { 
     int keyCode = e.getKeyCode(); 
     if ((key == KeyEvent.VK_LEFT) && (!right)) { 
       left = true; 
       up = false; 
       down = false; 
     } 
     if ((key == KeyEvent.VK_RIGHT) && (!left)) { 
      right = true; 
      up = false; 
      down = false; 
     } 
     if ((key == KeyEvent.VK_UP) && (!down)) { 
      up = true; 
      right = false; 
      left = false; 
     } 
     if ((key == KeyEvent.VK_DOWN) && (!up)) { 
      down = true; 
      right = false; 
      left = false; 
     } 
} 

然後,您將添加ActionListener類作爲關鍵偵聽器。

編輯:創建main方法的類的實例,

new SnakeJPanel(); 

然後創建一個構造函數,

private SnakeJPanel() { 
    addKeyListener(this); 
} 
+0

addKeyListener(this);無法添加到CreateAndShowGUI,它不能用於靜態類... – Atlantis

+0

創建類的實例,並在構造函數中添加鍵監聽器。編輯我的答案,檢查它的底部。 – Troubleshoot

+0

仍然不能工作:( – Atlantis

4

我已經嘗試了所有的技巧,我能找到,在互聯網上搜索天

那你爲什麼還在試圖使用KeyListener?您在論壇上找到的更好的解決方案將始終告訴您使用密鑰綁定。

請參閱Motion With the Keyboard關於使用KeyListner的常見問題和解決方案以及使用Key Bindings的工作示例,這是您應該使用的任何方法。