java java中的字符串或整数变量侦听器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3274211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:06:31  来源:igfitidea点击:

String or Integer variable Listener in java?

javatextvariableslabelbind

提问by Adam Outler

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.

首先,我对 Java 还很陌生,我只是想了解静态类如何与非静态类交互以及如何获取静态类来更新文本框。

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.

我希望我清楚我要做什么。我正在尝试将标签绑定到变量。

采纳答案by Romain Hippeau

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.

在 Java 中,没有语言特定的方式来监听变量。您需要做的是当您的代码更改变量时,然后让它也更新 JLabel。
您可以在按钮和其他 UI 小部件上使用侦听器,它们可能会更新 JLabel。
您可以实现的一种方法如下。你知道 getter 和 setter 吗?它们是获取和设置实例变量的方法。

private int x;

public int getX()
{
    return x;
}

public void setX(int anX)
{
    x = anX;
    updateLabel("This is x:" + anX)
}

public void process()
{
    while(true)
    {
        int anX = getX();
        setX(anX + 1);
    }
}

You should really try to minimize the use of statics as much as possible, They tend to encourage "Global Variables"

你真的应该尽可能地尽量减少静态的使用,他们倾向于鼓励“全局变量”

回答by wizztjh

回答by erickson

Java itself doesn't support binding as a language feature.

Java 本身不支持绑定作为语言功能。

JavaFX does, and it interfaces with Java code very smoothly, of course.

JavaFX 确实如此,当然,它与 Java 代码的接口非常流畅。

回答by Roland Illig

You should give your class a proper name (not Class1), so that your intention becomes clear. Maybe you want to have a counter?:

你应该给你的班级一个合适的名字(不是Class1),这样你的意图就会变得清晰。也许你想要一个计数器?:

package so3274211;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Counter {
  private int value = 0;
  private final List<Listener> listeners = new CopyOnWriteArrayList<Listener>();

  private void fireAfterValueChanged() {
    for (Listener listener : listeners) {
      listener.afterValueChanged(this);
    }
  }

  public int getValue() {
    return value;
  }

  public void increment() {
    value++;
    fireAfterValueChanged();
  }

  public void addListener(Listener listener) {
    listeners.add(listener);
  }

  public void removeListener(Listener listener) {
    listeners.remove(listener);
  }

  public interface Listener {
    void afterValueChanged(Counter 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.

在普通 Java 代码中,您不能直接侦听变量。但是,如果您将该变量放入具有适当修改方法的简单对象中(increase()在本例中),则可以从该方法调用侦听器。

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 methods addListener(Listener)and removeListener(Listener). You can find this pattern everywhere in AWT and Swing.

要调用侦听器,您必须以某种方式注册它。这通常用一个简单的 实现List<Listener>,并且它的接口由两个方法addListener(Listener)removeListener(Listener). 您可以在 AWT 和 Swing 中随处找到这种模式。

I have defined the Listenerinterface inside the Counterclass because this interface doesn't have much value outside that class, and I didn't want to call it CounterListener. That way, there are fewer .javafiles flying around.

ListenerCounter类内部定义了接口,因为这个接口在类之外没有太多价值,我不想调用它CounterListener。这样一来,到处.java乱飞的文件就少了。

Using that counter in an actual program may look like this:

在实际程序中使用该计数器可能如下所示:

package so3274211;

public class Main {

  public static void main(String[] args) {
    Counter c = new Counter();
    c.increment();
    c.addListener(new Counter.Listener() {
      @Override
      public void afterValueChanged(Counter counter) {
        System.out.println(counter.getValue());
      }
    });
    c.increment();
  }

}

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.

我首先创建了一个计数器,然后向它添加了一个侦听器。表达式new Counter.Listener() { ... }是一个匿名类定义,它也经常出现在 Java GUI 编程中。

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.

因此,如果您认真对待 GUI 编程,则无论如何都需要学习这些概念和实现技术(将变量封装在类中、添加侦听器代码、定义侦听器、从修改变量的代码中调用侦听器)。