检索在JDialog中输入的输入

我扩展了JDialog来创建一个自定义对话框,用户必须填写一些字段: 对话框http://sofzh.miximages.com/java/www.freeimagehosting.net

我该如何检索输入的数据?

我提出了一个有效的解决方案。 它模仿JOptionPane,但由于涉及静态字段,我对它的看法看起来很难看……这大致是我的代码:

public class FObjectDialog extends JDialog implements ActionListener { private static String name; private static String text; private JTextField fName; private JTextArea fText; private JButton bAdd; private JButton bCancel; private FObjectDialog(Frame parentFrame) { super(parentFrame,"Add an object",true); // build the whole dialog buildNewObjectDialog(); setVisible(true); } @Override public void actionPerformed(ActionEvent ae) { if(ae.getSource()==bAdd){ name=fName.getText(); text=fText.getText(); } else { name=null; text=null; } setVisible(false); dispose(); } public static String[] showCreateDialog(Frame parentFrame){ new FObjectDialog(parentFrame); String[] res={name,text}; if((name==null)||(text==null)) res=null; return res; } } 

正如我所说,这是正常的,但我想这可能会引发严重的并发问题……

有更清洁的方法吗? 如何在JOptionPane中完成?

如果我这样做,我总是这样工作:

 FObjectDialog fod = new FObjectDialog(this); fod.setLocationRelativeTo(this); // A model doesn't set its location automatically relative to its parent fod.setVisible(true); // Now this code doesn't continue until the dialog is closed again. // So the next code will be executed when it is closed and the data is filled in. String name = fod.getName(); String text = fod.getText(); // getName() and getText() are just two simple getters (you still have to make) for the two fields their content // So return textField.getText(); 

希望这可以帮助!
PS:你的程序看起来很棒!

如果您打算同时显示多个对话框,则会出现并发问题,否则会出现并发问题。 但是,摆脱所有静态的东西会使设计更清洁,更安全,更容易测试。 只需从调用代码控制对话框的创建和显示,您就不需要任何静态内容。