我正在尝试编写一个接受键盘输入的全屏应用程序。我已经尝试将 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());
}