JavaFX如何动态改变ProgressBar颜色

JavaFX如何动态改变ProgressBar颜色

本文介绍了JavaFX如何动态改变ProgressBar颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在。






有一个解决方法,您可以使用,直到错误修复您的问题中的示例代码



此答案中的代码会提供,然后动态修改进度条的栏颜色为您喜欢的任何值。

  import javafx.application.Application; 
import javafx.beans.value * *;
import javafx.geometry.Pos;
import javafx.scene。*;
import javafx.scene.control。*;
import javafx.scene.layout。*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ProgressBarDynamicColor extends Application {
public static void main(String [] args){launch(args); }
@Override public void start(Stage Stage){
PickedColorBar aquaBar = new PickedColorBar(0.4,Color.AQUA);
PickedColorBar fireBar = new PickedColorBar(0.6,Color.FIREBRICK);

HBox layout = new HBox(20);
layout.getChildren()。setAll(aquaBar,fireBar);
layout.setStyle( - fx-background-color:-fx-box-border,cornsilk; -fx-padding:15;);

stage.setScene(new Scene(layout));
stage.show();

aquaBar.wasShown();
fireBar.wasShown();
}

类PickedColorBar扩展VBox {
private final ProgressBar bar;
private final ColorPicker picker;

private boolean wasShownCalled = false;

final ChangeListener< Color> COLOR_LISTENER = new ChangeListener< Color>(){
@Override public void changed(ObservableValue< ;? extends Color> value,Color oldColor,Color newColor){
setBarColor(bar,newColor);
}
};

public PickedColorBar(double progress,Color initColor){
bar = new ProgressBar(progress);
picker = new ColorPicker(initColor);

setSpacing(10);
setAlignment(Pos.CENTER);
getChildren()。setAll(bar,picker);
}

//仅在进度条显示在舞台上后才调用。
public void isShown(){
if(!wasShownCalled){
wasShownCalled = true;
setBarColor(bar,picker.getValue());
picker.valueProperty()。addListener(COLOR_LISTENER);
}
}

private void setBarColor(ProgressBar bar,Color newColor){
bar.lookup(。bar)。setStyle( - fx-background -color:-fx-box-border,+ createGradientAttributeValue(newColor));
}

private String createGradientAttributeValue(Color newColor){
String hsbAttribute = createHsbAttributeValue(newColor);
returnlinear-gradient(to bottom,deriv(+ hsbAttribute +,30%)5%,deriv(+ hsbAttribute +,-17%))
}

private String createHsbAttributeValue(Color newColor){
return
hsb(+
(int)newColor.getHue +
(int)(newColor.getSaturation()* 100)+%,+
(int)(newColor.getBrightness()* 100)+%);
}
}
}

的css属性来操作Region背景。未来的JavaFX版本(例如JDK8 +)将包含一个公共Java API来操作背景属性,从而废弃了来自Java程序的属性的字符串处理。



示例程序输出: / p>


I was trying to solve my problem with colored progress bars in this thread. The solution was present, but then I ran into another problem: I can't change color dynamically from my code. I want to do it right from my code, not with pre-defined .css. Generally I can do it, but I run into some difficulties when I try to do it with more than one progess bar.

public class JavaFXApplication36 extends Application {

    @Override
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();
        ProgressBar pbRed = new ProgressBar(0.4);
        ProgressBar pbGreen = new ProgressBar(0.6);
        pbRed.setLayoutY(10);
        pbGreen.setLayoutY(30);

        pbRed.setStyle("-fx-accent: red;");       // line (1)
        pbGreen.setStyle("-fx-accent: green;");   // line (2)

        root.getChildren().addAll(pbRed, pbGreen);
        Scene scene = new Scene(root, 150, 50);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I always get 2 red progressbars with it! It seems that code in line (1) changes the style of ProgressBar class, not the instance.

Another strange moment is that deleting line (1) don't result in 2 green progress bars. So I can figure that line (2) is completely useless!! WHY?! That's definitely getting odd.

Is there any way to set different colors for separate progressbars?

解决方案

See also the StackOverflow JavaFX ProgressBar Community Wiki.


There is a workaround you can use until a bug to fix the sample code in your question is filed and fixed.

The code in this answer does a node lookup on the ProgressBar contents, then dynamically modifies the bar colour of the progress bar to any value you like.

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ProgressBarDynamicColor extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    PickedColorBar aquaBar = new PickedColorBar(0.4, Color.AQUA);
    PickedColorBar fireBar = new PickedColorBar(0.6, Color.FIREBRICK);

    HBox layout = new HBox(20);
    layout.getChildren().setAll(aquaBar, fireBar);
    layout.setStyle("-fx-background-color: -fx-box-border, cornsilk; -fx-padding: 15;");

    stage.setScene(new Scene(layout));
    stage.show();

    aquaBar.wasShown();
    fireBar.wasShown();
  }

  class PickedColorBar extends VBox {
    private final ProgressBar bar;
    private final ColorPicker picker;

    private boolean wasShownCalled = false;

    final ChangeListener<Color> COLOR_LISTENER = new ChangeListener<Color>() {
      @Override public void changed(ObservableValue<? extends Color> value, Color oldColor, Color newColor) {
        setBarColor(bar, newColor);
      }
    };

    public PickedColorBar(double progress, Color initColor) {
      bar    = new ProgressBar(progress);
      picker = new ColorPicker(initColor);

      setSpacing(10);
      setAlignment(Pos.CENTER);
      getChildren().setAll(bar, picker);
    }

    // invoke only after the progress bar has been shown on a stage.
    public void wasShown() {
      if (!wasShownCalled) {
        wasShownCalled = true;
        setBarColor(bar, picker.getValue());
        picker.valueProperty().addListener(COLOR_LISTENER);
      }
    }

    private void setBarColor(ProgressBar bar, Color newColor) {
      bar.lookup(".bar").setStyle("-fx-background-color: -fx-box-border, " + createGradientAttributeValue(newColor));
    }

    private String createGradientAttributeValue(Color newColor) {
      String hsbAttribute = createHsbAttributeValue(newColor);
      return "linear-gradient(to bottom, derive(" + hsbAttribute+ ",30%) 5%, derive(" + hsbAttribute + ",-17%))";
    }

    private String createHsbAttributeValue(Color newColor) {
      return
        "hsb(" +
          (int)  newColor.getHue()               + "," +
          (int) (newColor.getSaturation() * 100) + "%," +
          (int) (newColor.getBrightness() * 100) + "%)";
    }
  }
}

The code uses inlined string processing of css attributes to manipulate Region backgrounds. Future JavaFX versions (e.g. JDK8+) will include a public Java API to manipulate background attributes, making obsolete the string processing of attributes from the Java program.

Sample program output:

这篇关于JavaFX如何动态改变ProgressBar颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:17