我扩展了 JDialog 以创建一个自定义对话框,用户必须在其中填写一些字段:
我应该如何检索输入的数据?
我想出了一个可行的解决方案。它模仿 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 中完成的?