我的问题是当我单击框架屏幕的空间时,它会停止注册键盘键,因此我的播放器停止移动。
在此先感谢您的帮助。
编码:
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);
}
图形用户界面代码:
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();