如何正确隐藏JFrame [英] How to properly hide a JFrame

查看:339
本文介绍了如何正确隐藏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.

我有两个问题:

  1. 在上述情况下,我真的需要System.exit(0);吗?
  2. 如果我将此JFrame作为popup窗口,则不能真正使用System.exit(0);,因为这将终止整个程序.那么如何正确关闭弹出窗口并停留在主JFrame窗口中? (现在,我只能通过setVisible(false);关闭它,而当我在程序执行过程中多次关闭它时,程序将变得非常缓慢.)
  1. Do I really need to System.exit(0); in the above case?
  2. If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).

推荐答案

  1. 使用 CardLayout

是否存在另一个弹出容器的真实原因

if is there real reason for another popup container

  • use JDialog with parent to JFrame, with setModal / 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屋!

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