我正在尝试编写一个带有键盘输入的全屏应用程序。我已经尝试将keylisteners添加到我的JFrame中,现在添加到JFrame中的画布并将它们设置为可聚焦但是它似乎根本没有注册任何东西。在全屏模式下我有什么特别的事情要让它注册我的keyEvents吗?任何帮助非常感谢。 (显示只是JFrame的扩展,尚未更改)
public class chanceCore implements Runnable, KeyListener, MouseListener {
boolean isRunning = true;
display frame;
Canvas canvas;
GraphicsDevice device;
GraphicsEnvironment environment;
public chanceCore() {
setUp();
run();
}
public void setUp() {
frame = new display("CGE");
canvas = new Canvas();
frame.getContentPane().add(canvas);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addKeyListener(this);
canvas.addMouseListener(this);
canvas.setFocusable(true);
frame.setVisible(true);
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
device.setFullScreenWindow(frame);
}
@Override
public void run() {
while (isRunning) {
draw(canvas.getGraphics());
try {
Thread.sleep(200);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}
答案 0 :(得分:1)
试试这段代码。在调用setFullScreenMode()之后,只需添加这两行。 当我们进入全屏时出现问题,
frame.setVisible(false);
frame.setVisible(true);