简体   繁体   中英

Can I terminate a program with System.exit() without closing GUI?

I have a program that runs events based on values read from a text file. I set up a GUI that has a start button to launch the program running. If there is a terminate event in the text file then I shut the program down using 'System.exit(0);'. The only problem is that I don't want the GUI to close, I just want the program to stop running. Is there an easy way to keep the GUI open? I have a feeling this is a stupid question. Please excuse me if it is, as I am new to programming and very new to GUI's.

If you run whatever you are doing on one thread, and the gui on another, just terminate the other thread. Try to make your program a Runnable and then run it with an Executor, that'll be a simple solution. For example you could call a method on your main thread from the worker to see if it should terminate. Avoid terminating threads by force as a general advice.

Your GUI is part of your program itself. Calling System.exit(0) will stop the JVM along with your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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