在没有 System.exit 的情况下关闭 Java GUI

发布于 2024-11-30 02:11:00 字数 1125 浏览 3 评论 0原文

如何在没有 System.exit 的情况下关闭 Java GUI?当我使用dispose()时,GUI确实关闭,但之后它不会再次启动?我正在使用 GUI 自动抓取图像并将其从相机写入磁盘,因此每次启动 GUI 时都需要重复此操作。稍后,我想连接这个 GUI 以在 MATLAB 中启动。

public class MainWindow {
    int fs_c = 1;
    MainWindow() {
        JFrame main_f = new JFrame("GUI");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        //main_f.setSize(500, 620);
        main_f.pack();
        main_f.setVisible(true);
        commandVal = 0;

        while (true) {
            if (fs_c == 1) {
                commandVal = 5;
            }
            if (commandVal == 5 || commandVal == 6) {
                cImage.sendFrame(0);
                JFileChooser save_d = new JFileChooser();
                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");
                fs_c = 0;
                main_f.dispose();
            } else if (commandVal == -1) {
                stopCameraStuff();
            }
        }
    }
}

How can I close Java GUI without System.exit? When I use dispose() the GUI does close, but after that it won't start again? I am using GUI to automatically grab and write an image from camera to disk, so I need to repeat this action each time when I start GUI. Later, I want to connect this GUI to start in MATLAB.

public class MainWindow {
    int fs_c = 1;
    MainWindow() {
        JFrame main_f = new JFrame("GUI");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        //main_f.setSize(500, 620);
        main_f.pack();
        main_f.setVisible(true);
        commandVal = 0;

        while (true) {
            if (fs_c == 1) {
                commandVal = 5;
            }
            if (commandVal == 5 || commandVal == 6) {
                cImage.sendFrame(0);
                JFileChooser save_d = new JFileChooser();
                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");
                fs_c = 0;
                main_f.dispose();
            } else if (commandVal == -1) {
                stopCameraStuff();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-12-07 02:11:01

您还应该以某种方式中断 while 循环,以便该方法可以完成并返回给调用者。

You should also break the while loop somehow so that the method can be completed and return to the caller.

晒暮凉 2024-12-07 02:11:01

我会使用:

 setVisible(false);

而不是 dispose()。这样你就可以调用 setVisible(true) 让它重新出现。

I would use:

 setVisible(false);

instead of dispose(). That way you can call setVisible(true) to make it reappear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文