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

我很难搞清楚为什么我的透明舞台拒绝透明。 最后我发现,它是由安装到ImageViewTooltip引起的:

 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 ,舞台背景呈灰色(例如默认窗口背景)。 虽然显而易见的是按钮的作用以及工具提示对我的布局来说并不是必不可少的,但这样做会很好,只是给一点提示……

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

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

背景

在JavaFX场景/填充颜色的Oracle JavaFX论坛post中讨论了类似的行为。

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

发生这种情况是因为modena.css设置了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样式表(它将场景的根节点的背景设置为灰色而不是而不是在场景中没有控件时使用的空或透明填充。 要在场景中使用控件时保留应用程序的透明背景,必须将场景根节点背景显式设置为透明。


透明舞台的示例代码 :

 //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);