我正在尝试设置一个 KeyListener 来触发 KeyPressed 和 KeyReleased。
当我按住一个键时,如果在按下后立即触发则释放,例如,如果我按住一个键,我会从每个事件的打印语句中获得以下输出:
Pressed
Released
Pressed
Released
Pressed
Released
Pressed
Released
这是我的 KeyListener
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class EventListener extends JFrame implements KeyListener {
public EventListener() {
super();
setSize(new Dimension(200, 300));
addKeyListener(this);
setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed");
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("Released");
}
}
有谁知道发生了什么?