JavaFX 8工具提示删除了舞台的透明度

时间:2015-03-30 21:50:40

标签: java javafx javafx-8

我很难弄清楚为什么我的透明舞台拒绝透明。最后我发现,它是由安装在Tooltip中的ImageView引起的:

ImageView imageViewIcon = new ImageView();
imageViewIcon.setFitWidth(70);
imageViewIcon.setFitHeight(70);
imageViewIcon.setPreserveRatio(false);
imageViewIcon.setImage(new Image("./next.png"));

Tooltip tooltip = new Tooltip("Tooltip!");
if (this.config.getShowTooltip("true")) {
    Tooltip.install(imageViewIcon, tooltip);
}

当我注释掉最后4行时,透明度按预期工作,但安装了Tooltip后,舞台背景呈灰色(例如默认窗口背景)。虽然显而易见的是按钮的作用以及工具提示对我的布局来说并不重要,但这样做会很好,只是给一点提示......

有任何建议或解决方法吗?

1 个答案:

答案 0 :(得分:3)

<强>解决方案

在场景的根节点上设置样式-fx-background-color: transparent

<强>背景

Oracle JavaFX Forum post on the JavaFX Scene/Fill Color中讨论了类似的行为。

JavaFX CSS主要开发人员David Grieve的主题相关评论:

  

这是因为modena.css设置了根节点的背景颜色。在场景的根节点上设置样式-fx-background-color: transparent是解决方案。

BorderPane root = new BorderPane();  
root.setStyle("-fx-background-color: transparent;");  

Scene scene = new Scene(root, 600, 400, Color.BLACK); 

  

第一次实例化Control时会加载默认的用户代理样式表。这样做的原因是为了避免加载样式表并减少包含不使用CSS的节点的场景图中的CSS开销。


  

背后的历史是,摩德纳主题的设计师认为控件在这种特殊的背景颜色上看起来更好,这种颜色非常浅灰色。遗憾的是,无法从CSS设置场景的背景填充样式,因此样式设置在场景的根节点上。在JIRA中记录了一个问题,使得Scene可以通过CSS设置样式(RT-31282

     

以这种方式加载的优点是避免不使用控件的场景中的css开销。例如,这将是典型的闪屏。或者也许是游戏。这种设计选择很久以前就是CSS性能问题,但它对嵌入式设备仍然有意义。


如果您的问题是Tooltip是一个控件,那么当您将其添加到场景时,它会隐式触发为场景加载的默认modena.css样式表(设置背景场景的根节点为灰色,而不是在场景中没有控件时使用的空或透明填充。要在场景中使用控件时保留应用程序的透明背景,必须将场景根节点背景显式设置为透明。


Sample code for a transparent stage

//this is where the transparency is achieved:
//the three layers must be made transparent
//(i)  make the VBox transparent (the 4th parameter is the alpha)
root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
//(ii) set the scene fill to transparent
scene.setFill(null);
//(iii) set the stage background to transparent
stage.initStyle(TRANSPARENT);