简体   繁体   English

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

[英]Close one JFrame without closing another?

I want to display two (or more) JFrames at the same time. 我想同时显示两个(或更多) JFrame
When I close one of them (use the default close button), the other frames should still be visible. 当我关闭其中一个(使用默认关闭按钮)时,其他帧仍应可见。

How can I do that? 我怎样才能做到这一点?

If you do not want your application to terminate when a JFrame is closed, use 如果您希望应用程序在关闭JFrame时终止,请使用

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; DO_NOTHING_ON_CLOSE (在WindowConstants中定义):不要做任何事情; require the program to handle the operation in the windowClosing method of a registered WindowListener object. 要求程序在已注册的WindowListener对象的windowClosing方法中处理该操作。
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects. HIDE_ON_CLOSE (在WindowConstants中定义):在调用任何已注册的WindowListener对象后自动隐藏框架。
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects. DISPOSE_ON_CLOSE (在WindowConstants中定义):在调用任何已注册的WindowListener对象后自动隐藏和DISPOSE_ON_CLOSE框架。
  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. EXIT_ON_CLOSE (在JFrame中定义):使用System exit方法退出应用程序。 Use this only in applications. 仅在应用程序中使用它。

This was my answer before the question was clarified, might still be useful: 在澄清问题之前,这是我的答案,可能仍然有用:

You can use setVisible(false) on your JFrame if you want to display the same frame again. 如果要再次显示相同的帧,可以在JFrame上使用setVisible(false)
Otherwise call dispose() to remove all of the native screen resources . 否则,请调用dispose()删除所有本机屏幕资源

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);
                }
            }
        });

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM