日期:2014-05-20 浏览次数:20962 次
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FatherFrame extends JFrame implements ActionListener {
private JButton btn=new JButton("click");
public JTextField getJtx() {
return jtx;
}
private JTextField jtx=new JTextField(15);
private JDialog dialog=null;
public FatherFrame(String title){
super(title);
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(jtx);
c.add(btn);
btn.addActionListener(this);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
dialog=new ChildDialog(this);
// dialog.getParent();
}
public static void main(String[] args) {
new FatherFrame("demo");
}
}
class ChildDialog extends JDialog{
private FatherFrame father=null;
private JLabel label=null;
private JTextArea jta=new JTextArea();
public ChildDialog(FatherFrame fatherFrame){
this.father=fatherFrame;
jta.setText(father.getJtx().getText());
label=new JLabel("以下是从父窗口获得的信息:");
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(label);
this.getContentPane().add(jta);
this.pack();
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}