KeyListener什么时候会失效啊??
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触发不了?