我认为您错过了重点
JOptionPane。它具有显示自己的对话框的功能…
public class TestOptionPane02 { public static void main(String[] args) { new TestOptionPane02(); } public TestOptionPane02() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JTextField textField = new JTextField(10); String btnString1 = "Save Object"; String btnString2 = "Delete Object"; //Create an array of the text and components to be displayed. String msgString1 = "Object label:"; Object[] array = {msgString1, textField}; //Create an array specifying the number of dialog buttons //and their text. Object[] options = {btnString1, btnString2}; int result = JOptionPane.showOptionDialog(null, array, "", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, "New Object", options, options[0]); switch (result) { case 0: System.out.println("Save me"); break; case 1: System.out.println("Delete me"); break; } } }); }}要手动执行此操作,您将需要做更多的工作。
首先,您将不得不侦听面板的属性更改事件,寻找的更改,
JOptionPane.VALUE_PROPERTY并忽略
JOptionPane.UNINITIALIZED_VALUE…的任何值。
一旦检测到更改,就需要处理对话框。
您将需要提取通过
JOptionPane#getValue方法选择的值,该方法返回
Object。您将不得不自己打断该值的含义…
不用说,
JOptionPane.showXxxDialog方法可以为您完成所有这些工作…
现在,如果您担心必须完成对话框的所有设置,那么我将编写一个实用程序方法,该方法可以完全完成对话框或采用必需的参数…但这就是我
更新
不知道为什么我不早想到…
而不是传递数组
String作为options参数,而是传递的数组
JButton。这样,您可以附加自己的侦听器。
options-一组对象,指示用户可以做出的可能选择; 如果对象是组件,则可以正确渲染它们
;非字符串对象使用其toString方法呈现;如果此参数为null,则选项由外观决定



