JDialog setVisible(false) 与 dispose()
在对话框上使用 setVisible(false) 并稍后重用它是否有意义,或者每次调用 dispose() 并创建一个新的 JDialog 更安全。 setVisible(false) 的内存泄漏怎么办?
编辑: 我的问题并不是关于退出应用程序。有关以主框架作为父级并在应用程序生命周期内打开和关闭的对话框的更多信息。例如,假设我的应用程序有大约 10 个对话框,每次打开它们时,它们都会显示不同的数据。我应该重用这些实例并使用 setVisible() 还是每次都创建一个新的对话框并在关闭时 dispose() 它们。
Does it make sense to use setVisible(false) on a dialog and reuse it later or is safer to call dispose() every time and to make a new JDialog.
What about memory leaks with setVisible(false)?
EDIT:
My Question isn't so much about quitting the application. More about Dialogs that have the main frame as parent and are opened and closed during the application life time. E.g. let's say my applications has about 10 dialogs that display different data every time I open them. Should I reuse the instances and use setVisible() or should I make a new Dialog every time and dispose() them on closing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用
dispose()
释放资源并释放内存。如果您想再次显示该对话框,只需调用setVisible(true)
即可。需要注意的是,当 Java 虚拟机 (VM) 中最后一个可显示的窗口被处理后,VM 可能会终止。请参阅AWT 线程问题 了解更多信息。
I would recommend using
dispose()
to release resources and free up memory. If you want to show the dialog again, simply invokesetVisible(true)
.It's important to note that when the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.
我仍然看不出 JDialog#dispose(); 和
JDialog.setVisible(false)
之间有任何区别 这里,它们中的每一个都可以被唤醒以供重用,并且无论是否被处置或可见,我的观点是这个问题必须分为三个单独的区域
1)某些
JFrame
是JDialog
或JWindow
的父级(仅存在JFrame
),那么最后一个必须关灯2) 没有
JDialog
的父级灯
JFrame、JDialog 或 JWindow
,那么最后一个必须使用 --> 关闭可到达的Window[]wins = Window.getWindows();
System.exit(0);
JFrame
和JFrame.EXIT_ON_CLOSE
,或者其他方式可以be 实现WindowsListener
和System.exit(0);
I still can't see any diference
between JDialog#dispose();
andJDialog.setVisible(false)
more here, each of them could be wakeup for reuse, and doesn't matter if was/were Disposed or Visibledmy view is that this question must be splited to three separates areas
1) some
JFrame
is parent forJDialog
orJWindow
(exist only is is there JFrame
), then last one must to turn-off the lights2) without parent for
JDialog
3) there still exist another
JFrame, JDialog or JWindow
, then last one must to turn-off the lightsWindow[] wins = Window.getWindows();
System.exit(0);
JFrame
withJFrame.EXIT_ON_CLOSE
, or another way could be implementsWindowsListener
withSystem.exit(0);
调用
dispose()
会释放与对话框关联的资源。您可以在dispose()
后保留该对话框。如果您担心周围有太多对话框,请使用WeakReference
来保存引用。这将确保您不使用的对话框仅在不需要它们占用的空间时才可以在垃圾回收中幸存。Calling
dispose()
frees the resources associated with the dialog. You can keep the dialog around after youdispose()
it. If you are worried about having too many dialogs laying around, use aWeakReference
to hold the reference. That will ensure that your dialogs you are not using only survive garbage collection as long as the space they occupy is not needed.当窗口被隐藏两次时,我经历了不同(由于软件设计不良,例如)
如果您处置已处置的窗口,则虚拟机将挂起。 (java 8)
如果你在一个已经不可见的窗口上设置visible false,生活就会继续......
I experienced a difference when a window shall be hiden twice (in cause of bad software design e.g.)
If you dispose an already disposed window the VM hangs. (java 8)
If you setvisible false on an already invisible window life goes on...