java例程练习(键盘事件)

import java.awt.*;
import java.awt.event.*;
public class TestKeyEvent {
    public static void main(String[] args) {
		new KeyFrame().launchFrame();
    }
}

class KeyFrame extends Frame {
	public void launchFrame() {
		setSize(200,200);
		setLocation(200,200);
		this.addKeyListener(new MyKeyMonitor());
		this.setVisible(true);
	}

	class MyKeyMonitor extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			if(keyCode == KeyEvent.VK_UP) {
				System.out.println("UP");
			}
		}
	}
}

你可能感兴趣的:(java)