【问题标题】:Get a value from JDialog to the parent JFrame从 JDialog 获取一个值到父 JFrame
【发布时间】:2013-06-09 10:11:51
【问题描述】:

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

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

Object obj=sasirMdp.showDialog();

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

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

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

编译器告诉我是否要创建类ReturnValue

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

【问题讨论】:

  • @Heuster 是的,我读到了那篇文章,它对我没有帮助,接受的答案是阅读手册,当我阅读它时我无法得到我想要的东西
  • 好的,您能添加一些您的 JDialog 的代码以及如何调用它吗?然后更容易指出如何将该答案应用于您的项目。

标签: java swing jdialog


【解决方案1】:

在我看来,您正在混淆 JDialog 和 JOptionPane。你应该阅读How to Make Dialogs。这是对带有 swing 的对话框的一个很好的介绍。

【讨论】:

    【解决方案2】:

    你想要这样的东西吗?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      相关资源
      最近更新 更多