java 以编程方式关闭显示在 JDialog 中的 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10936306/
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-10-31 03:08:52  来源:igfitidea点击:

programmatically close a JPanel which is displayed in JDialog

javaswing

提问by gtiwari333

I have a main application frame (MainFrame class). On actionperformed event of a JButton, a JPanel (MyJPanel class)is opened by placing it in JDialog. I am not extending JDialogto create MyJPanelclass because I might need MyJPanel at other purposes too.

我有一个主应用程序框架 ( MainFrame class)。在 a 的 actionperformed 事件上JButton, a JPanel (MyJPanel class)通过将其放入 来打开JDialog。我没有扩展JDialog创建MyJPanel类,因为我可能也需要 MyJPanel 用于其他目的。

My Problem is I cannot programmatically close the MyJPanelwhich is displayed in JDialog. Is there anything that I missing? Could you please figure it out?

我的问题是我不能以编程方式关闭MyJPanel其显示在JDialog。有什么我想念的吗?你能弄清楚吗?

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


    public class MainFrame extends JPanel {
        public MainFrame() {

            JButton btnOpenJdialog = new JButton("Open JDialog");
            add(btnOpenJdialog);
            btnOpenJdialog.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JDialog jd = new JDialog();
                    MyJPanel mjp = new MyJPanel(true);//showing in JDialog
                    jd.setTitle("JDialog");
                    jd.add(mjp);
                    jd.pack();
                    jd.setVisible(true);

                }
            });
        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {

                public void run() {
                    createAndShowGUI();
                }
            });
        }

        public static void createAndShowGUI() {

            JFrame frame = new JFrame("Test-JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MainFrame());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    }

MyJPanel Class :

MyJPanel 类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;

public class MyJPanel extends JPanel {
    private boolean isShownInJDialog = false;

    public MyJPanel() {
        JButton btnCloseMe = new JButton("Finish Action");
        add(btnCloseMe);
        btnCloseMe.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isShownInJDialog) {
                    MyJPanel.this.setVisible(false);
                    //how to close the JDialog too.
                }
                else {
                    //just hide the content, 
                    MyJPanel.this.setVisible(false);
                }
            }
        });
    }

    public MyJPanel(boolean isShownInJDialog) {
        this();
        this.isShownInJDialog = isShownInJDialog;

    }

}

UPDATEI was able to solve this using Howard's answer as :

更新我能够使用霍华德的回答来解决这个问题:

...     
if (isShownInJDialog) {
        Window w = SwingUtilities.getWindowAncestor(MyJPanel.this);
        w.setVisible(false);
}
...

回答by Howard

If I understand your question correctly, you want to close the JDialogwhich your MyJPanelis contained in but do not have a reference to it?

如果我没有理解你的问题,你要关闭的JDialog,你的MyJPanel是包含在,但没有提到它?

You may either provide such a reference using the constructor of MyJPanelor change the code inside your ActionListenerto

您可以使用 的构造函数提供这样的引用,也可以MyJPanel将内部代码更改ActionListener

Window w = SwingUtilities.getWindowAncestor(MyJPanel.this);
w.setVisible(false);

which looks up the parent window of your panel without direct reference.

它在没有直接参考的情况下查找面板的父窗口。