package test; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; public class test extends JFrame { private MyPanel1 myPanle; public static void main(String[] args) { test Test = new test(); } public test() { myPanle = new MyPanel1(); this.add(myPanle); this.addKeyListener(myPanle); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class MyPanel1 extends JPanel implements KeyListener{ @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { System.out.println(e.getKeyCode()); if (e.getKeyCode()==32){ System.out.println("空格"); } } @Override public void keyReleased(KeyEvent e) { } }