JAVA键盘事件。。。。。。。。。。

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() {// 为标签添加键盘事件监听
        @Override
        public 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了,它不显示,怎么办????

2个回答

刚才好像看到过一个了,监听时间加在JFrame上
l.addKeyListener(new KeyListener()改成
this.addKeyListener(new KeyListener()

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyEventDemo extends JFrame{
        private KeyboardPanel keyboardPanel = new KeyboardPanel();

        public KeyEventDemo(){
              add( keyboardPanel);

               keyboardPanel.setFocusable(true);

       }

        public static void main(String[] args){
              KeyEventDemo frame = new KeyEventDemo();
              frame.setTitle( "KeyEventDemo");
              frame.setSize(300,300);
              frame.setLocationRelativeTo( null);
              frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
              frame.setVisible( true);

       }

        static class KeyboardPanel extends JPanel{
               private int x = 100;
               private int y = 100;
               private char KeyChar = 'A' ;

               public KeyboardPanel(){
                     addKeyListener( new KeyAdapter(){
                            public void keyPressed(KeyEvent e){
                                   switch(e.getKeyCode()){
                                   case KeyEvent.VK_DOWN: y += 10;break;
                                   case KeyEvent.VK_UP: y -= 10; break;
                                   case keyEvent.VK_LEFT: x -= 10; break;
                                   case KeyEvent.VK_RIGHT: x += 10; break;
                                   default: keyChar = e.getKeyChar();
                                  }

                                  repaint();
                           }
                     });
              }

               protected void paintComponent(Graphics g){
                      super.paintComponent(g);

                     g.setFont( new Font("TimesRoman" ,Font.PLAIN,24));
                     g.drawString(String. valueOf(KeyChar), x, y);
              }
       }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐