java中的字符串或整数变量监听器?
首先,我对 java 还很陌生,我只是想了解静态类如何与非静态类交互以及如何获取静态类来更新文本框。
我一直在寻找信息,但我什么也找不到。我需要一个可变监听器。这就像我想要做的事情:
public class Class1{
public static int X;
public static void Process(){
for (true){
X = X + 1;
}
}
然后我有另一个类,我想将变量绑定到文本框,
Public class Class2{
****** On Class1.X.changeValue { Form.jLabel1.setText(Class1.X) }
}
我希望我清楚我想要做什么。我正在尝试将标签绑定到变量。
First off, I am pretty new to java and I am just staring to get the hang of how static classes can interface with non-static classes and how to get a static class to update a textbox.
I keep searching for information, but I cannot find anything. I need a variable listener. here is something like what I am trying to do:
public class Class1{
public static int X;
public static void Process(){
for (true){
X = X + 1;
}
}
Then I have another class where I want to bind a variable to a textbox
Public class Class2{
****** On Class1.X.changeValue { Form.jLabel1.setText(Class1.X) }
}
I hope I was clear on what I am trying to do. I am trying to bind a label to a variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用观察者设计模式
you can use Observer Design pattern
在 Java 中,没有特定于语言的方法来监听变量。您需要做的是,当您的代码更改变量时,然后让它也更新 JLabel。
您可以在按钮和其他 UI 小部件上设置侦听器,它们可能会更新 JLabel。
您可以实现此目的的一种方法如下。你知道 getter 和 setter 吗?它们是获取和设置实例变量的方法。
你真的应该尝试尽可能减少静态的使用,他们倾向于鼓励“全局变量”
In Java there is no language specific way to have listeners on variables. What you will need to do is when your code changes the variable then have it also update the JLabel.
You can have listeners on buttons and other UI widgets and they might update the JLabel.
One way you might implement this is as follows. Do you know about getters and setters ? They are methods that do the getting and setting of instance variables.
You should really try to minimize the use of statics as much as possible, They tend to encourage "Global Variables"
Java 本身不支持绑定作为语言功能。
JavaFX 确实如此,当然,它与 Java 代码的交互非常顺畅。
Java itself doesn't support binding as a language feature.
JavaFX does, and it interfaces with Java code very smoothly, of course.
您应该为您的类指定一个适当的名称(而不是
Class1
),以便您的意图变得清晰。也许你想要一个计数器?:在普通的Java代码中你不能直接监听变量。但是,如果您使用适当的修改方法(在本例中为
increase()
)将该变量放入一个简单对象中,则可以从此方法调用侦听器。要调用侦听器,您必须以某种方式注册它。这通常使用简单的
List
实现,其接口由addListener(Listener)
和removeListener(Listener)
两个方法组成。代码>.在 AWT 和 Swing 中随处可见这种模式。我在
Counter
类中定义了Listener
接口,因为该接口在该类之外没有太多价值,而且我不想将其称为CounterListener
。这样,到处乱飞的.java
文件就会减少。在实际程序中使用该计数器可能如下所示:
我首先创建了一个计数器,然后向其添加了一个侦听器。表达式 new Counter.Listener() { ... } 是一个匿名类定义,它也经常出现在 Java GUI 编程中。
因此,如果您认真对待 GUI 编程,您无论如何都需要学习这些概念和实现技术(将变量封装在类中、添加侦听器代码、定义侦听器、从修改变量的代码中调用侦听器)。
You should give your class a proper name (not
Class1
), so that your intention becomes clear. Maybe you want to have a counter?:In ordinary Java code you cannot listen to a variable directly. But if you put that variable into a simple object with proper modification methods (
increase()
in this case), you can call the listeners from this method.To call a listener you have to somehow register it. This is usually implemented with a simple
List<Listener>
, and the interface to that consists of the two methodsaddListener(Listener)
andremoveListener(Listener)
. You can find this pattern everywhere in AWT and Swing.I have defined the
Listener
interface inside theCounter
class because this interface doesn't have much value outside that class, and I didn't want to call itCounterListener
. That way, there are fewer.java
files flying around.Using that counter in an actual program may look like this:
I first created a counter and then added a listener to it. The expression
new Counter.Listener() { ... }
is an anonymous class definition, which also appears often in Java GUI programming.So if you are serious about GUI programming you need to learn these concepts and implementation techniques (encapsulating a variable in a class, adding the listener code, defining a listener, calling the listener from the code that modifies the variable) anyway.