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

发布于 2024-09-10 05:08:14 字数 430 浏览 5 评论 0原文

首先,我对 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 技术交流群。

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

发布评论

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

评论(4

泪冰清 2024-09-17 05:08:15

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

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

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.

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"

不离久伴 2024-09-17 05:08:15

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.

北风几吹夏 2024-09-17 05:08:15

您应该为您的类指定一个适当的名称(而不是 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);
  }

}

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

要调用侦听器,您必须以某种方式注册它。这通常使用简单的 List 实现,其接口由 addListener(Listener)removeListener(Listener) 两个方法组成。代码>.在 AWT 和 Swing 中随处可见这种模式。

我在 Counter 类中定义了 Listener 接口,因为该接口在该类之外没有太多价值,而且我不想将其称为 CounterListener。这样,到处乱飞的 .java 文件就会减少。

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

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

}

我首先创建了一个计数器,然后向其添加了一个侦听器。表达式 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?:

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.

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.

I have defined the Listener interface inside the Counter class 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 .java files flying around.

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.

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.

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