如何正确隐藏JFrame [英] How to properly hide a JFrame
问题描述
我有一个非常简单的JFrame
窗口,其中包含一个按钮:No
.
I have a very simple JFrame
window that contains one button: No
.
在主要功能中,将setVisible(true);
设置为JFrame
,在No
按钮监听器中,我想关闭窗口,因此将可见性设置为false:setVisible(false);
,然后在其中执行System.exit(0);
为了防止多次运行该程序时可能的内存泄漏.
In the main function I set setVisible(true);
my JFrame
and in the No
button listener I want to close the window so I set the visibility to false: setVisible(false);
and after that I do System.exit(0);
in order to prevent possible memory leaks when running the program many times.
我有两个问题:
- 在上述情况下,我真的需要
System.exit(0);
吗? - 如果我将此
JFrame
作为popup
窗口,则不能真正使用System.exit(0);
,因为这将终止整个程序.那么如何正确关闭弹出窗口并停留在主JFrame
窗口中? (现在,我只能通过setVisible(false);
关闭它,而当我在程序执行过程中多次关闭它时,程序将变得非常缓慢.)
- Do I really need to
System.exit(0);
in the above case? - If I have this
JFrame
as apopup
window, I can't really useSystem.exit(0);
because this will terminate the whole program. So how can I properly close the popup window and stay in the mainJFrame
window? (Now I close it only bysetVisible(false);
and when I do it several times through the program execution, the program turns very slow).
推荐答案
-
使用
CardLayout
是否存在另一个弹出容器的真实原因
if is there real reason for another popup container
-
使用
JDialog
,其父级为JFrame
,其中带有setModal
/ModalityTypes
use
JDialog
with parent toJFrame
, withsetModal
/ModalityTypes
仅创建一个JDialog并通过getContentPane#removeAll()
使用JOptionPane
进行简单的用户交互
use JOptionPane
for simple users interaction
将以上两点放在一起,以将CardLayout
用于弹出JDialog
,将父级设置为JFrame
,请注意从一张卡切换到另一张卡后可能需要/来调用JDialog.pack()
put both together, above two points, to use CardLayout
for popup JDialog
with parent to JFrame
, notice after switch from one card to another could be / is required to call JDialog.pack()
这篇关于如何正确隐藏JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!