【问题标题】:ProgressIndicator transparent background进度指示器透明背景
【发布时间】:2014-07-17 17:02:01
【问题描述】:

所以我有这个代码:

    final StackPane g = new StackPane();        
    final ProgressIndicator p1 = new ProgressIndicator();
    p1.setPrefSize(100, 100);  
    p1.progressProperty().addListener(new ChangeListener<Number>() {

        @SuppressWarnings("rawtypes") 
        @Override
        public void changed(ObservableValue ov, Number oldVal, Number newVal) {

            if (p1.getProgress() < 0.3) {
                p1.setStyle("-fx-progress-color: red;");
            } else if (p1.getProgress() < 0.65) {
                p1.setStyle("-fx-progress-color: orange;");
            } else {
                p1.setStyle("-fx-progress-color: green;");
            }

        }
    });
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.setAutoReverse(true);

    final KeyValue kv = new KeyValue(p1.progressProperty(), 1);
    final KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv);
    timeline.getKeyFrames().add(kf1);


    g.getChildren().addAll(p1); //veil
    g.setAlignment(Pos.CENTER);

然后我将堆栈窗格添加到场景中,并将场景添加到舞台。

我有两个阶段,一个是我所有的东西的初始阶段,另一个是进度指示器。这是问题所在:包含进度指示器的阶段有一个白色方形(框)作为背景,非常适合其他阶段,因为通常具有相同的白色背景。但是当初始背景不是白色时,我可以看到进度指示器阶段的白色背景。所以这是我的问题:如何使背景透明?我尝试了 BlendMode 类,但这似乎不起作用。有任何想法吗?

提前致谢!

【问题讨论】:

    标签: css javafx javafx-8


    【解决方案1】:

    对我来说,以上答案都不起作用。

    我终于搞定了:

    progressIndicator.getScene().getRoot().setStyle("-fx-background-color: transparent");
    

    如果您正在使用场景,请通过以下操作使场景透明:

    scene.setFill(null);
    

    【讨论】:

      【解决方案2】:

      试试这个 css(基于 Java 8u40 的错误修复):

      .progress-indicator:indeterminate > .spinner {
          /** Applying to undo styling from .spinner, reported in RT-37965 */
          -fx-background-color: transparent;
          -fx-background-insets: 0;
          -fx-background-radius: 0;
      }
      

      【讨论】:

        【解决方案3】:

        使用外部样式表

        .progress-indicator .indicator {
            -fx-background-color: transparent ;
        }
        

        这是一个完整的示例(我也使用了外部样式表来管理颜色,但无论如何都可以使用):

        import javafx.application.Application;
        import javafx.application.Platform;
        import javafx.concurrent.Task;
        import javafx.css.PseudoClass;
        import javafx.scene.Node;
        import javafx.scene.Scene;
        import javafx.scene.control.Control;
        import javafx.scene.control.ProgressIndicator;
        import javafx.scene.layout.StackPane;
        import javafx.stage.Stage;
        
        public class ProgressIndicatorTest extends Application {
        
            @Override
            public void start(Stage primaryStage) {
                StackPane root = new StackPane();
                ProgressIndicator pi = new ProgressIndicator();
                Task<Void> counter = new Task<Void>() {
                    @Override
                    public Void call() throws Exception {
                        for (int i = 1; i <= 100; i++) {
                            Thread.sleep(50);
                            updateProgress(i, 100);
                        }
                        return null;
                    }
                };
                pi.progressProperty().bind(counter.progressProperty());
                pi.progressProperty().addListener((obs, oldProgress, newProgress) -> {
                    PseudoClass warning = PseudoClass.getPseudoClass("warning");
                    PseudoClass critical = PseudoClass.getPseudoClass("critical");
                    if (newProgress.doubleValue() < 0.3) {
                        pi.pseudoClassStateChanged(warning, false);
                        pi.pseudoClassStateChanged(critical, true);
                    } else if (newProgress.doubleValue() < 0.65) {
                        pi.pseudoClassStateChanged(warning, true);
                        pi.pseudoClassStateChanged(critical, false);
                    } else {
                        pi.pseudoClassStateChanged(warning, false);
                        pi.pseudoClassStateChanged(critical, false);
                    }
                });
                pi.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
                root.setStyle("-fx-background-color: antiqueWhite;");
                root.getChildren().add(pi);
                Scene scene = new Scene(root, 400, 400);
                scene.getStylesheets().add("progress.css");
                primaryStage.setScene(scene);
                primaryStage.show();
                new Thread(counter).start();
            }
        
            public static void main(String[] args) {
                launch(args);
            }
        }
        

        progress.css:

        .progress-indicator .indicator {
            -fx-background-color: transparent ;
        }
        .progress-indicator {
            -fx-progress-color: green ;
        }
        .progress-indicator:critical {
            -fx-progress-color: red ;
        }
        .progress-indicator:warning {
            -fx-progress-color: orange ;
        }
        

        【讨论】:

        • 我试过了,还是不行,我还有白盒。我认为应该设置剪辑。
        • 对我来说很好。更新了一个例子。如果这不起作用,请尝试使用ScenicView 查看白框来自何处(例如,它可能来自您的堆栈窗格)。
        • 实际上它来自堆栈窗格,当我将其更改为组时,它起作用了。另外,我在场景中添加了参数“Color.Transparent”。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-16
        • 1970-01-01
        • 2022-01-23
        • 2018-01-07
        • 2018-02-12
        • 2011-10-24
        • 1970-01-01
        相关资源
        最近更新 更多