如何将 JDialog 存储在 JOptionPane 创建的 JDialog 对象中

how to store JDialog in JDialog object created by JOptionPane

我创建 JDialog JOptionPane 就像

JOptionPane. showMessageDialog(null, "Last Warning!","Warning", JOptionPane.WARNING_MESSAGE);

这直接显示了一个对话框,return无效,所以我不能将它存储在 JDialog 变量中。

我想做这样的事情

JDialog warning = JOptionPane. showMessageDialog(null, "Last Warning!","Warning", JOptionPane.WARNING_MESSAGE);
// some code
warning.setVisible(true);

我该怎么做?

JOptionPane 为了方便起见,有两个静态实用方法,并允许根据需要为自定义操作创建实例。这样做的方式更冗长,但更灵活:

JOptionPane pane = new JOptionPane("Last Warning!", JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION);
JDialog warning = pane.createDialog("Warning");

// more code...

warning.setVisible(true);

有关更多信息,我建议查看 JOptionPane.showOptionDialog(...) 的源代码,因为它显示了如何为各种消息类型、初始值等配置对话框。或者,查看 How to Make Dialogs 来自 Java 教程。