JFrame.dispose()vs System.exit() [英] JFrame.dispose() vs System.exit()

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

问题描述

这两种方法有什么区别 - System.exit() JFrame.dispose()

What is the difference between these two methods - System.exit() and JFrame.dispose()?

如果我们想在单击按钮时关闭Java Swing应用程序,我应该使用哪种方法?

If we want to close a Java Swing application when a button is clicked, which method should I use?

推荐答案

System.exit(); 导致Java VM完全终止。

System.exit(); causes the Java VM to terminate completely.

JFrame.dispose(); 导致 JFrame 窗口被操作系统销毁和清理。根据文档,这可能会导致如果没有其他Windows可用,Java VM将终止,但这应该被视为副作用而不是常态。

JFrame.dispose(); causes the JFrame window to be destroyed and cleaned up by the operating system. According to the documentation, this can cause the Java VM to terminate if there are no other Windows available, but this should really just be seen as a side effect rather than the norm.

你选择的那个真的取决于在你的情况。如果要终止当前Java VM中的所有内容,则应使用 System.exit(),并清除所有内容。如果您只想销毁当前窗口,如果这是唯一的窗口,则副作用将关闭Java VM,然后使用 JFrame.dispose()

The one you choose really depends on your situation. If you want to terminate everything in the current Java VM, you should use System.exit() and everything will be cleaned up. If you only want to destroy the current window, with the side effect that it will close the Java VM if this is the only window, then use JFrame.dispose().

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

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