KeyAdapter侦听器适用于Windows,而不适用于Mac [英] KeyAdapter listener works in Windows, not on Mac
问题描述
我创建了一个简单的Nibbles(Snakes)游戏,一切都很完美。
I created a simple Nibbles (Snakes) game and everything works perfectly.
然而,当我将所有内容复制到我的mac(在PC上开发)并编译时相同的java文件,用于移动我的蛇的keyAdapter事件监听器没有响应。
However, when I copy everything over to my mac (develop on a PC) and compile the same java files, the keyAdapter event listener for moving my snake around doesn't respond.
我不确定是什么问题。
这里是片段,但如果有人愿意看一下整个节目,我可以私下发送。谢谢!
here's the snippet, but if anyone would be willing to take a look at the whole program, I could send that privately. THANKS!
getThis().addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_UP)
if(!glass.game.getDirection().equals("D"))
glass.game.setDirection("U");
if(event.getKeyCode() == KeyEvent.VK_DOWN)
if(!glass.game.getDirection().equals("U"))
glass.game.setDirection("D");
if(event.getKeyCode() == KeyEvent.VK_LEFT)
if(!glass.game.getDirection().equals("R"))
glass.game.setDirection("L");
if(event.getKeyCode() == KeyEvent.VK_RIGHT)
if(!glass.game.getDirection().equals("L"))
glass.game.setDirection("R");
}
});
推荐答案
使用 KeyBindings
而不是 KeyListener
, KeyListener
未指定用于侦听 KeyEvents
在Swing JComponents中,这是AWT组件的监听器,例如这里
use KeyBindings
instead of KeyListener
, KeyListener
isn't designated for listening KeyEvents
in the Swing JComponents, this's listener for AWT Components, example here
这篇关于KeyAdapter侦听器适用于Windows,而不适用于Mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!