怎么把JDialog子窗体的数据传递给主程序父窗体 我在主窗体弹出一个JDialog子窗体,然后子窗体把一些参数传递给父窗体,到底怎么传递呢?父传递子己弄好了,就差这个了...我百度了好久很少有关于JAVA父子窗体传递值的资料,希望高手指教.谢谢. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 问题的关键是怎么取得父窗口的句柄,没做过SWING,不太了解。能不能把代码发上来,然后让大家一起查查JavaDoc。 参数是在父窗口定义好的比如父类:public void setMap(Map map){...}你在子窗口调用父窗口的setMap方法,就把你的参数包成map给父窗口了 我以前写过一个(JDialog)对话框实现数据交换。实现的就是子窗口向父窗口传递数据。lz看下有没有用http://blog.csdn.net/chosen0ne/archive/2009/08/23/4476046.aspx 2楼的方法我试过了,可是不行,因为我在JDialog获取不了父窗体的对象,所有无法直接调用父窗体的方法要是能获取父窗体对象就好了 你把父窗口作为dialog的Listener可以么?这样,在发生某个事件的时候,在Listener里面就可以直接用this变量来改变父窗口的属性了。 拜托,你的父窗口的对象是传到子窗口的子窗口:public MyDialog(ParentFrame parent){this.parent=parent;}...你得到了父窗口的对象,想干吗干吗。 晕,风之力你自己写个例子看看好了,并且没有什么this.parent这个属性,根本不行~~下面是我子窗体的构造方法。public ConfirmWin(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } 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(); }} 单实例父:public static WinMain winMain; public static WinMain getInstance(){ if(null==winMain){ winMain=new WinMain(); } return winMain; }private String param;public void setParam(String param){ this.param=param}子:WinMain.getInstance.setParam("pp"); TreeSet的排序问题 java api有个地方看不懂... 求助:关于JTextPane的应用问题? 问一个关于父类的父类的方法调用问题 java编程,同步问题 高手来看看我这个"记事本"的另存为功能怎么没有真正实现 JAVA的系列化对象 如何能被 MFC CArchive的反系列化吗? 关于nio,急啊,高手帮帮忙 大家来说说!!! 怎样把一个.csv文件写如数据库 用java循环解答 Java装饰模式
能不能把代码发上来,然后让大家一起查查JavaDoc。
...
}你在子窗口调用父窗口的setMap方法,就把你的参数包成map给父窗口了
lz看下有没有用
http://blog.csdn.net/chosen0ne/archive/2009/08/23/4476046.aspx
这样,在发生某个事件的时候,在Listener里面就可以直接用this变量来改变父窗口的属性了。
拜托,你的父窗口的对象是传到子窗口的子窗口:public MyDialog(ParentFrame parent){
this.parent=parent;
}
...你得到了父窗口的对象,想干吗干吗。
下面是我子窗体的构造方法。public ConfirmWin(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
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();
}
}
父:
public static WinMain winMain;
public static WinMain getInstance(){
if(null==winMain){
winMain=new WinMain();
}
return winMain;
}
private String param;
public void setParam(String param){
this.param=param
}
子:
WinMain.getInstance.setParam("pp");