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();
}
}
}
}
You should also break the while loop somehow so that the method can be completed and return to the caller.
I would use:
setVisible(false);
instead of dispose(). That way you can call setVisible(true) to make it reappear.
精彩评论