Java 计时器和计时器任务:访问计时器外部的变量 [英] Java Timer and Timer Task : Accessing variables outside Timer

查看:57
本文介绍了Java 计时器和计时器任务:访问计时器外部的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要课程中:

public class Main{
    public static void main(String[] args) {
    //some code
    final int number = 0;


    numberLabel.setText(number);

    Timer t = new Timer();

        t.scheduleAtFixedRate(new TimerTask(){
           public void run(){
           //elapsed time
               number = number + 1;
           }

        }, 1000, 1000);

   }

}

我正在使用最终的 int 变量 number 将经过的时间显示到标签 numberLabel 中.但我无法访问计时器内的最终 int 变量,错误提示:

I am using the final int variable number to display into a label numberLabel the elapsed time. But i cannot access the final int variable inside the timer, error says:

无法分配最终局部变量编号,因为它是在封闭类型中定义的"

我知道我可以在 run() 中直接使用 numberLabel.setText() 更新标签,但是我需要 number 变量来计算一些时间.如何更新 number 变量?谢谢

I know i can update the label directly using numberLabel.setText() inside the run() however i need the number variable for some computations of time. How do i update the number variable? Thank you

推荐答案

您应该将 number 声明为类字段,而不是方法的局部变量.这样它就不需要是 final 的,可以在匿名内部类中使用.

You should declare number as a class field, not a variable local to a method. This way it does not need to be final and can be used in anonymous inner classes.

我建议不要将其设为静态,并且不要在静态环境中使用 Timer,而是在实例世界中使用它.

I suggest that it not be made static and that you not use your Timer in a static environment but rather in the instance world.

public class Main{
    private int number = 0;

    public void someNonStaticMethod() {
      //some code
      // final int number = 0;

      numberLabel.setText(number);
      Timer t = new Timer();
      t.scheduleAtFixedRate(new TimerTask(){
           public void run(){
           //elapsed time
               number = number + 1;
           }

      }, 1000, 1000);
   }
}

<小时>

顺便说一句,您对 numberLabel.setText(...) 的使用表明这将用于 Swing GUI.如果是这样,那么不要使用 java.util.Timer,而应该使用 javax.swing.Timer 或 Swing Timer.


As an aside, your use of numberLabel.setText(...) suggests that this will be used in a Swing GUI. If so, then don't use a java.util.Timer but rather you should use a javax.swing.Timer or Swing Timer.

public class Main2 {
  public static final int TIMER_DELAY = 1000;
  private int number = 0;

  public void someMethod() {
    numberLabel.setText(String.valueOf(number));
    new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        number++;
        numberLabel.setText(String.valueOf(number));
      }
    }).start();
  }
}

同样,如果这是一个 Swing 应用程序(你不说),那么代码由在 Swing 事件线程 EDT(事件调度线程)上运行的 Timer 重复运行是至关重要的.java.util.Timer 不会这样做,而 Swing Timer 会这样做.

Again if this is a Swing application (you don't say), then it is critical that the code run repeatedly by the Timer run on the Swing event thread, the EDT (Event Dispatch Thread). The java.util.Timer does not do this while the Swing Timer does.

这篇关于Java 计时器和计时器任务:访问计时器外部的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆