从动态类引用动态设置器 [英] Referencing a dynamic setter from a dynamic class
问题描述
我正在尝试引用一个setter ...我收到帮助,并在解决问题之前选择了答案....看到这里:在表单外使用setter?
I am trying to reference a setter... I received help and selected the answer too soon before resolving the issue.... see here: Using a setter from outside a form?
所以,我在做什么这个...数据进入日志,它被解析,然后它返回到显示的表单。
So, what I'm doing is this... Data goes into the log and it gets parsed, then it goes back to the form where it is displayed.
public class Log {
private MainForm mainForm; // our MainForm variable
public Log(MainForm mainForm) {
// setting the MainForm variable to the correct reference in its constructor
this.mainForm = mainForm;
}
private void consoleOut(String data) {
System.out.println(data);
if (mainForm != null) {
// now we can use the reference passed in.
mainForm.setConsoleText("data");
}
}
}
这是我表单中的设置者。
Here's the setter in my form.
public class MainForm extends FrameView {
public MainForm(SingleFrameApplication app) {
super(app);
...........CUT FOR LENGTH.................
public void setConsoleText(String Text){
jTextArea2.append(Text);
}
为简单起见,编辑
由于某种原因,MainForm在Log类中总是出现null。
For some reason MainForm always comes out null in the Log class.
如何获得对我的主窗体的引用?
How can I get a reference to my Main form?
嗯...我刚刚去了一个静态文本框和静态设置器....仍在寻找一个更好的主意。
Meh... I just went with a static textbox and a static setter.... Still looking for a better idea.
推荐答案
唯一的解释是,当您实例化日志时,您将传递一个null到构造函数。在 mainform被分配之前,您是否正在调用新的日志(主窗体)
?
The only explanation is that when you are instantiating Log you are passing a null to the constructor. Are you calling new Log(mainform)
before mainform has been assigned?
// Don't do this
private Log log = new Log(mainForm);
private MainForm mainForm = new MainForm();
检查对象的构造顺序。
这篇关于从动态类引用动态设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!