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

查看:96
本文介绍了JFrame.dispose() 与 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() 与 System.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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