gpt4 book ai didi

Java - JFrame 全屏按键监听器不起作用

转载 作者:行者123 更新时间:2023-12-02 05:45:01 26 4
gpt4 key购买 nike

我正在尝试通过尝试一些不同的东西来自学 Java 和游戏编程。但现在我遇到了这个问题,当我的java应用程序通过 GraphicsDevice 全屏显示时,KeyListeners不工作。就像当我按下键盘上的按钮时它没有记录任何内容。当应用程序不是全屏时,一切都会按预期运行。

我使用的是 Mac。

代码有点乱,但应该很容易导航等。

游戏.类

public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;

// Render Vars
public static final int WIDTH = 1280;
public static final int HEIGHT = 800; //WIDTH / 16 *
public static final int SCALE = 1;
public final static String TITLE = "Test Game - inDev 1.0.0";
public static boolean fullscreen = false;

public static JFrame window = new JFrame(TITLE);

public static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
public static Color defaultColor = Color.gray;

// Thread Vars
public static boolean running = false;
private static Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

// Mechanic Vars
public static boolean mouseDown;
public static int mouseX;
public static int mouseY;

// Game Vars
public static GameState gameState = new Play();
public static boolean keyPressed;

public static Game game = new Game();

public static void main(String arghs[]) {
game.setSize(new Dimension(WIDTH, HEIGHT));
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));

fullscreen = true;

window.add(game);
window.setUndecorated(true);
window.pack();
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setVisible(true);

// HERE I GO FULLSCREEN
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);

game.addKeyListener(new KeyEventListener());
game.addMouseListener(new MouseEventListener());

game.start();
}

public void run() {

init();

long lastTime = System.nanoTime();
final double numTicks = 100.0;
double ns = 1000000000 / numTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();

while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;

if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " Ticks, FPS: " + frames);
updates = 0;
frames = 0;
}
}
stop();
}

private synchronized void start() {
if (running) {
return;
}
running = true;

thread = new Thread(this);
thread.setName("My Game");
thread.start();
}
public synchronized static void stop() {
if (!running) {
return;
}
running = false;

thread = null;
System.exit(1);
}

private void init() {
gameState.init();
}

private void update() {
gameState.update();
}

private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g.setColor(defaultColor);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

// Draw Content
gameState.render(g);

// Draw to Screen;
g.dispose();
bs.show();
}

public static void setGameState(GameState state) {
gameState = state;
gameState.init();
}
}

KeyEventListener.class

public class KeyEventListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (!Game.keyPressed) {
Game.gameState.keyPressed(e);
}
Game.keyPressed = true;
}
public void keyReleased(KeyEvent e) {
Game.gameState.keyReleased(e);
Game.keyPressed = false;
}
}

MouseEventListener.class(仅用于记录鼠标位置)

public class MouseEventListener extends MouseAdapter {

public void mousePressed(MouseEvent e) {
Game.mouseDown = true;
}
public void mouseReleased(MouseEvent e) {
Game.mouseDown = false;
}
public void mouseMoved(MouseEvent e) {
Game.mouseX = e.getX();
Game.mouseY = e.getY();
}
public void mouseDragged(MouseEvent e) {
Game.mouseX = e.getX();
Game.mouseY = e.getY();
}
}

GameState.class

public abstract class GameState {

public abstract void init();
public abstract void update();
public abstract void render(Graphics g);
public void keyPressed(KeyEvent e) {

}
public void keyReleased(KeyEvent e) {

}

}

我的 Play.class 是当前的 gameState

public class Play extends GameState {

public static int key;

public void init() {

}

public void update() {

}

public void render(Graphics g) {
g.drawString("Key: " + key, 100, 100);
}

public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
}

public void keyReleased(KeyEvent e) {

}

}

最佳答案

KeyListener 仅当组件注册为可聚焦且具有焦点时才会响应按键事件。

将窗口设置为全屏后,尝试添加...

game.requestFocusInWindow();

您可能还需要使用game.setFocusable(true)

如果不是因为您使用的是 Canvas,我建议使用 key bindings API解决所有这些焦点问题

已更新

我添加了...

window.addWindowFocusListener(new WindowAdapter() {

@Override
public void windowGainedFocus(WindowEvent e) {
System.out.println("gainedFocus");
if (!game.requestFocusInWindow()) {
System.out.println("Could not request focus");
}
}

});

窗口可见之后但在全屏显示之前

已更新

哦,你一定会喜欢这个...

基于这个问题:FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion

在将窗口设置为全屏模式后,我添加了 setVisible(false) ,然后添加了 setVisible(true) ,它似乎已经修复了它...

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
window.setVisible(false);
window.setVisible(true);

已验证在 Mac 上运行,使用 Java 7

关于Java - JFrame 全屏按键监听器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24157794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com