我的问题有问题。我目前正在Java applet中使用KeyListener,当我键入一个键时没有任何问题(无显示)。这是代码:
package appl;
import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Appl extends Applet implements KeyListener {
@Override
public void keyTyped(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyCode());
}
@Override
public void keyPressed(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyChar());
}
@Override
public void keyReleased(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyChar());
}
/*
public static void main(String[] args) {
}
*/
}
答案 0 :(得分:1)
实施KeyListener
并不意味着您的程序正在使用它。您必须将其添加到您的小程序。
public class Appl extends Applet implements KeyListener {
@Override
public void keyTyped(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyCode());
}
@Override
public void keyPressed(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyChar());
}
@Override
public void keyReleased(KeyEvent ke) {
System.out.println("Pressed: " + ke.getKeyChar());
}
public void init() {
// YOUR CODE
addKeyListener(this);
}
}