我正在改变ProgressBar中的bar的颜色
pBar.setStyle("-fx-accent: green");
但我遇到了一个问题:这似乎不适合我! (或者我只是不明白的东西)
这里是代码:
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(); } }
我总是得到2个红色进度条吧!似乎行(1)中的代码改变了ProgressBar类的样式,而不是实例.
另一个奇怪的时刻是,删除行(1)不会导致2个绿色进度条.所以我可以看出这条线(2)是完全没用的!为什么?!这绝对是奇怪的.
有没有办法为单独的进度条设置单独的颜色?
解决方法
答案更新,添加一个简单的非动画示例与多个进度条
您的问题中的代码应显示两个不同的彩色进度条,事实上它不是JavaFX css处理系统中的错误.在这里记录运行时项目的错误:http://javafx-jira.kenai.com.
作为解决方法,而不是在进度条上调用setStyle,定义用于对样式表中的进度条进行颜色的重音颜色,并将样式类添加到进度条.然后,您可以在同一应用程序中创建多个进度条,全部具有不同的颜色.
正如Uluk指出的那样,您可以使用JavaFX 2.2 caspian.css与JavaFX 2 css reference guide和JavaFX 2 css tutorial一起来确定如何对事物进行风格化.
示例css:
/** progress.css place in same directory as ColoredProgressBarStyleSheet.java or SimpleColoredProgressBar.java ensure build system copies the css file to the build output path */ .root { -fx-background-color: cornsilk; -fx-padding: 15; } .progress-bar { -fx-Box-border: goldenrod; } .green-bar { -fx-accent: green; } .yellow-bar { -fx-accent: yellow; } .orange-bar { -fx-accent: orange; } .red-bar { -fx-accent: red; }
简单示例程序:
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.ProgressBar; import javafx.scene.layout.VBox; import javafx.stage.Stage; // shows multiple progress bars drawn in different colors. public class SimpleColoredProgressBar extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); layout.getChildren().setAll( new ColoredProgressBar("red-bar",0.2),new ColoredProgressBar("orange-bar",0.4),new ColoredProgressBar("yellow-bar",0.6),new ColoredProgressBar("green-bar",0.8) ); layout.getStylesheets().add(getClass().getResource("progress.css").toExternalForm()); stage.setScene(new Scene(layout)); stage.show(); } class ColoredProgressBar extends ProgressBar { ColoredProgressBar(String styleClass,double progress) { super(progress); getStyleClass().add(styleClass); } } }
简单的示例程序输出:
更复杂的示例程序与单个动画进度条,根据进度量动态地更改颜色:
import javafx.animation.*; import javafx.application.Application; import javafx.beans.value.*; import javafx.event.*; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Duration; // shows a progress bar whose bar changes color depending on the amount of progress. public class ColoredProgressBarStyleSheet extends Application { public static void main(String[] args) { launch(args); } private static final String RED_BAR = "red-bar"; private static final String YELLOW_BAR = "yellow-bar"; private static final String ORANGE_BAR = "orange-bar"; private static final String GREEN_BAR = "green-bar"; private static final String[] barColorStyleClasses = { RED_BAR,ORANGE_BAR,YELLOW_BAR,GREEN_BAR }; @Override public void start(Stage stage) { final ProgressBar bar = new ProgressBar(); final Timeline timeline = new Timeline( new KeyFrame(Duration.millis(0),new KeyValue(bar.progressProperty(),0)),new KeyFrame(Duration.millis(3000),1)) ); Button reset = new Button("Reset"); reset.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { timeline.playFromStart(); } }); bar.progressProperty().addListener(new ChangeListener<Number>() { @Override public void changed(ObservableValue<? extends Number> observable,Number oldValue,Number newValue) { double progress = newValue == null ? 0 : newValue.doubleValue(); if (progress < 0.2) { setBarStyleClass(bar,RED_BAR); } else if (progress < 0.4) { setBarStyleClass(bar,ORANGE_BAR); } else if (progress < 0.6) { setBarStyleClass(bar,YELLOW_BAR); } else { setBarStyleClass(bar,GREEN_BAR); } } private void setBarStyleClass(ProgressBar bar,String barStyleClass) { bar.getStyleClass().removeAll(barColorStyleClasses); bar.getStyleClass().add(barStyleClass); } }); final VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); layout.getChildren().setAll(bar,reset); layout.getStylesheets().add(getClass().getResource("progress.css").toExternalForm()); stage.setScene(new Scene(layout)); stage.show(); timeline.play(); } }
更复杂的示例程序输出: