如何从静态类更新文本框?
这让我抓狂。我有一个基于文本的工作应用程序。它有很多变量,现在需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
textField1
是父类的属性,则update
方法不应该是static
。这当然意味着您需要将该方法应用于 ODB2tunerConsole 对象的实例。Java 中的规则是静态方法不能通过显式使用类实例的引用来访问其类的非静态属性和方法。
这导致刚接触 Java 面向对象编程的人尝试将所有内容都静态化。但正如你所看到的,这会带来麻烦。正确的解决方案是将静力学的使用限制在真正需要的情况下。它们如下:
public static final String FOO = "foo";
Assuming that
textField1
is an attribute of the parent class, theupdate
method should not bestatic
. That of course means that you need to apply the method to an instance of theODB2tunerConsole
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:
public static final String FOO = "foo";
如果您为 Log 提供对 OBD2nerConsole 实例的静态引用,并从 update(String) 中删除 static,您应该能够更新textField1。
修改后的
Log.java
列表:Updatable.java
列表:ODB2nerConsole.java
修改后代码片段列表:Updatables 列表。 java:
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
:Listing of
Updatable.java
:Listing of modified snippet of
ODB2nerConsole.java
:Listing of
Updatables.java
: