Java 关闭一个 JFrame 而不关闭另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1944446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:40:01  来源:igfitidea点击:

Close one JFrame without closing another?

javaswingjframe

提问by Keating

I want to display two (or more) JFramesat the same time.
When I close one of them (use the default close button), the other frames should still be visible.

我想同时显示两个(或更多)JFrame
当我关闭其中一个(使用默认关闭按钮)时,其他帧应该仍然可见。

How can I do that?

我怎样才能做到这一点?

采纳答案by Peter Lang

If you do notwant your application to terminate when a JFrameis closed, use

如果您希望应用程序在 aJFrame关闭时终止,请使用

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

instead of

代替

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From the documentation:

文档

  • 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): Exit the application using the System exit method. Use this only in applications.
  • DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不要做任何事情;要求程序处理已注册 WindowListener 对象的 windowClosing 方法中的操作。
  • HIDE_ON_CLOSE(在 WindowConstants 中定义):在调用任何注册的 WindowListener 对象后自动隐藏框架。
  • DISPOSE_ON_CLOSE(在 WindowConstants 中定义):在调用任何已注册的 WindowListener 对象后自动隐藏和处置框架。
  • EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit 方法退出应用程序。仅在应用程序中使用它。


This was my answer before the question was clarified, might still be useful:

这是我在问题澄清之前的回答,可能仍然有用:

You can use setVisible(false)on your JFrameif you want to display the same frame again.
Otherwise call dispose()to remove all of the native screen resources.

如果您想再次显示相同的帧,您可以setVisible(false)在您的上使用JFrame
否则调用dispose()删除所有本机屏幕资源

回答by Laurent K

Does it help you ?

它对你有帮助吗?

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TwoJFrames {
    public static void main(String[] args) {
        int nb = 4;
        if (args != null && args.length > 0) {
            nb = Integer.parseInt(args[0]);
        }

        final int frameCount = nb;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                for (int i = 0; i < frameCount; i++) {
                    JFrame frame = new JFrame("Frame number " + i);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    JPanel p = new JPanel(new BorderLayout());
                    p.add(new JLabel("Click on the corner to close..."), BorderLayout.CENTER);
                    frame.setContentPane(p);
                    frame.setSize(200, 200);
                    frame.setLocation(100 + 20 * i, 100 + 20 * i);
                    frame.setVisible(true);
                }
            }
        });

    }
}