KeyListener无法正常工作?

时间:2012-06-26 16:07:11

标签: java swing jframe keylistener focusable

好的,所以我为我的游戏提供了这个关键的监听器并且它无法正常工作但是当我创建一个新项目时只是为了测试它对我来说是否有效呢?所以这里是我游戏的一些代码:

public class GameKeyListener implements KeyListener {

private void pressMainWeapon() {
     Constants.MAIN_WEAPON_PRESSED = Constants.MAIN_WEAPON_PRESSED ? true : false;
    }

    private void pressSecondaryWeapon() {
     Constants.SECONDARY_WEAPON_PRESSED = Constants.SECONDARY_WEAPON_PRESSED ? true : false;
    }

    private void pressSkillsButton() {
        Constants.SKILLS_BUTTON_PRESSED = Constants.SKILLS_BUTTON_PRESSED ? true : false;
    }

       private void gameKeyPressed(int keyCode) {

              switch (keyCode) {      

                 case KeyEvent.VK_Q:
                     pressMainWeapon();
                    break;

                 case KeyEvent.VK_E:
                     pressSecondaryWeapon();
                     break;

                 case KeyEvent.VK_TAB:
                     pressSkillsButton();
                     break;

                 case KeyEvent.VK_W:
                     Constants.CHARACTER_Y -= 5;
                     break;

                 case KeyEvent.VK_A:
                     Constants.CHARACTER_X -= 5;
                     break;

                 case KeyEvent.VK_S:
                     Constants.CHARACTER_Y += 5;
                     break;

                 case KeyEvent.VK_D:
                     Constants.CHARACTER_X += 5;
                     break;

              }
           }

    private void gameKeyReleased(int keyCode) {  

    }

    private void gameKeyTyped(char keyChar) {  

    }

    @Override
    public void keyPressed(KeyEvent e) {
        gameKeyPressed(e.getKeyCode());
        System.out.println(e.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        gameKeyReleased(e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        gameKeyTyped(e.getKeyChar());
    }

public class GraphicsMain {

static JFrame f = new JFrame("Gurillia");

private static GameCanvas gc = new GameCanvas();

public static void startGraphics() throws IOException {
    f.setVisible(true);     
    f.setIgnoreRepaint(true);

    f.setContentPane(gc);
    gc.setPreferredSize(new Dimension(Constants.GAME_WIDTH, Constants.GAME_HEIGHT));        

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setResizable(false);

    f.addKeyListener(new GameKeyListener());
}

但这确实有效:

public class testkeylistener implements KeyListener {

@Override
public void keyPressed(KeyEvent arg0) {
    System.out.println(arg0.getKeyChar());
}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public static void main(String args[]) {
    JFrame f = new JFrame("test");
    f.setSize(1000,1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.pack();
    f.addKeyListener(new testkeylistener());
}

那么问题是什么?

3 个答案:

答案 0 :(得分:5)

问题是您使用KeyListener时,Swing旨在使用KeyBindings。差异可能是由于不同的焦点行为造成的。

例如,请参见this question,其中KeyListener偶尔只会工作

答案 1 :(得分:2)

应将KeyListener添加到画布,并添加到setFocusable,因为需要Focus

答案 2 :(得分:2)

尝试将侦听器添加到子(canvas),而不是JFrame本身。 键是在当前焦点的元素上触发的。

JFrames几乎没有焦点。