addKeylistener()无法正常工作 [英] addKeylistener() not working
问题描述
对于学校来说,我必须制作一款基于Breakout的小游戏.
For school I have to make a small game which is based on Breakout.
我得到了执行此操作的JFrame:
I got my JFrame which does this:
game.setFocusable(true);
setContentpane(game);
在我的游戏中,我添加了一个输入处理程序,该输入处理程序扩展了Keylistener并实现了JPanel.
in my game I am adding a inputhandler which extends Keylistener and implements JPanel.
setFocusable(true);
Inputhandler input = new Inputhandler();
addKeylistener(input);
这似乎不起作用,我已经编写了很多测试,但是看不到让输入句柄捕获任何keyPressed.
It just doesn't seem to work, I've been writing a lot of tests but I can't see to get the input handle capture any keyPressed.
当我将JFrame更改为:
When I change my JFrame to:
add(game);
它的工作方式就像它应该工作一样,但是这样做时遇到的问题是以正确的方式绘画我的面板.我在这个问题上有些棘手,所以请有人帮帮我.
it works like it is meant to work but the problem I encounter when doing this way is painting my panels the correct way. I'm kinda stuck on this issue so please someone help me out.
我现在到达的点:
public Game(){
setFocusable(true);
requestFocus();
requestFocusInWindow();
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getActionMap().put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Space is pressed");
}
});
this.inputHandler = new InputHandler();
addKeyListener(this.inputHandler);
setPreferredSize(new Dimension(500,500));
}
推荐答案
如果每次问这个问题时我都有一美元,我就会退休.根据之前的类似问题...
If I had a dollar for every time this question were asked, I'd retire rich. As per previous similar questions...
- 是的,您需要使JPanel具有可聚焦性,以使其KeyListener正常工作
- 并且,您还必须将焦点放在它上面,因为集中注意力是不够的.通常,这是通过在侦听的JPanel上调用
requestFocusInWindow()
来实现的. - 如果KeyListener要继续运行,没有其他任何东西可以拥有焦点或夺走焦点.
- 这是为什么我们大多数人建议反对在Swing应用程序中使用KeyListeners的几个原因之一
- 通常支持使用键绑定.
- Yes you would need to make the JPanel focusable for its KeyListener to work
- And you'd also have to give it the focus, since being focusable is not enough. Usually this is achieved by calling
requestFocusInWindow()
on the listened to JPanel. - And nothing else can have the focus or steal the focus if the KeyListener is to continue functioning.
- Which is one of several reasons why most of us recommend against use of KeyListeners for Swing applications
- And usually in favor of using Key Bindings.
修改
我已经使用了您的代码,并且可以工作,无论是键绑定还是KeyListener:
I've used your code and it works, both the key bindings and the KeyListener:
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JPanel {
private InputHandler inputHandler;
public Game() {
setFocusable(true);
requestFocus();
requestFocusInWindow();
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getActionMap().put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Space is pressed");
}
});
this.inputHandler = new InputHandler();
addKeyListener(this.inputHandler);
setPreferredSize(new Dimension(500, 500));
}
class InputHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("key pressed");
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("key released");
}
}
private static void createAndShowGui() {
Game mainPanel = new Game();
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这篇关于addKeylistener()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!