1

我在我的项目中添加了一个 jDialog Swing Form,如下图所示:

在此处输入图像描述

现在我想在关闭这个 JDialog 时从那个 jtextField 获取父 JFrame 的值,我用谷歌搜索它,我发现了这个:

Object obj=sasirMdp.showDialog();

showDialog但是编译器告诉我在我的 JDialog中没有命名方法。

当我将此方法添加到 JDialog 类时:

ReturnValue showDialog() {
    setVisible(true);
    return result;
}

copmiler 告诉我是否要创建课程ReturnValue

如果有人知道如何从 中获取该值JDialog,我将不胜感激。

4

2 回答 2

2

在我看来,您正在混淆 JDialog 和 JOptionPane。您应该阅读如何制作对话框。这是一个很好的关于swing对话框的介绍。

于 2013-06-09T10:20:43.130 回答
1

你想要这样的东西吗?

public class TestJDialog extends JFrame implements ActionListener
{
private JLabel l;

public TestJDialog(String title)
{
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setLayout(new GridLayout(0,1));
    JButton  b = new JButton("Input Dialog");
    b.addActionListener(this);
    this.add(b);

    l = new JLabel();
    this.add(l);

    setSize(300, 100);
    setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
    String s = evt.getActionCommand();
    String input = JOptionPane.showInputDialog(this,
                                               "Saisissez votre mot de passé:",
                                               s,
                                               JOptionPane.QUESTION_MESSAGE);
    l.setText("Mot passé: " + input);
}

public static void main(String[] args)
{
    new TestJDialog("Example");
}
}
于 2013-06-20T14:32:23.753 回答