简体   繁体   English

如何将信息从一个对象传递到另一个

[英]How do I pass information from one object to another

There's a main JFrame MainPanel using two Internal Frames Read and View , There's a JList of items in Read which is populated by files and a button. 有一个使用两个内部框架ReadView的主JFrame MainPanel ,在Read有一个JList项,由文件和一个按钮填充。

Expected working : Read has a JList populated with files. 预期的工作Read有一个JList,其中填充了文件。 I'll select a .txt file and press the button and the name of the file (actually I wanted to output the content but let's output the name of the file for simplicity) should be outputted in the JTextArea of View JInternalFrame. 我将选择一个.txt文件,然后按一下按钮,文件名(实际上我想输出内容,但是为了简单起见,让我们输出文件名)应该在View JInternalFrame的JTextArea中输出。

What's Wrong : 有什么问题

  1. The View being initialised in MainPanel , can't be reinitialised in Read which pops up the error. MainPanel中初始化的View不能在Read中重新初始化,这会弹出错误。
  2. If I use View viewPanel = new View() in the Read JInternalFrame's click button event , the JInternalFrame is'nt displayed at all. 如果我在Read JInternalFrame的click按钮事件中使用View viewPanel = new View() ,则根本不会显示JInternalFrame。

MainPanel: 主面板:

public MainPanel() {
    initComponents();
    pack();
    insert = new Insert();
    view = new View();
    read = new Read();
    jPanel2.add(insert);
    jPanel2.add(view);
    jPanel2.add(read);
    insert.setVisible(true); // Initially only insert is visible later on
    view.setVisible(false);  // I have buttons which set's the view and
    read.setVisible(false);  // read visible

}

Read : 阅读:

public class Read extends javax.swing.JInternalFrame {
public Read() {
    initComponents();
    pack();
    DefaultListModel model = new DefaultListModel();
    jList1.setModel(model);
    File folder = new File("/home/Naruto/Dattebayo");
    File[] listOfFiles = folder.listFiles();
    for(File file : listOfFiles){
        model.addElement(file.getName());

    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String selected = (String) jList1.getSelectedValue();

    view.setText(selected); // **Error** 
    view.setVisible(true);  // **View is'nt visible here**
    this.setVisible(false);       
}                                        
}

You ask: 你问:

How do I initialize an JInternalFrame from another JInternalFrame? 如何从另一个JInternalFrame初始化JInternalFrame?

But your real overall question, in a nutshell is in fact: 但实际上,您真正的总体问题实际上是:

How do I pass information from one object to another. 如何将信息从一个对象传递到另一个。

That's it, and really this has little to do with Swing or GUI (for the initial part of my answer), but is rather a basic Java question. 就是这样,实际上这与Swing或GUI无关(对于我的回答的最初部分),但这只是一个基本的Java问题。 The problem you're seeing is that if you create a new View object inside of your Read object, you can change the state of this View instance, but it will have no effect on the visualized View instance because they are two completely unique and distinct objects. 您看到的问题是,如果您在Read对象中创建了一个新的View对象,则可以更改此View实例的状态,但是它对可视化的View实例没有任何影响,因为它们是两个完全唯一且不同的对象。

One BAD solution is to make key fields and methods in View static which would allow you to pass information from one object to another using class-level fields and methods, but this breaks OOP design, would increase the cyclomatic complexity of your code making it much harder to debug or to enhance or expand. 一个BAD解决方案是将View中的关键字段和方法设为静态,这将使您可以使用类级字段和方法将信息从一个对象传递到另一个对象,但这会破坏OOP设计,这会增加代码的循环复杂性,使其变得更加复杂。难以调试或增强或扩展。

A better solution is to simply pass your visualized View instance , the one that is in fact being displayed into the Read instance, and then call methods on it. 更好的解决方案是简单地传递可视化的View实例 ,实际上将其显示在Read实例中,然后在其上调用方法。 For example: 例如:

public class Read extends javax.swing.JInternalFrame {
    private View view;  // *******

    // give the constructor a View parameter
    public Read(View view) {

        this.view = view; // ***** update the view field

        initComponents();
        pack();
        DefaultListModel model = new DefaultListModel();
        jList1.setModel(model);
        File folder = new File("/home/Naruto/Dattebayo");
        File[] listOfFiles = folder.listFiles();
        for(File file : listOfFiles){
            model.addElement(file.getName());

        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String selected = (String) jList1.getSelectedValue();

        // this should work now
        view.setText(selected); 
        view.setVisible(true);  
        this.setVisible(false);       
    }                                        
}

and then pass the view in when both classes are created 然后在创建两个类时传递视图

public MainPanel() {
    initComponents();
    pack();
    insert = new Insert();
    view = new View();
    read = new Read(view); // *********
    jPanel2.add(insert);
    jPanel2.add(view);
    jPanel2.add(read);
    insert.setVisible(true); // Initially only insert is visible later on
    view.setVisible(false);  // I have buttons which set's the view and
    read.setVisible(false);  // read visible

}

Note that a better overall solution, one that does take into account that yours is a GUI program, is to change the structure of your program to a Model-View-Control, or MVC, structure (I urge you to Google this). 请注意,一个更好的整体解决方案是将您的程序结构更改为“模型-视图-控件”或“ MVC”结构(请考虑使用Google),这确实要考虑到您是GUI程序。 The advantage to doing this is that while it would initially seem to increase the complexity of your code, it scales much nicer, and in the long run decreased the code's complexity, greatly decreases coupling and improves cohesion (which is mostly why it reduces complexity) and makes it much easier to debug since your separate parts of the program would be isolated separately testable units. 这样做的好处是,虽然它最初看起来似乎会增加代码的复杂性,但是它的伸缩性要好得多,从长远来看,它降低了代码的复杂性, 大大减少了耦合并提高了内聚性(这就是为什么它降低了复杂性)由于程序的各个部分将被隔离为可单独测试的单元,因此调试起来变得更加容易。

我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity

暂无
暂无

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

相关问题 如何将信息从一个 class 传递到另一个 - how do I pass information from one class to another 如何在Java中将在一个类中创建的对象传递给另一个类? - How do I pass an object created in one class to another in java? 我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 如何将int值从一种方法传递给另一种方法 - How do I pass int values from one method to another 如何将值从一类传递到另一类? - How do I pass the values from one class to another? 我如何将变量从一个jframe传递到另一个jframe - how do i pass variable from one jframe to another jframe 如何将2个数组从一个类传递到另一个类 - How do I pass 2 arrays from one class to another 如何将另一个类中的对象传递给paintComponent方法? - How do I pass an object from another class into the paintComponent method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM