关闭由JOptionPane.showOptionDialog()创建的对话框 [英] Closing a dialog created by JOptionPane.showOptionDialog()
问题描述
我正在创建一个选项对话框,使用 JOptionPane.showOptionDialog(...)
;
I am creating an options dialog using JOptionPane.showOptionDialog(...)
;
对于选项参数我传递一个JButton数组,每个都有自己的 ActionListener
。
For the options parameter I am passing an array of JButtons each with its own 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?
可能有所不同的一点:负责显示此对话框的类是单例,因此负责显示对话框的方法是 static 。因此,调用 javax.swing.JInternalFrame.doDefaultCloseAction();
无法从静态上下文工作。
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".
谢谢
推荐答案
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);
}
}
});
这篇关于关闭由JOptionPane.showOptionDialog()创建的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!