如何从静态类更新文本框?

发布于 2024-09-10 10:05:22 字数 754 浏览 7 评论 0原文

这让我抓狂。我有一个基于文本的工作应用程序。它有很多变量,现在需要 GUI。我从基础开始。每当一些数据发送到我的日志时,我希望它显示在我的文本框中。

这是数据传递的统一入口点,可以对其进行操作。

public class Log {

    private static void consoleOut(String data) {
    System.out.println(data);
    OBD2nerConsole.update(data);
    }
      public static void level0(String data) {
    if (Status.ConsoleLevel >= 0) {
    consoleOut(data);

    }

这是我的表单,它有一个文本框和一些按钮。

public class OBD2nerConsole extends java.awt.Frame {

    public static void update(String data) {
        textField1.setText(textField1.getText() + data);
    }   

}

我遇到的问题是我正在使用静态和非静态我猜......文本框中没有显示任何内容。我一直在尝试并删除了所有错误,但它不起作用。我真的不知道该怎么办。看来这是最好的配置,因为没有错误,但文本框没有做任何事情。

我可能应该补充一点,这是我的第一个表格!

This is driving me nuts. I have a working text based application. It has many many variables which now need a GUI. I'm starting with the basics. Whenever some data is sent to my log, I want it to display in my textbox.

Here is a unified entry point for data to pass where it can be manipulated.

public class Log {

    private static void consoleOut(String data) {
    System.out.println(data);
    OBD2nerConsole.update(data);
    }
      public static void level0(String data) {
    if (Status.ConsoleLevel >= 0) {
    consoleOut(data);

    }

This is my form and it has a text box and a few buttons.

public class OBD2nerConsole extends java.awt.Frame {

    public static void update(String data) {
        textField1.setText(textField1.getText() + data);
    }   

}

The prolem I am having is that I am working with a static and non-static I guess.. There is nothing displayed in the text box. I kept playing around and removed all of the errors but it is not working. I don't really know what to do. It seems that this is the best configuration, because there are no errors, but the text box is not doing anything.

I should probly add that this is my first form EVER!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深陷 2024-09-17 10:05:22

假设 textField1 是父类的属性,则 update 方法不应该是static。这当然意味着您需要将该方法应用于 ODB2tunerConsole 对象的实例。

Java 中的规则是静态方法不能通过显式使用类实例的引用来访问其类的非静态属性和方法。

这导致刚接触 Java 面向对象编程的人尝试将所有内容都静态化。但正如你所看到的,这会带来麻烦。正确的解决方案是将静力学的使用限制在真正需要的情况下。它们如下:

  • 共享常量;例如 public static final String FOO = "foo";
  • 仅依赖于其参数状态的辅助方法。
  • 对全局数据结构的隐藏引用,使用“单例”模式公开(如果需要)。

Assuming that textField1 is an attribute of the parent class, the update method should not be static. That of course means that you need to apply the method to an instance of the ODB2tunerConsole object.

The rule in Java is that a static method cannot access the non-static attributes and methods of its class with explicitly using a reference to an instance of the class.

This leads people who are new to object-oriented programming in Java to try to make everything static. But as you see, that leads to trouble. The correct solution is to limit your use of statics to cases where they are really needed. These are as follows:

  • Shared constants; e.g. public static final String FOO = "foo";
  • Helper methods that depend only on the state of their arguments.
  • Hidden references to global data structures, exposed (if necessary) using the "singleton" pattern.
守望孤独 2024-09-17 10:05:22

如果您为 Log 提供对 OBD2nerConsole 实例的静态引用,并从 update(String) 中删除 static,您应该能够更新textField1

修改后的 Log.java 列表:

public class Log {
    private static Updatable console = Updatables.getUpdatable();

    private static void consoleOut(String data) {
        System.out.println(data);
        console.update(data);
    }

    public static void level0(String data) {
        if (Status.ConsoleLevel >= 0) {
            consoleOut(data);
        }
    }
}

Updatable.java 列表:

public interface Updatable {
    void update(String);
}

ODB2nerConsole.java 修改后代码片段列表:

public class OBD2nerConsole extends java.awt.Frame implements Updatable {
    @Override
    public void update(String data) {
        textField1.setText(textField1.getText() + data);
    }
}

Updatables 列表。 java:

public class Updatables {
    public Updatable getUpdatable() {
        return new ODB2nerConsole();
    }
}

If you give Log a static reference to an instance of OBD2nerConsole and remove static from update(String) you should be able to update the textField1.

Listing of modified Log.java:

public class Log {
    private static Updatable console = Updatables.getUpdatable();

    private static void consoleOut(String data) {
        System.out.println(data);
        console.update(data);
    }

    public static void level0(String data) {
        if (Status.ConsoleLevel >= 0) {
            consoleOut(data);
        }
    }
}

Listing of Updatable.java:

public interface Updatable {
    void update(String);
}

Listing of modified snippet of ODB2nerConsole.java:

public class OBD2nerConsole extends java.awt.Frame implements Updatable {
    @Override
    public void update(String data) {
        textField1.setText(textField1.getText() + data);
    }
}

Listing of Updatables.java:

public class Updatables {
    public Updatable getUpdatable() {
        return new ODB2nerConsole();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文