简体   繁体   中英

passing values from one jInternalFrame to another jInternalFrame

有人能帮忙将值从一个jInternalFrame1传递给另一个jInternalFrame2吗?我无法在jInternalFrame2中创建jInternalFrame1的对象。

"can you provide code for this data Model?"

The second internal frame accepts a DataModel object. The two will remain the same object when between the frames.

Note if you need something more complex (like back and forth interaction between the frames), you should look into some tutorial on Model View Controller architecture , where you will need to use PropertyChaneListeners and such

public class DataModel {
    private String data;

    public DataModel() {
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }    
}

public class MyInternalFrame1 extends JInternalFrame {
    private DataModel dataModel = new DataModel();

    public DataModel getDataModel() {
        return dataModel;
    }

}

public class MyInternalFrame2 extends JInternalFrame {
    private DataModel dataModel;

    public MyInternaFrame1() {}

    public MyIntenalFrame2(DataModel datModel) {
        this.dataModel = dataModel;
    }

    public void setDataModel(DataModel dataModel) {
        this.dataModel = dataModel;
    }
}

In the Main GUI program, you can do something like this

public class GUI extends JFrame {
    MyInternalFrame1 iFrame1 = new MyInternalFrame1();

    ....
    // somewhere else in code
    DataModel dataModel = iFrame1.getDataModel();
    dataModel.setData("Hello");
    new MyInternalFrame2(dataModel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM