检索 JDialog 中输入的输入

发布于 2024-08-28 21:39:37 字数 1358 浏览 5 评论 0原文

我扩展了 JDialog 以创建一个自定义对话框,用户必须在其中填写一些字段: dialog

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

我想出了一个可行的解决方案。它模仿 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 中是如何完成的?

I extended JDialog to create a custom dialog where the user must fill some fields :
dialog

How should I retrieve the data entered ?

I came up with a solution that works. It mimics JOptionPane but the way I do it looks ugly to me because of the static fields involved... Here is roughly my code :

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;
    }
}

As I said, that works properly, but I guess that might raise serious concurrency issues...

Is there a cleaner way to do that ? How is it done in JOptionPane ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-09-04 21:39:37

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

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:你的程序看起来很棒!

If I do this, I always works like this:

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();

Hope this helps!
PS: Your program looks great!

若水微香 2024-09-04 21:39:37

如果您打算同时显示多个对话框,那么您就会遇到并发问题,否则不会。然而,摆脱所有静态的东西会让设计更干净、更安全、更容易测试。只需通过调用代码控制对话框的创建和显示,您不需要任何静态内容。

If you intend to display multiple dialogs at the same time, then you have concurrency issues, not otherwise. However, getting rid of all the static stuff would make the design cleaner, safer and easier to test. Just control the creation and showing of the dialog from the calling code and you don't need any static stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文