gpt4 book ai didi

java - 键监听器和键绑定(bind)并非每次编译都有效

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:57 24 4
gpt4 key购买 nike

基本上我已经创建了一个连接到我的 JPanel 的关键监听器..有时当我编译并运行它时它确实工作得很好并且没有任何问题..但有时当我编译时它不会工作。它无法识别按键。

这是我在 JPanels 更新方法中用于关键监听器的代码:

if(keyListener.getMovingRight() == KeyEvent.VK_RIGHT){
           player.playerMoveRight();
       }
       if(keyListener.getMovingLeft() == KeyEvent.VK_LEFT){
           player.playerMoveLeft();
       }
       if(keyListener.getMovingUp() == KeyEvent.VK_UP){
           player.playerMoveUp();
       }
       if(keyListener.getMovingDown() == KeyEvent.VK_DOWN){
        player.playerMoveDown();
       }

上面的代码知道是否从此类按下了按键:

public class KeyBoard implements KeyListener {

   private int playerMoveUp;
   private int playerMoveDown;
   private int playerMoveLeft;
   private int playerMoveRight;

   public KeyBoard(){
       playerMoveUp = 0;
       playerMoveDown = 0;
       playerMoveLeft = 0;
       playerMoveRight = 0;
   }

   @Override
   public void keyTyped(KeyEvent keyEvent) {
   }

   @Override
   public void keyPressed(KeyEvent keyEvent) {

       //Switch statement to get which keys were pressed
       switch(keyEvent.getKeyCode()){

           case KeyEvent.VK_UP:
               playerMoveUp = keyEvent.getKeyCode();
               break;

           case KeyEvent.VK_DOWN:
               playerMoveDown = keyEvent.getKeyCode();
               break;

           case KeyEvent.VK_LEFT:
               playerMoveLeft = keyEvent.getKeyCode();
               break;

           case KeyEvent.VK_RIGHT:
               playerMoveRight = keyEvent.getKeyCode();
               break;
       }
   }

   @Override
   public void keyReleased(KeyEvent keyEvent) {

       //Switch statement to get which keys were released
       switch(keyEvent.getKeyCode()){

           case KeyEvent.VK_UP:
               playerMoveUp = 0;
               break;

           case KeyEvent.VK_DOWN:
               playerMoveDown = 0;
               break;

           case KeyEvent.VK_LEFT:
               playerMoveLeft = 0;
               break;

           case KeyEvent.VK_RIGHT:
               playerMoveRight = 0;
               break;
       }
   }

   public int getMovingUp(){
       return playerMoveUp;
   }
   public int getMovingDown(){
       return playerMoveDown;
   }
   public int getMovingLeft(){
       return playerMoveLeft;
   }
   public int getMovingRight(){
       return playerMoveRight;
   }
}

我当然通过这样做将关键监听器添加到 JPanel:

panel.addKeyListener(KeyBoardClasshere);

由于它不能正常工作,我尝试了其他称为“Key Bindigs”的东西,因为我听说这会增加我的机会..

我将其添加到 JPanel 类中,并在更新方法中注释掉了关键监听器:

   

public void keyBindingsInitialize(ActionMap am, InputMap im){
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Right");
am.put("Right", RightBind);

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Left");
am.put("Left", LeftBind);

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Up");
am.put("Up", UpBind);

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Down");
am.put("Down", DownBind);
   }
   
   Action RightBind = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
player.playerMoveRight();
}
   };
   Action LeftBind = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
player.playerMoveLeft();
}
   };
   Action UpBind = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
player.playerMoveUp();
}
   };
   Action DownBind = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
player.playerMoveDown();
}
   };

这也有效,但有时当我编译并运行项目时,它不起作用,然后如果我终止项目然后再次运行它,它就会起作用。在我看来,这太奇怪了。有人有建议吗?

编辑:我按照这个问题的答案:KeyListener on JPanel randomly unresponsive

我确实通过单击出现的按钮和空格键得到了响应,但没有得到更新方法中使用的键。现在,在我添加按钮后,有时当我运行应用程序时,我得到的只是一个灰色窗口,必须重新启动几次才能使其再次正常,就像我链接的问题一样。

最佳答案

尝试添加

setFocusable(true);

到 JPanel 的构造函数。

关于java - 键监听器和键绑定(bind)并非每次编译都有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16367195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com