【问题标题】:Transparent Node Background透明节点背景
【发布时间】:2015-03-12 08:22:22
【问题描述】:

我如何以编程方式使Node的背景透明-(透视);我目前正在使用 AnchorPane 作为父节点到其他 ProgressIndicator 和其他节点作为子节点,我希望它们脱颖而出?我尝试使用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();

我在这里的要求是让我的孩子Nodes 脱颖而出,没有AnchorPane 的白色背景-(所以AnchorPane 需要是透明的),我该如何实现?

【问题讨论】:

  • 你尝试了什么?您的代码是否只有 AnchorPane 作为 Scene 的根,或者 AnchorPane 中是否也有子节点?请添加您尝试过的代码,因为透明度也取决于hierarchy。
  • Sir @ItachiUchiha 它是一个带有子节点的父节点,我已经给了一些示例......,还有你的意思是什么因为透明度也取决于层次结构这是什么意思真的像听起来那样的意思吗?
  • 也许我的回答对我的评论有所启发。

标签: javafx javafx-2 javafx-8


【解决方案1】:

包括Stage、Scene 和Layout 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);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 当然只有Regions的背景可以用CSS设置样式。
    • 抱歉没有完全阅读您的全部答案,但是先生,它不起作用,我在代码示例中添加了我的要求,您可以检查我的编辑或者我不明白吗?
    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2011-03-08
    • 2021-09-20
    • 2019-02-07
    • 2020-10-15
    • 2017-06-22
    • 2010-10-20
    • 2011-02-01
    相关资源
    最近更新 更多