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
programmatically close a JPanel which is displayed in JDialog
提问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 JDialog
to create MyJPanel
class 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 MyJPanel
which 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 JDialog
which your MyJPanel
is contained in but do not have a reference to it?
如果我没有理解你的问题,你要关闭的JDialog
,你的MyJPanel
是包含在,但没有提到它?
You may either provide such a reference using the constructor of MyJPanel
or change the code inside your ActionListener
to
您可以使用 的构造函数提供这样的引用,也可以MyJPanel
将内部代码更改ActionListener
为
Window w = SwingUtilities.getWindowAncestor(MyJPanel.this);
w.setVisible(false);
which looks up the parent window of your panel without direct reference.
它在没有直接参考的情况下查找面板的父窗口。