颜色 – JavaFX ProgressBar:如何改变栏颜色?

前端之家收集整理的这篇文章主要介绍了颜色 – JavaFX ProgressBar:如何改变栏颜色?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在改变ProgressBar中的bar的颜色
  1. pBar.setStyle("-fx-accent: green");

但我遇到了一个问题:这似乎不适合我! (或者我只是不明白的东西)

这里是代码

  1. public class JavaFXApplication36 extends Application {
  2.  
  3. @Override
  4. public void start(Stage primaryStage) {
  5. AnchorPane root = new AnchorPane();
  6. ProgressBar pbRed = new ProgressBar(0.4);
  7. ProgressBar pbGreen = new ProgressBar(0.6);
  8. pbRed.setLayoutY(10);
  9. pbGreen.setLayoutY(30);
  10.  
  11. pbRed.setStyle("-fx-accent: red;"); // line (1)
  12. pbGreen.setStyle("-fx-accent: green;"); // line (2)
  13.  
  14. root.getChildren().addAll(pbRed,pbGreen);
  15. Scene scene = new Scene(root,150,50);
  16. primaryStage.setTitle("Hello World!");
  17. primaryStage.setScene(scene);
  18. primaryStage.show();
  19. }
  20. }

我总是得到2个红色进度条吧!似乎行(1)中的代码改变了ProgressBar类的样式,而不是实例.

另一个奇怪的时刻是,删除行(1)不会导致2个绿色进度条.所以我可以看出这条线(2)是完全没用的!为什么?!这绝对是奇怪的.

有没有办法为单独的进度条设置单独的颜色?

解决方法

答案更新,添加一个简单的非动画示例与多个进度条

您的问题中的代码显示两个不同的彩色进度条,事实上它不是JavaFX css处理系统中的错误.在这里记录运行时项目的错误http://javafx-jira.kenai.com.

作为解决方法,而不是在进度条上调用setStyle,定义用于对样式表中的进度条进行颜色的重音颜色,并将样式类添加到进度条.然后,您可以在同一应用程序中创建多个进度条,全部具有不同的颜色.

正如Uluk指出的那样,您可以使用JavaFX 2.2 caspian.cssJavaFX 2 css reference guideJavaFX 2 css tutorial一起来确定如何对事物进行风格化.

这里是一些基于这些引用中的信息自定义进度条的示例代码.

示例css:

  1. /** progress.css
  2. place in same directory as
  3. ColoredProgressBarStyleSheet.java or SimpleColoredProgressBar.java
  4. ensure build system copies the css file to the build output path */
  5.  
  6. .root { -fx-background-color: cornsilk; -fx-padding: 15; }
  7.  
  8. .progress-bar { -fx-Box-border: goldenrod; }
  9.  
  10. .green-bar { -fx-accent: green; }
  11. .yellow-bar { -fx-accent: yellow; }
  12. .orange-bar { -fx-accent: orange; }
  13. .red-bar { -fx-accent: red; }

简单示例程序:

  1. import javafx.application.Application;
  2. import javafx.geometry.Pos;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.ProgressBar;
  5. import javafx.scene.layout.VBox;
  6. import javafx.stage.Stage;
  7.  
  8. // shows multiple progress bars drawn in different colors.
  9. public class SimpleColoredProgressBar extends Application {
  10. public static void main(String[] args) { launch(args); }
  11.  
  12. @Override public void start(Stage stage) {
  13. final VBox layout = new VBox(10);
  14. layout.setAlignment(Pos.CENTER);
  15. layout.getChildren().setAll(
  16. new ColoredProgressBar("red-bar",0.2),new ColoredProgressBar("orange-bar",0.4),new ColoredProgressBar("yellow-bar",0.6),new ColoredProgressBar("green-bar",0.8)
  17. );
  18. layout.getStylesheets().add(getClass().getResource("progress.css").toExternalForm());
  19. stage.setScene(new Scene(layout));
  20. stage.show();
  21. }
  22.  
  23. class ColoredProgressBar extends ProgressBar {
  24. ColoredProgressBar(String styleClass,double progress) {
  25. super(progress);
  26. getStyleClass().add(styleClass);
  27. }
  28. }
  29. }

简单的示例程序输出

更复杂的示例程序与单个动画进度条,根据进度量动态地更改颜色:

  1. import javafx.animation.*;
  2. import javafx.application.Application;
  3. import javafx.beans.value.*;
  4. import javafx.event.*;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.*;
  8. import javafx.scene.layout.VBox;
  9. import javafx.stage.Stage;
  10. import javafx.util.Duration;
  11.  
  12. // shows a progress bar whose bar changes color depending on the amount of progress.
  13. public class ColoredProgressBarStyleSheet extends Application {
  14. public static void main(String[] args) { launch(args); }
  15.  
  16. private static final String RED_BAR = "red-bar";
  17. private static final String YELLOW_BAR = "yellow-bar";
  18. private static final String ORANGE_BAR = "orange-bar";
  19. private static final String GREEN_BAR = "green-bar";
  20. private static final String[] barColorStyleClasses = { RED_BAR,ORANGE_BAR,YELLOW_BAR,GREEN_BAR };
  21.  
  22. @Override public void start(Stage stage) {
  23. final ProgressBar bar = new ProgressBar();
  24.  
  25. final Timeline timeline = new Timeline(
  26. new KeyFrame(Duration.millis(0),new KeyValue(bar.progressProperty(),0)),new KeyFrame(Duration.millis(3000),1))
  27. );
  28.  
  29. Button reset = new Button("Reset");
  30. reset.setOnAction(new EventHandler<ActionEvent>() {
  31. @Override public void handle(ActionEvent event) {
  32. timeline.playFromStart();
  33. }
  34. });
  35.  
  36. bar.progressProperty().addListener(new ChangeListener<Number>() {
  37. @Override public void changed(ObservableValue<? extends Number> observable,Number oldValue,Number newValue) {
  38. double progress = newValue == null ? 0 : newValue.doubleValue();
  39. if (progress < 0.2) {
  40. setBarStyleClass(bar,RED_BAR);
  41. } else if (progress < 0.4) {
  42. setBarStyleClass(bar,ORANGE_BAR);
  43. } else if (progress < 0.6) {
  44. setBarStyleClass(bar,YELLOW_BAR);
  45. } else {
  46. setBarStyleClass(bar,GREEN_BAR);
  47. }
  48. }
  49.  
  50. private void setBarStyleClass(ProgressBar bar,String barStyleClass) {
  51. bar.getStyleClass().removeAll(barColorStyleClasses);
  52. bar.getStyleClass().add(barStyleClass);
  53. }
  54. });
  55.  
  56. final VBox layout = new VBox(10);
  57. layout.setAlignment(Pos.CENTER);
  58. layout.getChildren().setAll(bar,reset);
  59. layout.getStylesheets().add(getClass().getResource("progress.css").toExternalForm());
  60. stage.setScene(new Scene(layout));
  61. stage.show();
  62.  
  63. timeline.play();
  64. }
  65. }

更复杂的示例程序输出

猜你在找的Java相关文章