Java 关闭由 JOptionPane.showOptionDialog() 创建的对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730044/
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
Closing a dialog created by JOptionPane.showOptionDialog()
提问by llm
I am creating an options dialog using JOptionPane.showOptionDialog(...)
;
我正在创建一个选项对话框JOptionPane.showOptionDialog(...)
;
For the options parameter I am passing an array of JButtons each with its own ActionListener
.
对于 options 参数,我传递了一个 JButton 数组,每个 JButton 都有自己的ActionListener
.
One of these buttons is responsible for closing the dialog. My question is: what code do I place in the close button's event handler to close the option dialog?
这些按钮之一负责关闭对话框。我的问题是:我应该在关闭按钮的事件处理程序中放置什么代码来关闭选项对话框?
A point that may make a difference: the class responsible for showing this dialog is a singleton and, as such, the method responsible for displaying the dialog is static. Therefore, calling javax.swing.JInternalFrame.doDefaultCloseAction();
does not work "from a static context".
有一点可能会有所不同:负责显示此对话框的类是单例,因此,负责显示对话框的方法是static。因此,javax.swing.JInternalFrame.doDefaultCloseAction();
“从静态上下文”调用不起作用。
Thanks
谢谢
采纳答案by Adamski
final JButton btn = new JButton("Close");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Window w = SwingUtilities.getWindowAncestor(btn);
if (w != null) {
w.setVisible(false);
}
}
});
回答by cmujica
Try
尝试
JOptionPane.getRootFrame().dispose();