import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JFrame;import javax.swing.JLabel;public class Demo extends JFrame {public Demo() {JLabel l = new JLabel("没有点击键盘Q键");// 创建标签add(l);// 添加标签l.addKeyListener(new KeyListener() {// 为标签添加键盘事件监听@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Q) {// 如果按下的是Q键
l.setText("点击了Q键");// 更改标签的值
}
}
});
setSize(200, 200);// 设置大小
setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭窗口退出程序
setVisible(true);// 窗体可以显示
public static void main(String[] args) {new Demo();}}这里按Q了,它不显示,怎么办????