透明节点背景

Transparent Node Background

我如何以编程方式制作Node的背景transparent-(透视);我目前正在玩 AnchorPane 作为 parent 节点到其他 ProgressIndicator 和其他节点作为 children,我希望它们脱颖而出?我尝试了 Scene,它与我想要的不相近,有什么解决方法吗?

让我们说

// suppose i have a fake layout like this
    AnchorPane ap = new AnchorPane();

    ProgressIndicator pi = new ProgressIndicator();
    ProgressIndicator pii = new ProgressIndicator();
    ProgressIndicator piii = new ProgressIndicator();
    ProgressIndicator piiii = new ProgressIndicator();
    AnchorPane.setRightAnchor(pi, 0.0);
    AnchorPane.setBottomAnchor(piii, 0.0);
    AnchorPane.setRightAnchor(piiii, 0.0);
    AnchorPane.setBottomAnchor(piiii, 0.0);
    AnchorPane.setLeftAnchor(piii, 0.0);
    Circle circle = new Circle();
    circle.setRadius(50);
    circle.setFill(Color.RED);
    AnchorPane.setBottomAnchor(circle, 210.0);
    AnchorPane.setTopAnchor(circle, 210.0);
    AnchorPane.setLeftAnchor(circle, 210.0);
    AnchorPane.setRightAnchor(circle, 210.0);
    ap.getChildren().addAll(pi,pii,piii,circle,piiii);
    primaryStage.setScene(new Scene(ap,500,500));
    primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.show();

我这里的要求是让我的 children Nodes 脱颖而出,没有 AnchorPane 的白色背景 -(所以 AnchorPane 需要反式parent) ,我该如何实现?

您应该首先了解 JavaFX 视图层次结构的工作原理。首先,你有一个Stage。在一个阶段,可以显示一个Scene。在场景中,您可以嵌入一个 Node,它通常是一个窗格或排序,例如 BorderPane。在这种情况下,您应该将窗格设置为透明,因为它是舞台场景的内容。

使用 setStyle("-fx-background-color: transparent;");,或者更好:将其添加到 css 文件中的 class,将 css 文件设置为样式 sheet包含您的 Node 的场景,并将 css class 添加到节点的 styleClasses

包括 StageSceneLayout Containers 在内的所有内容都有其背景颜色。所以如果你需要一个完全透明的背景,你需要为每个设置transparent fill

舞台

primaryStage.initStyle(StageStyle.TRANSPARENT);

针对场景

scene.setFill(Color.TRANSPARENT);

对于容器

container.setBackground(Background.EMPTY);

为您的代码

Scene scene = new Scene(ap,500,500);
scene.setFill(Color.TRANSPARENT);
ap.setBackground(Background.EMPTY);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);