给一个JPanel添加一个键盘事件!怎么添加

解决方案 »

  1.   

    public class MyFrame extends JFrame {
    public static void main(String[] args) {
    new MyFrame();
    } public MyFrame() {
    super("hello");
    this.setSize(300, 400);
    this.setVisible(true);
    this.setBounds(200,200,400,400);
    JPanel jp = new JPanel();
    jp.setSize(200,200);
    jp.setBackground(Color.GREEN);
    jp.addKeyListener(new KeyAdapter (){

    public void keyPressed(KeyEvent ke) {

    System.out.println("@@");
    }
    public void keykeyReleased(KeyEvent ke) {

    System.out.println("@@");
    }
    });
    jp.setFocusable(true);
    this.getContentPane().add(jp);
    }
    }