简体   繁体   English

JFrame 不会关闭

[英]JFrame Won't Close

I am working on a personal project to experiment with 2D physics.我正在做一个个人项目来试验 2D 物理。 Right now, I am trying to set up the JFrame so that it updates 60 times per second.现在,我正在尝试设置 JFrame 使其每秒更新 60 次。 However, when I call the method to start updating the JFrame, I cannot close the JFrame.但是,当我调用该方法开始更新 JFrame 时,我无法关闭 JFrame。 But if I leave out the part where I call the method, I can close the JFrame.但是如果我省略了调用该方法的部分,我可以关闭 JFrame。 This is the main code:这是主要代码:


import javax.swing.JFrame;

import utils.Frame;

public class Engine {

    Frame w;
    
    boolean running = false;
    JFrame f;
    
    public void start() {
        init();
        updater();
    }
    
    public void init() {
        
        w = new Frame();
        
        running = true;
        f = w.create(500, 500, "Work please");
        
    }
    
    public void updater() {
        
        int fps = 60;
        double timePerTick = 1000000000 / fps;
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();
        long timer = 0;

        while (running) {
            now = System.nanoTime();
            delta += now - lastTime;
            timer += now - lastTime;
            lastTime = now;

            if (delta >= timePerTick) {
                //render
                delta -= timePerTick;
            }

        }
    }
    
}

And this is the code for Frame:这是 Frame 的代码:


import javax.swing.JFrame;

public class Frame {

    public JFrame create(int width, int height, String title) {

        JFrame f = new JFrame();
        f.setSize(width, height);
        f.setTitle(title);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        
        return f;
    }

}

If I don't call updater() in start(), I can close the JFrame.如果我不在 start() 中调用 updater(),我可以关闭 JFrame。 I know that I leave running = true, but it should still close because that would work on my old computer.我知道我离开 running = true,但它仍然应该关闭,因为这可以在我的旧计算机上运行。 And even if I need to make running = false, I already tried that by adding a WindowListener to f in Frame.即使我需要设置running = false,我已经尝试通过在Frame 中的f 中添加一个WindowListener。 It would call a method to make running = false, but for some reason the WindowListener wouldn't active when I hit the close button.它会调用一个方法来使running = false,但由于某种原因,当我点击关闭按钮时,WindowListener 不会激活。 Thank you for your help in advance.提前谢谢你的帮助。 If this will help you in any way, I had to download a direct package of the Eclipse IDE from the website because every time I used the installer, every project would get the error "Failed to init ct.sym..." And for some reason, when I run a Java program with a JFrame in it, instead of the usual Jar logo, the Java mascot "Duke" shows up as the icon, which has been throwing me off.如果这对您有任何帮助,我必须直接下载 Eclipse IDE 的 package IDE 从网站上下载,因为每次我使用安装程序时都会出错... some reason, when I run a Java program with a JFrame in it, instead of the usual Jar logo, the Java mascot "Duke" shows up as the icon, which has been throwing me off.

Make sure you have two methods that control whether the program is running.确保您有两种方法可以控制程序是否正在运行。

However make sure both methods are synchronized by using the-但是,请确保使用以下方法同步这两种方法 -

synchronized

-keyword. -关键词。

public synchronized void start() {
    if (running) return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop() {
    if (!running) return;
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM