在没有 System.exit 的情况下关闭 Java GUI
如何在没有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还应该以某种方式中断 while 循环,以便该方法可以完成并返回给调用者。
You should also break the while loop somehow so that the method can be completed and return to the caller.
我会使用:
而不是 dispose()。这样你就可以调用 setVisible(true) 让它重新出现。
I would use:
instead of dispose(). That way you can call setVisible(true) to make it reappear.