JPanel上的KeyListener随机无响应

时间:2011-11-10 22:55:39

标签: java swing jpanel keylistener

我在项目中遇到默认的Java KeyListener问题。 我注意到,当我开始时,KeyListener似乎没有转发KeyEvents。

问题的症状: 启动应用程序时,不处理键输入。这有时只会发生。有时我必须关闭并启动应用程序7-8次,直到出现。有时这是第一次尝试。当它发生时,在我再次重新启动应用程序之前它将无法工作。

我正在使用的内容: Window 7 x64以及最新的Eclipse和JDK版本。

我发现了什么: 我在调试模式下放了一个断点并检查了JPanel实例。看起来KeyListener总是成功添加到它。 此外,MouseListener和MouseMotionListener始终工作得很好。

最小代码:

public class Player implements KeyListener
{
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){ }

    public void keyPressed(KeyEvent e){
        System.out.println("Key Pressed!");
    }

}

public class Game {

    public static void main(String[] args) {
        new Game();
    } 

    public Game(){
        JFrame window = new JFrame();
        window.setVisible(true);

        //Now set the window size correctly
        window.setSize(800, 600);  
        //Set-up the rest of the window
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        window.setResizable(true);


        //Create our panel
        JPanel canvas = new JPanel();
        canvas.setFocusable(true);
        window.add( canvas ); //Add it to our window

        Player k = new Player();
        canvas.addKeyListener(k);
    }
}

感谢您的时间!

PS: 好的,回答我自己的问题:

似乎我必须在设置窗口大小后调用setVisible(true)

    JFrame window = new JFrame();


    Now set the window size correctly
    window.setSize(800, 600);  
    window.setVisible(true);

像这样切换出setSize()和setVisible()似乎可以使它工作。试了十几次没有问题。

我想如果它的大小为0x0,setVisible可能不喜欢将Focus赋予窗口。 问题是:为什么这只会在某些情况下导致问题?

2 个答案:

答案 0 :(得分:2)

尝试将JButton添加到“canvas”JPanel,然后按下按钮并查看KeyListener会发生什么 - 它失败了,因为JPanel失去了焦点。要防止这种情况发生,请改用密钥绑定(请参阅上面评论中的链接以获取教程)。例如,

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Game2 {

   private static final String UP = "up";

   public static void main(String[] args) {
      new Game2();
   }

   public Game2() {
      JFrame window = new JFrame("Press up-arrow key");
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel canvas = new JPanel();
      canvas.setPreferredSize(new Dimension(400, 300));
      window.add(canvas);

      canvas.add(new JButton(new AbstractAction("Press space-bar") {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Button or space-bar pressed");
         }
      }));
      ActionMap actionMap = canvas.getActionMap();
      int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = canvas.getInputMap(condition);

      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
      actionMap.put(UP, new UpAction());

      window.pack();
      window.setLocationRelativeTo(null);
      window.setVisible(true);
   }
}

@SuppressWarnings("serial")
class UpAction extends AbstractAction {
   @Override
   public void actionPerformed(ActionEvent arg0) {
      System.out.println("Up Arrow pressed!");
   }
}

答案 1 :(得分:1)

不知道这是否与你的问题有关,但由于它的间歇性,也许是......你应该在最后一个和在swing线程中执行setVisible()。如果你愿意,你可以在setVisible之后调用setSize,但是用户可能会看到一个闪烁,同样应该在swing线程中完成。这是你的最后一步:

SwingUtilities.invokeLater( new Runnable() {
   public void run() {
      window.setVisible( true );
   }
} );

为此,您还需要进行最终窗口声明:

...
final JFrame window = new JFrame();
...