日期:2014-05-20  浏览次数:20819 次

怎么把JDialog子窗体的数据传递给主程序父窗体
我在主窗体弹出一个JDialog子窗体,然后子窗体把一些参数传递给父窗体,到底怎么传递呢?父传递子己弄好了,就差这个了...
我百度了好久很少有关于JAVA父子窗体传递值的资料,希望高手指教.谢谢.

------解决方案--------------------
参数是在父窗口定义好的

比如父类:

public void setMap(Map map){
...
}

你在子窗口调用父窗口的setMap方法,就把你的参数包成map给父窗口了
------解决方案--------------------
我以前写过一个(JDialog)对话框实现数据交换。实现的就是子窗口向父窗口传递数据。
lz看下有没有用
http://blog.csdn.net/chosen0ne/archive/2009/08/23/4476046.aspx
------解决方案--------------------
Java code

package testGUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestParent extends JFrame implements ActionListener {

    private static final long serialVersionUID = 2793297815624831929L;

    public String getLabelValue() {
        return labelValue;
    }

    public void setLabelValue(String labelValue) {
        this.labelValue = labelValue;
    }

    private String labelValue = null;

    private JLabel label = null;

    private JButton btn = null;

    public TestParent() {
        init();
    }

    private void init() {
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
        this.getContentPane().add(getSouthPanel(), BorderLayout.SOUTH);
        this.setSize(new Dimension(200, 200));
        this.setLocation(new Point(200,200));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private JPanel getSouthPanel() {
        JPanel panel = new JPanel();
        label = new JLabel();
        panel.add(label);
        return panel;
    }

    private JPanel getCenterPanel() {
        JPanel panel = new JPanel();
        btn = new JButton("ChildDialog");
        btn.addActionListener(this);
        panel.add(btn);
        return panel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(btn)) {
            new ChildDialog(TestParent.this, true);
            label.setText(getLabelValue());
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new TestParent();
    }

}

class ChildDialog extends JDialog implements ActionListener {
    private static final long serialVersionUID = -7633528897837030539L;

    private JTextField text = null;

    private JButton btn = null;

    private TestParent parent = null;

    public ChildDialog(TestParent _parent, boolean _modal) {
        super(_parent, _modal);
        this.parent = _parent;
        init();
    }

    private void init() {
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
        this.getContentPane().add(getSouthPanel(), BorderLayout.SOUTH);
        this.setSize(new Dimension(100, 100));
        this.setLocation(new Point(100,100));
        this.setVisible(true);
    }

    private JPanel getCenterPanel() {
        JPanel panel = new JPanel();
        text = new JTextField(5);
        panel.add(text);
        return panel;
    }

    private JPanel getSouthPanel() {
        JPanel panel = new JPanel();
        btn = new JButton("OK");
        btn.addActionListener(this);
        panel.add(btn);
        return panel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(btn)) {
            parent.setLabelValue(text.getText());
            onExit();
        }
    }

    private void onExit() {
        this.dispose();
    }
}