在JavaFX中同步倒数计时器的最佳方法 [英] Best way to synchronize countdowntimers in JavaFX

查看:304
本文介绍了在JavaFX中同步倒数计时器的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要9个倒数计时器。计时器由用户启动。在我的实现中,我为每个启动的计时器创建一个计时器类。 timerclass使用时间线。根据计时器的开始,秒是异步的。



我不确定如何继续。



我首先想到的是,所有倒计时都只使用一个时间轴。我将所有stringProperties放入列表中,时间轴将更改该属性。我不确定这是否是一个好方法吗?



在某些Google上,我发现有animationtimer可以用于解决此类问题。但是我听不懂这些例子。我必须覆盖handle方法。我应该如何用它更新计时器? > PauseTransition 或 TimeLine (1)更新所有计数器,如以下





(1)要使用 TimeLine 而不是 PauseTransition update()更改为:

  void update(){

时间轴时间轴=新时间轴();
timeline.setCycleCount(Animation.INDEFINITE);

KeyFrame keyFrame = new KeyFrame(
Duration.seconds(1),
event-> {updateCounters();}
);

timeline.stop();
timeline.getKeyFrames()。clear();
timeline.getKeyFrames()。add(keyFrame);
timeline.play();
}


My Programm needs nine Countdowntimers. The timers are started by the user. In my implementation I create a timerclasses for each timer started. The timerclass uses a timeline. Depending on the start of the timers the seconds are asynchrone.

I am not sure how to proceed.

My first thought were to use only 1 timeline for all countdowns. I would put all stringProperties into a list and the timeline will change the property. I am not so sure if this is a good way?

With some google I found out that there is animationtimer which could be used for such a problem. But I couldn't understand the examples. I have to overwrite the handle method. How should I update my timer with this?

解决方案

The idea is correct: use one animation tool such as PauseTransition or TimeLine (1) to update all counters as demonstrated in the following MRE:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SyncedCounters extends Application {

    private static final int MAX_COUNT = 100;
    private Map<Label, Integer> counters;
    private VBox countersPane;

    @Override public void start(Stage stage) throws IOException {

        counters = new HashMap<>();
        countersPane = new VBox();
        Button addCounter = new Button("Add Counter");
        addCounter.setOnAction(e->addCounter());
        BorderPane root = new BorderPane(countersPane, null, null, null, addCounter);
        stage.setScene(new Scene(new ScrollPane(root),250,200));
        stage.show();
        update();
    }

    private void update() {

        PauseTransition pause = new PauseTransition(Duration.seconds(1));
        pause.setOnFinished(event ->{
            updateCounters();
            pause.play();
        });
        pause.play();
    }

    private void addCounter() {

        Label label = new Label(String.valueOf(MAX_COUNT));
        label.setAlignment(Pos.CENTER);
        label.setPrefSize(150, 25);
        counters.put(label, MAX_COUNT);
        countersPane.getChildren().add(label);
    }


    private void updateCounters() {
        for(Label l : counters.keySet()){
            int counterValue = counters.get(l);
            if(counterValue > 0 ){
                counterValue--;
                l.setText(String.valueOf(counterValue));
                counters.put(l, counterValue);
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}



(1) To use TimeLine instead of PauseTransition change update() to :

void update() {

    Timeline timeline = new Timeline();
    timeline.setCycleCount(Animation.INDEFINITE);

    KeyFrame keyFrame = new KeyFrame(
            Duration.seconds(1),
            event -> {updateCounters();}
    );

    timeline.stop();
    timeline.getKeyFrames().clear();
    timeline.getKeyFrames().add(keyFrame);
    timeline.play();
}

这篇关于在JavaFX中同步倒数计时器的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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