子窗口的class里保持父窗口class的引用。
在子窗口的关闭事件里设置父窗口的值。

解决方案 »

  1.   

    父窗口:import java.awt.*;
    import java.awt.event.*;
    public class father {
    public static String str;
    private TextField t;
    public father(){
    f=new Frame();   
    f.setLayout(null);
    Button b=new Button("sure");
     t=new TextField();
       b.setBounds(50, 50, 50, 50);
       t.setBounds(50, 150, 100, 50);
       f.setBounds(100, 100, 300, 300);
       b.addActionListener(new actionListener());
       f.add(t);
       f.add(b);
       f.show();
       f.addWindowListener(new winListener());
    }
      private  Frame f;
      class actionListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
      sub bbb=new sub();

      }
      
      }
       public static void main(String args[]){
      father ffff=new father();
       }
       class winListener implements WindowListener{
    @Override
    public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
    f.dispose();
    System.out.println(str);
    } @Override
    public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub

    }
       
       }
    }
    子窗口:
    import java.awt.*;
    import java.awt.event.*;
    public class sub {
    public static String a;
    public Frame f;
    private father fa;
    private TextField t;
    public sub(){
    f=new Frame();
    Button bb=new Button("haha");
    f.setLayout(null);
    bb.setBounds(50, 50, 100, 50);
    t=new TextField();
    t.setBounds(50, 150, 100, 50);
    f.setBounds(100, 100, 400, 300);
    f.add(t);
    f.add(bb);
    f.addWindowListener(new winlistener());
    bb.addActionListener(new listener());
    f.show();
    }
    public sub(father fa){
    this.fa=fa;
    }
    public static void main(String args[]){
    sub s=new sub();

    }
    class winlistener implements WindowListener{ @Override
    public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
    f.dispose(); } @Override
    public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub

    }

    }
     class listener implements ActionListener{
     public void actionPerformed(ActionEvent e){
    fa.str=t.getText().trim();
     }
     }
    }已经差不多可以,如果再加个按钮就行,但是我希望能够在子窗口关闭的时候。主窗口就能够在文本上显示出来,而不需要我在主窗口做其他的操作。
    麻烦大家帮忙修改下
      

  2.   


    public class sub { 
    public static String a; 
    public Frame f; 
    private father fa; 
    private TextField t; 
    public sub(father fa){ 
       this.fa = fa;
    }创建子窗口的时候,把父窗口的引用传进来如何?