Java GUI 键盘事件

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Java键盘事件 {
	public static void main(String[] args) {
		Frame f = new Frame("KeyEvent");
		f.setLayout(new FlowLayout());
		f.setSize(400,300);
		f.setLocation(300, 200);
		TextField tf = new TextField(30);
		f.add(tf);
		f.setVisible(true);
		tf.addKeyListener(new KeyAdapter() {
			public void KeyPressed(KeyEvent e) {
				int keycode=e.getKeyCode();
				String s = KeyEvent.getKeyText(keycode);
				System.out.println("输入的内容为"+s);
				System.out.println("对应的KeyCode为"+keycode);
			}
		});
	}

}

posted @ 2019-08-13 19:30  南墙1  阅读(108)  评论(0编辑  收藏  举报