简体   繁体   English

将值从一个jInternalFrame传递到另一个jInternalFrame

[英]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. 第二个内部框架接受一个DataModel对象。 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 请注意,如果您需要更复杂的内容(例如框架之间的来回交互),则应查看有关Model View Controller体系结构的一些教程,在该教程中,您将需要使用PropertyChaneListeners等。

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 在主GUI程序中,您可以执行以下操作

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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