我的程序是类似金山打字通的打苹果键盘游戏,我是让JFrame  addKeyboardListener的,刚打开程序能成功监听
但是每当我按下了一个button后,JFrame就失去焦点了,我试了在button的事件监听中调用jFrame.requestFocusInWindow();也不行
有什么方法能使我操作完button后,使JFrame重新获回焦点呢?
我查了API,好像 setGlobalPermanentFocusOwner,这个“设置持久焦点所有者”最适合的,但我不知道怎么用,这个函数括号里应该输入些什么内容?可以举个完整的使用此函数的例子吗?感谢各位
protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner)

解决方案 »

  1.   

    protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner)This method does not actually set the focus to the specified Component.
    Use Component.requestFocus() or Component.requestFocusInWindow() to change the focus owner, subject to platform limitations
      

  2.   

    You know , I had  used that way before asking the question here ,which is using Component.requestFocus() or Component.requestFocusInWindow(), but unfortunately,it could not be worked, 
    however,thank you all the same.
      

  3.   

    addFocusListener失去焦点后再重新设定,不知可行不。
      

  4.   

    可以给button添加一个和Frame一样的监听器,一样的handler。不知这样可否?
      

  5.   


    我觉得应该是it did not work,而不是it could not be worked!
      

  6.   

    在buttong监听器的最后,把焦点重设到frame上,可行否?
      

  7.   

    现在可以了,原来我之前,在button监听事件中,把焦点重新设到panel上,没有弄到Jframe,原来自己弄错了,
    都怪自己粗心
    感谢各位的热心回答,#6楼的Mybeautiful,说法正确
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==button){
    frame.requestFocusInWindow();
      

  8.   

    这个我之前试过,addFocusListener提示说此方法对JFrame不可用,不知为什么
      

  9.   

    我也是楼主,我换个帐号(因为连续只能回复三次)
    这个方法也真的可以啊,看来有不少方法能实现呢,CSDN真强大,也可能我太菜了,呵呵