全屏窗口将无法使用KeyListener或KeyBoardFocusManager获得键盘输入 [英] Full screen Window won't get keyboard input using KeyListener or KeyBoardFocusManager
问题描述
我在使KeyBoardFocusManger
与全屏Window
一起使用时遇到问题.不管怎样,它都不会得到键盘输入.我使用了System.exit(0)
和println()
来查找对keypressed/released/typed方法的任何调用,但是不会引发任何错误.我已经尝试过KeyListeners
;但是当我阅读 this 后,我改为了KeyboardFocusManager
,同样的事情仍然发生.我真的很绝望.根据我的判断,Window
没有得到键盘的关注?
I'm having problems with getting my KeyBoardFocusManger
to work with my full screen Window
. No matter what, it just wont get keyboard input. I used a System.exit(0)
and a println()
to look for any call to the keypressed/released/typed method, but no errors are thrown. I've tried KeyListeners
; but after I read this, I changed to a KeyboardFocusManager
, and the same thing still happens. I'm really getting desperate; from what I can judge, the Window
is not getting focus of the keyboard?
这是我的主要爱好:
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
并添加到addListeners()
方法中的UI:
and here is the UI which contains the KeyBoardFocusManger
, and is added in addListeners()
method:
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杀死我的应用程序.最后,这是我将进行游戏的面板,我的背景画在此面板上:
The exit button is just because I got tired of killing my app via taskmanager. Finally here is my panel on which the game will take place and my background is painted on:
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 时仍然没有任何反应:
I have now tried using a keybinding on my drawingPanel
but still nothing happens when I press 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);
}
});
}
}
推荐答案
为什么要在Swing GUI中使用AWT组件?我担心(但不确定),这样做可能会丢失一些Swing功能.
Why are you using AWT components in your Swing GUI? I fear (but don't know for sure) that by doing this, you may be losing some of the Swing functionality.
If you are only capturing select key select key strokes to control the game, consider using Key Bindings.
修改:
不,AWT组件没有问题,但仍然可能不应该使用.
Edit:
No, the AWT components aren't at fault, but still probably should not be used.
编辑2 :
由于某些原因,您的顶级窗口未处于聚焦状态.继续测试代码...
Edit 2:
Your top level Window is not focused for some reason. Continuing to test code...
编辑3 :
使用JFrame对我有用:
Edit 3:
Using a JFrame worked for me:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test3 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if (gs.isFullScreenSupported()) {
SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(gs.getDefaultConfiguration());
gs.setFullScreenWindow(spaceInvaderUI);
} else {
JOptionPane.showMessageDialog(null,
"Does not support full screen!", "Error 0x01",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
});
}
}
// class SpaceInvaderUI extends JWindow {
class SpaceInvaderUI extends JFrame {
private JPanel drawingPanel;
private Image background;
private JButton btnExit;
public SpaceInvaderUI(GraphicsConfiguration gc) {
super(gc);
createWindow();
addKeyBindings();
setUndecorated(true);
}
private void addKeyBindings() {
int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = drawingPanel.getInputMap(condition );
ActionMap actionMap = drawingPanel.getActionMap();
boolean released = false;
KeyStroke upArrowKeyStrokePressed = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
String upArrowPressed = "up arrow pressed";
inputMap.put(upArrowKeyStrokePressed , upArrowPressed);
actionMap.put(upArrowPressed, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("up arrow pressed");
}
});
released = true;
String upArrowReleased = "up arrow released";
KeyStroke upArrowKeyStrokeReleased = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
inputMap.put(upArrowKeyStrokeReleased , upArrowReleased);
actionMap.put(upArrowReleased , new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("up arrow released");
}
});
}
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) {
// System.out.println("in dispatch. 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;
// }
// }
}
class DrawingPanel extends JPanel {
private final Image background;
private final SpaceInvaderUI invaderUI;
DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
this.background = background;
this.invaderUI = invaderUI;
setBackground(Color.pink);
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
}
}
这篇关于全屏窗口将无法使用KeyListener或KeyBoardFocusManager获得键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!