键盘监听
package com.zeng.GUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TextKeyListener {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame {
public MyKeyFrame() {
setBounds(1, 2, 300, 500);
setVisible(true);
addKeyListener(new MyKeyListener());
}
}
class MyKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println("keyCode = " + keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println(" 你按下了上键");
}
}
}