基本上我已经创建了一个连接到我的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类中添加了这个,并在update方法中注释掉了键侦听器:
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();
}
};
这也有效,但有时当我编译并运行项目时它只是不工作然后它工作,如果我终止项目,然后再次运行..它太陌生在我的意见..任何人有建议吗?
编辑: 我在这个问题上关注了answear:KeyListener on JPanel randomly unresponsive
我确实通过点击apears按钮和空格键而不是更新方法中使用的按键得到响应。现在我添加按钮后,有时当我运行应用程序时,我得到的是一个灰色的窗口,必须重新启动它几次以使其恢复正常,就像我链接的问题一样。
答案 0 :(得分:1)
尝试添加
setFocusable(true);
到你的JPanel的构造函数。
答案 1 :(得分:0)
通过在游戏循环中添加frame.revalidate();
来解决此问题。
感谢所有的回复!