键盘监听事件
package com.zhang.Study.事件监听.键盘监听事件; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Application { public static void main(String[] args) { new MyFrame(); } } class MyFrame extends Frame{ public MyFrame(){ setBounds(400,400,400,400); setVisible(true); //这里用匿名内部类不用定义类的名字,比较方便 this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int keycode =e.getKeyCode(); if(keycode==KeyEvent.VK_UP){//VK_UP对应的按键值是38 System.out.println(keycode);//输出按键对应的值,每一个按键都有一个对应的值 System.out.println("你按下了上键"); } } }); } }