package 键盘事件;import java.awt.event.KeyEvent;import javax.swing.Jframe;public class Test extends Jframe{public static void main(String[] args) {Test test=new Test();test.loading();}int width=500;int height=250;public void loading(){setSize(width, height);setLocationRelativeTo(null); //关闭事件setDefaultCloseOperation(3); //用户不能调整窗口大小setResizable(false); //标题setTitle("键盘事件"); //窗口可见setVisible(true);this.addKeyListener(new demo());}}package 键盘事件;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.Jframe;public class demo implements KeyListener{@Overridepublic void keyPressed(KeyEvent e) {int key;key=e.getKeyCode();System.out.println(key);}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}}