关闭对话框窗口的可选方法 [英] Optional way to close a dialog window

查看:163
本文介绍了关闭对话框窗口的可选方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义JFrame在我正在处理的Java应用程序中实现一个简单的对话框。

I'm using a custom JFrame to implement a simple dialog in a Java application I'm working on.

用户按下应用按钮后窗口,它应该关闭。

After the user pushes the 'Apply' button in the window, it should close.

最常规的方法是什么?最好的方法是在课堂上 setVisible(false)吗?有没有更多的建议?

What would be the most conventional way to do this? Is setVisible(false) inside the class the best way? Is there any way more recommended?

推荐答案

要关闭Swing中的窗口,如 JFrame JDialog 你有两个选择。

To close a window in Swing like JFrame or JDialog you have two options.

只需致电 dispose()方法:


public void dispose()

释放所有本机此窗口使用的屏幕资源,其
子组件及其拥有的所有子组件。也就是说,这些组件的资源
将被销毁,他们消耗的任何内存都将返回给操作系统
,并且它们将被标记为不可显示。

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

例如:

frame.dispose();
dialog.dispose();






派遣 WINDOW_CLOSING event



您可以发送新的 WindowEvent 是这样的:


Dispatch a WINDOW_CLOSING event

You can dispatch a new WindowEvent like this:

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));

如果窗口附有 WindowListener 。如果设置了框架(或对话框)的默认关闭操作,则将执行此操作。可能的关闭操作是:

If the window has attached a WindowListener it will be notified. If the frame's (or dialog's) default close operation is set then this action will be performed. The possible close operations are:


  • DO_NOTHING_ON_CLOSE (在中定义WindowConstants ):不要做任何事情;
    要求程序在已注册的WindowListener对象的windowClosing
    方法中处理该操作。

  • HIDE_ON_CLOSE (在<$ c $中定义c> WindowConstants ):在调用任何已注册的WindowListener对象后自动隐藏
    帧。

  • DISPOSE_ON_CLOSE (在WindowConstants中定义) :在调用任何已注册的WindowListener
    对象后自动隐藏并
    处理该帧。

  • EXIT_ON_CLOSE (在 JFrame中定义并且不适用于 JDialog ):使用System退出方法退出应用程序。仅在应用程序中使用它。

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame and not available for JDialog): Exit the application using the System exit method. Use this only in applications.

这篇关于关闭对话框窗口的可选方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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