java中的System.exit(0) [英] System.exit(0) in java

查看:144
本文介绍了java中的System.exit(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swing编写应用程序。我需要通过单击 JButton 退出应用程序,因为我可以使用 System.exit()或者我应该使用其他一些方法,这是最好的做法。如果调用 System.exit()不是最佳做法,那么告诉原因并告诉退出应用程序的替代方法。

I am writing an application program using swing. I need to exit from the application by clicking the JButton for that can i use System.exit() or should i use some other methods, which is the best practice. If calling System.exit() is not best practice then tell the reason and tell the alternative way to exit from the application.

推荐答案

Personnaly,我认为最好让应用程序自行退出:如果你用 main编写一个主类(String [] args) ,使用空代码,运行它将从Java程序静默退出。

Personnaly, I do consider as best practice to let application exit by itself : if you write a main class with a main(String[] args), with empty code, running it will exit silently from Java program.

但是,在大多数情况下,像大多数开发人员一样,我依赖于 System.exit(/ 退出代码 /),特别是对于Swing应用程序,其中Swing EDT将无休止地运行,如justkt 写道..这种方法的已知drawxback是大多数应用程序代码都没有被调用,我通过调用<设置关闭挂钩来避免code> Runtime.addShutdownHook(Thread) ,这将允许我清理应用程序(关闭线程,等等)。

But, in most times, like most of developpers, I rely upon System.exit(/an exit code/), especially for Swing applications, where the Swing EDT will run endlessly, as justkt wrote.. The known drawxback of this method is that most of applciation code is not called, what I avoid by setting a shutdown hook by calling Runtime.addShutdownHook(Thread), which will allow me to clean application (close threads, and so on).

这篇关于java中的System.exit(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆