我一直在玩Java,並且添加了一個KeyListener。當我輸入一個鍵時,它打印「0」,我希望它打印鍵碼。Java AWT KeyListener不能正常工作
Key.java
import java.awt.event.*;
public class Key implements KeyListener {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
System.out.println("TYPED: " + Integer.toString(e.getKeyCode()));
}
}
Main.java
public void init() {
addKeyListener(new Key());
addMouseListener(new Mouse());
this.setBackground(new Color(100, 100, 255));
this.setSize(screen);
}
感謝所有幫助!
1)爲什麼要編寫一個小程序?如果由於規格而到期。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)對於Swing,通常使用基於AWT的較低級別KeyListener的鍵綁定。有關如何使用它們的詳細信息,請參見[如何使用鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)。這是基於Swing還是AWT? –