JavaFX每秒更改标签的文本

问题描述 投票:-1回答:1

我想向用户显示其每秒的点击次数(CPS),因此我想将显示点击次数的标签设置为每秒0。问题是我需要将标签设置为静态,这是不工作。这是我的代码:

 public class Controller{

    public  Button clickButton;
    public  Label cpsLabel;
    public  Label highscoreLabel;
    static int count = 0;
//  static int highscoreInt = 0;
    public void handleButtonClick() {
        count++;
        cpsLabel.setText("CPS: " + count);
    }
    public static void Timer() {
        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              Controller.count = 0;
              cpsLabel.setText("CPS: " + count);
          }
        }, 0, 1, TimeUnit.SECONDS);
        }

}
javafx timer static label runnable
1个回答
0
投票

您不应该在Java FX主线程之外修改JavaFX组件,甚至不要在组件上放置静态的。请检查以下有关Concurrency in JavaFX的文章

© www.soinside.com 2019 - 2024. All rights reserved.