2
我的问题是,当我点击框架屏幕的空间时,它停止键盘按键被注册,所以我的播放器停止移动。JFrame鼠标单击停止键盘按钮
在此先感谢您的帮助。
代码:
private Component comp;
....
public InputManager(Component comp) {
this.comp = comp;
mouseLocation = new Point();
centerLocation = new Point();
// register key and mouse listeners
comp.addKeyListener(this);
comp.addMouseListener(this);
comp.addMouseMotionListener(this);
comp.addMouseWheelListener(this);
// allow input of the TAB key and other keys normally
// used for focus traversal
comp.setFocusTraversalKeysEnabled(false);
}
GUI代码:
Game game = new Game();
game.setMinimumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
game.setPreferredSize(new Dimension(WIDTH * 2, HEIGHT * 2));
game.setMaximumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
+1键绑定。停止使用+1作为侦听器,使用+1作为混音备注。嗯,我需要upvote两个其他职位给你你的信用? – Robin 2012-08-03 22:07:18
谢谢,这帮助了很多。我一定会尝试关键绑定。 – DCSoft 2012-08-03 22:34:25
我找到了一个临时解决方案,我已经将游戏类传入InputManager类而不是JFrame。 – DCSoft 2012-08-03 22:50:13