【发布时间】:2014-12-10 15:09:30
【问题描述】:
我一直在使用JOptionPane API 来显示自定义对话框,我发现了一个奇怪的情况:当我选择 OK 或 Cancel 选项时或按 Esc 键此对话框不会按预期显示。
问题是,不是使用这一行来显示模式对话框:
JOptionPane.showConfirmDialog( null
, "The quick brown fox jumps over the lazy dog."
, "New Dialog"
, JOptionPane.OK_CANCEL_OPTION
, JOptionPane.PLAIN_MESSAGE);
我想使用 API,将所有参数一一设置并显示一个对话框,如 docs 所示(参见直接使用部分):
JOptionPane pane = new JOptionPane(arguments);
pane.set.Xxxx(...); // Configure
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
但是,当我关闭对话框时,我的应用程序继续运行,即使我将默认关闭操作设置为DISPOSE_ON_CLOSE,这让我怀疑对话框没有正确处理。
下面是一个MCVE 来玩:
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Demo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("The quick brown fox jumps over the lazy dog.");
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
JDialog dialog = optionPane.createDialog("New Dialog");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
});
}
}
【问题讨论】:
标签: java swing dispose joptionpane