actx23a
2018-09-16 16:09
采纳率: 60.5%
浏览 1.7k
已采纳

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条回答 默认 最新

  • 小小青龙儿 2018-09-18 06:49
    已采纳

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

    点赞 打赏 评论
  • dabocaiqq 2018-09-16 16:11
     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);
                  }
           }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题