从动态类引用动态设置器 [英] Referencing a dynamic setter from a dynamic class

查看:138
本文介绍了从动态类引用动态设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图引用一个安装程序...我收到帮助,并在解决问题之前选择了答案....看到这里:

所以,我在做的是一个简单的方法,它使用一个setter-from-outside-a-

  

public class Log {
private MainForm mainForm; // MainForm变量

public Log(MainForm mainForm){
//将MainForm变量设置为其构造函数中的正确引用
this.mainForm = mainForm;
}

private void consoleOut(String data){
System.out.println(data);
if(mainForm!= null){
//现在我们可以使用传入的引用
mainForm.setConsoleText(data);
}
}
}

  public class MainForm extends FrameView {
public MainForm(SingleFrameApplication app){
super ;
........... CUT FOR LENGTH .................
public void setConsoleText(String Text){
jTextArea2.append(Text);
}

b

由于某种原因,MainForm在Log类中总是出现null。



如何获得对我的主窗体的引用?



Meh ...我只是使用一个静态文本框和一个静态设置器....仍在寻找一个更好的主意。

解决方案

唯一的解释是,当你实例化Log时,你传递一个null到构造函数。您是否在分配之前调用新的日志(mainform)

  //不这样做
private Log log = new Log(mainForm);

private MainForm mainForm = new MainForm();

检查您的对象的构造顺序。


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);
    }

edited for simplicity sake.

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.

解决方案

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();

Check the order of construction of your objects.

这篇关于从动态类引用动态设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆