关闭

关于两个窗口间的数据传递

1080人阅读 评论(0) 收藏 举报

其实道理很简单,关键是在frame1在调用frame2的时候,frame2怎么获得frame1的对象

一种方法是改造frame2的构造函数,例如:

Frame2(Frame1 fObj){}

即把把Frame1类的对象带进Frame2中,那么frame2就可以调用frame1的各种属性和方法了

第二种方法就是不改造Frame2的高造函数,而是写一个方法返回Frame1的对象

例如:

public void getFrame1Object(Frame1 fObj){

     f1Obj=fObj;

}

 

偷个懒,用jbuilder做的测试,ctrl c + v 太方便了

主程序代码:

package 互相调用菜单;

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Application {
    boolean packFrame = false;

    /**
     * Construct and show the application.
     */
    public Application() {
        Frame1 frame = new Frame1();
        // Validate frames that have preset sizes
        // Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
            frame.pack();
        } else {
            frame.validate();
        }

        // Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }

    /**
     * Application entry point.
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.
                                             getSystemLookAndFeelClassName());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

                new Application();
            }
        });
    }
}

 

窗口1代码:

 

package 互相调用菜单;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Rectangle;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Frame1 extends JFrame {
    JPanel contentPane;
    JButton btn = new JButton();
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(400, 300));
        setTitle("我是窗口1");
        btn.setBounds(new Rectangle(119, 141, 99, 25));
        btn.setText("调用窗口2");
        btn.addActionListener(new Frame1_btn_actionAdapter(this));
        contentPane.add(btn);
    }

    public void btn_actionPerformed(ActionEvent e) {
        //Frame2 f2Obj=new Frame2(this);//改动构造方法
        /*
        Frame2 f2Obj=new Frame2();//不改动构造方法(这行和下行一组)
        f2Obj.f1Obj=this;
        */
        Frame2 f2Obj=new Frame2();
        f2Obj.getFrame1Object(this);
        f2Obj.setSize(400,300);
        f2Obj.setTitle("我是窗口2");
        f2Obj.setLocation(this.getX(),this.getY());
        f2Obj.setVisible(true);
    }

    public  Object getObjectOfFram1(){
        return this;
    }
}


class Frame1_btn_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_btn_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btn_actionPerformed(e);
    }
}

 

窗口2代码:

 

package 互相调用菜单;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Rectangle;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Frame2 extends JFrame {
    Frame1 f1Obj;
    JTextField txt = new JTextField();
    JButton btn = new JButton();
    public Frame2() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    public Frame2(Frame1 f) {
    f1Obj=f;
    try {
        jbInit();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}


    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        txt.setText("");
        txt.setBounds(new Rectangle(116, 26, 104, 22));
        btn.setBounds(new Rectangle(118, 146, 125, 25));
        btn.setText("返回窗口1的数据");
        btn.addActionListener(new Frame2_btn_actionAdapter(this));
        this.getContentPane().add(txt);
        this.getContentPane().add(btn);
    }

    public void btn_actionPerformed(ActionEvent e) {
        String str=f1Obj.getTitle();
        txt.setText(str);
    }
   
    public void getFrame1Object(Frame1 fObj){
        f1Obj=fObj;
    }
}


class Frame2_btn_actionAdapter implements ActionListener {
    private Frame2 adaptee;
    Frame2_btn_actionAdapter(Frame2 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {

        adaptee.btn_actionPerformed(e);
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:164304次
    • 积分:1678
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:49篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论