注册 登录
编程论坛 JAVA论坛

KeyListener什么时候会失效啊??

流浪小白 发布于 2017-03-02 20:31, 1047 次点击
import java.awt.*;
import java.awt.event.*;

public class TextKey {
    public static void main(String[] args) {
        new KFrame().lauchFrame();
    }
}

class KFrame extends Frame {
    public void lauchFrame() {
        setLayout(new FlowLayout());
        Button b = new Button("请点一下我!");
        Label l = new Label("请观看");
        setBounds(100,100,300,400);
        add(b);
        add(l);
        b.addActionListener(new ActionMonitor());
        this.addKeyListener(new KeyMonitor());
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                setVisible(false);
            }
        });
        setVisible(true);
    }
   
    class ActionMonitor implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println("谢谢你来点我!");
        }
    }
   
    class KeyMonitor extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keycode = e.getKeyCode();
            if(keycode == KeyEvent.VK_UP) {
                System.out.println("再见!");
            }
            //System.out.println("再见!");
        }
    }
}  为什么我这个KeyListener触发不了?
0 回复
1