【发布时间】:2010-04-12 07:19:15
【问题描述】:
我扩展了 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 中是如何实现的?
【问题讨论】:
-
您使用什么外观和感觉?
-
@Martijn Courteaux : Nimbus (stackoverflow.com/questions/2616448/…) ;-)
标签: java user-interface modal-dialog jdialog