JavaFX如何动态更改ProgressBar颜色? [英] JavaFX How to change ProgressBar color dynamically?
问题描述
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);
场景场景=新场景(根,150,50);
primaryStage.setTitle(Hello World!);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我总是得到2个红色进度条吧!似乎行(1)中的代码改变了ProgressBar类的样式,而不是实例。
另一个奇怪的时刻是删除行(1)不会导致2个绿色进度条。所以我可以看出这条线(2)是完全没用的!为什么?!
有没有办法为单独的进度条设置不同的颜色?
另请参阅 StackOverflow JavaFX ProgressBar社区维基。
有一个解决方法可以使用,直到在您的问题中修复示例代码的错误提交并修复。
此答案中的代码执行 ProgressBar 内容的场景/ Node.html#lookup%28java.lang.String%29rel =nofollow noreferrer>节点查找,然后动态修改进度条的条形颜色为您喜欢的任何值。
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扩展VBox {
private final ProgressBar bar;
私人最终ColorPicker选择器
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 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);
returnlinear-gradient(to bottom,derived(+ hsbAttribute +,30%)5%,derived(+ hsbAttribute +,-17%))
}
private String createHsbAttributeValue(Color newColor){
return
hsb(+
(int)newColor.getHue()+ +
(int)(newColor.getSaturation()* 100)+%,+
(int)(newColor.getBrightness()* 100)+%
}
}
}
代码使用内联字符串处理的css属性来操纵区域背景。未来的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颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!