【问题标题】:javafx progressbar bind color dynamically to progressjavafx进度条动态绑定颜色到进度
【发布时间】:2018-09-25 15:12:51
【问题描述】:

所以我需要为 ProgressIndicator/Bar 制作一个计时器。我用时间线制作了“动画”,但我不知道如何更改 ProgressIndicator 的颜色?

private ProgressIndicator progress = new ProgressIndicator();
private Timeline timeline = new Timeline();

public void doTime(int sec){

    Timeline time = new Timeline(

    new KeyFrame(
            Duration.ZERO,       
            new KeyValue(progress.progressProperty(), 1)
    ),
    new KeyFrame(
            Duration.seconds(sec), 
            new KeyValue(progress.progressProperty(), 0)
    )
);
time.setCycleCount(1);
time.play();
}

如果进度为 0.33,我想要红色。我没有找到任何进展的听众。

if (progress.getProgress() <= 0.33) {
     progress.setStyle("-fx-progress-color: red;");

}

你有什么想法可以实现吗?

【问题讨论】:

    标签: css javafx progress-bar progress progress-indicator


    【解决方案1】:

    您可以向进度指示器的progressProperty() 添加监听器:

    progress.progressProperty().addListener((obs, oldProgress, newProgress) -> {
        if (newProgress <= 0.33) {
            progress.setStyle("-fx-progress-color: red;");
    
        } else {
            progress.setStyle("");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-09-19
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      相关资源
      最近更新 更多