返回信息流就简单的建一个窗口,里面new一个jpanel,在jpanel上设置一个键盘监听,如果键盘按下了方向键“上”,就在控制台窗口输出一行“up”,现在程序按下键盘后没有反应,求帮助
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestKey extends JFrame {
TestKey() {
this.setBounds(600, 400, 800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
PanelEditor pe = new PanelEditor();
pe.setBounds(0, 0, 800, 600);
this.add(pe);
}
public static void main(String[] args) {
new TestKey();
}
}
class PanelEditor extends JPanel {
PanelEditor() {
MonitorKey mk = new MonitorKey();
this.addKeyListener(mk);
this.setFocusable(true);
}
}
class MonitorKey extends KeyAdapter {
public void keyPressed(KeyEvent arg0) {
int key_code = arg0.getKeyCode();
if(key_code == KeyEvent.VK_UP){
System.out.println("UP");
}
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #29590同步于 2014/4/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
【求助】急!键盘监听器没有反应
lzj0218
2014/4/25镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复