[英]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. 有一个使用两个内部框架
Read
和View
的主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 : 有什么问题 :
View
being initialised in MainPanel
, can't be reinitialised in Read
which pops up the error. MainPanel
中初始化的View
不能在Read
中重新初始化,这会弹出错误。 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.
这样做的好处是,虽然它最初看起来似乎会增加代码的复杂性,但是它的伸缩性要好得多,从长远来看,它降低了代码的复杂性, 大大减少了耦合并提高了内聚性(这就是为什么它降低了复杂性)由于程序的各个部分将被隔离为可单独测试的单元,因此调试起来变得更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.