这个JPanel是用来画图的,上面没有任何有焦点的控件。
如何获得键盘事件?panel.addKeyListener(new KeyAdapter(){
  public void keyTyped(KeyEvent e){
    ......
  }
}
);
不行,处理方法根本不被调用参考
http://expert.csdn.net/Expert/topic/1238/1238484.xml?temp=.617901
两贴一起结算

解决方案 »

  1.   

    在Swing里面,JPanel根本实现KeyListener接口,所以上面的方法是徒劳的。
    要么换用其他的如JTextPane等组件,都可以画图。要么自己实现。
      

  2.   

    button.addKeyListener(JPanel panel)
    用panel监听也不行?
      

  3.   

    呵呵~~不好意思错了,我以为是panel监听button呢或许楼上说的是对的
      

  4.   

    一般JPanel都会被加到一个JFrame里面,所以建议给你的JFrame添加KeyAdapter
    这个JFrame拥有JPanel的引用是很容易,同样可以对JPANEL进行各种操作
      

  5.   

    JPanel has more Methods inherited from class javax.swing.JComponent
    so it has the Methods of requestFocus(),this will let the JPanel get the focus.