所有的拳头,我一直在很多论坛上看到这个问题,并且经过了很多深思熟虑,我找不到答案。对不起,如果它在那里而错过了它。
现在,我用JFrame制作一个游戏,我创建了一个类窗口,并试图让它在开启时集中注意力。您将看到我创建了一个WindowFocusListener,以下所有SysPrint都给出了true,但是" isFocusOwner",即使它在作为windowGainedFocus(WindowEvent e)的事件被调用时打印,并且requestFocus返回true。因此我很困惑,这里是代码:
public class Window extends Canvas {
public Window(int width, int height, String title, Game game){
JFrame frame = new JFrame(title);
frame.setPreferredSize(LoadSaved());
frame.setMaximumSize(LoadSaved());
frame.setMinimumSize(LoadSaved());
frame.setUndecorated(FullScrean());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setBackground(Color.BLACK);
frame.setAlwaysOnTop(true);
frame.setFocusable(true);
frame.toFront();
frame.requestFocusInWindow();
frame.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
System.out.println(frame.isFocusOwner()); //prints false
System.out.println(frame.isFocusable()); //prints true
System.out.println(frame.isVisible()); //prints true
System.out.println(frame.isActive()); //prints true
System.out.println(frame.isAlwaysOnTop()); //prints true
System.out.println(frame.isAutoRequestFocus()); //prints true
}
});
frame.setVisible(true);
game.start();
}
}
我唯一的另一个想法是我的主题" game.start()"干扰,因为我对这方面的知识不太自信,这是我的主要游戏课程的一部分,可以提供帮助:
public class Game extends Canvas {
public Game() {
new Window(WIDTH, HEIGHT, "Magic Online", this);
}
//The thread
public synchronized void start() {
if (running)
return;
running = true;
init();
Thread threadVisual = new Thread() {
//The game loop
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
render();
updates++;
delta--;
}
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
};
threadVisual.start();
}
public void init() {
WIDTH = getWidth();
HEIGHT = getHeight();
tex = new Texture();
mouse = new Mouse();
sound = new Sound();
key = new Keyboard();
renderer = new Renderer();
Shell.addShell(new Shell(new Main()));
this.addKeyListener(key);
this.addMouseListener(mouse);
this.addMouseMotionListener(mouse);
this.addMouseWheelListener(mouse);
}
public static void main(String args[]) {
new Game();
}
}
对不起,我试图裁掉大部分不相关的代码,我相信一切都在这里找到我的答案。如果你绝对需要一个SSCCE,我会做一个,但不必解决我的所有纹理。提前谢谢。