我在让KeyBoardFocusManger与全屏窗口一起使用时遇到问题.不管怎样,它都不会得到键盘输入.我使用System.exit(0)和println()来查找对keypressed / released / typed方法的任何调用,但是不会引发任何错误.我已经尝试过KeyListeners;但是当我阅读this之后,我换成了KeyboardFocusManager,同样的事情仍然发生.我真的很绝望.根据我的判断,Window是否无法吸引键盘的注意力?
这是我的主要内容:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// Determine if full-screen mode is supported directly
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if (gs.isFullScreenSupported()) {
Frame frame = new Frame(gs.getDefaultConfiguration());
SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(frame);
// Enter full-screen mode
gs.setFullScreenWindow(spaceInvaderUI);
} else {
JOptionPane.showMessageDialog(null, "Does not support full screen!", "Error 0x01", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
});
}
这是包含KeyBoardFocusManger的UI,并添加到addListeners()方法中:
class SpaceInvaderUI extends Window {
private JPanel drawingPanel;
private Image background;
private JButton btnExit;
public SpaceInvaderUI(Frame frame) {
super(frame);
try {
background = ImageIO.read(getClass().getResourceAsStream("background.png"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
System.exit(2);
}
createWindow();
}
private void createComponents() throws HeadlessException {
drawingPanel = new DrawingPanel(background, this);
btnExit = new JButton("Exit");
}
private void createWindow() {
createComponents();
addListeners();
addComponentsToWindow();
}
private void addComponentsToWindow() {
add(drawingPanel, BorderLayout.CENTER);
add(btnExit, BorderLayout.SOUTH);
}
private void addListeners() {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
private class MyDispatcher implements KeyEventDispatcher {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
System.out.println("pressed");
System.exit(0);
} else if (e.getID() == KeyEvent.KEY_RELEASED) {
System.out.println("released");
System.exit(0);
} else if (e.getID() == KeyEvent.KEY_TYPED) {
System.out.println("Typed");
System.exit(0);
}
return false;
}
}
}
退出按钮只是因为我厌倦了通过taskmanager杀死我的应用程序.最后,这是我将进行游戏的面板,并且画了我的背景:
public class DrawingPanel extends JPanel {
private final Image background;
private final SpaceInvaderUI invaderUI;
DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
this.background = background;
this.invaderUI = invaderUI;
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(background.getScaledInstance((int) invaderUI.getWidth(), (int) invaderUI.getHeight(), Image.SCALE_SMOOTH), 0, 0, this);
}
}
先感谢您.
编辑:我现在已经尝试在我的drawingPanel上使用键绑定,但是当我按f2时仍然没有任何反应:
class SpaceInvaderUI extends Window {
private JPanel drawingPanel;
private Image background;
private JButton btnExit;
public SpaceInvaderUI(Frame frame) {
super(frame);
try {
background = ImageIO.read(getClass().getResourceAsStream("background.png"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
System.exit(2);
}
createWindow();
}
private void createComponents() throws HeadlessException {
drawingPanel = new DrawingPanel(background, this);
btnExit = new JButton("Exit");
}
private void createWindow() {
createComponents();
addListeners();
addComponentsToWindow();
}
private void addComponentsToWindow() {
add(drawingPanel, BorderLayout.CENTER);
add(btnExit, BorderLayout.SOUTH);
}
private void addListeners() {
Action exit = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
drawingPanel.getInputMap().put(KeyStroke.getKeyStroke("F2"),
exit);
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
}