JavaFX 8工具提示删除了舞台的透明度 [英] JavaFX 8 tooltip removes transparency of stage

查看:79
本文介绍了JavaFX 8工具提示删除了舞台的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had a hard time figuring out why my transparent stage refuses to be transparent. Finally I found out, that it was caused by a Tooltip that was installed into an 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行时,透明度按预期工作,但是安装工具提示后,舞台背景为灰色(例如默认窗口背景)。虽然按钮的作用很明显,而且工具提示对我的布局来说并不重要,但这样做很好,只是给一点提示......

When I comment out the last 4 lines, the transparency works as expected, but with the Tooltip installed the stages background is grayish (e.g. the default window background). Though it's obvious what the button does and the tooltip is not essential for my layout it'd be nice to have, just to give a little hint...

任何建议或解决方法?

Any suggestions or workarounds?

推荐答案

解决方案

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

Set the style -fx-background-color: transparent on the root node of the scene.

背景

Oracle中讨论了类似的行为关于JavaFX场景/填充颜色的JavaFX论坛帖子

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

Relevant comments from the thread by David Grieve, the lead developer for the JavaFX CSS features:


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

This happens because modena.css sets the background color of the root node. Setting the style -fx-background-color: transparent on the root node of the scene is the solution.



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

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








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

The default user agent stylesheet is loaded the first time a Control is instantiated. The reason for this is to avoid loading stylesheets and reduce CSS overhead in scene-graphs that contain nodes that don't use CSS.








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

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

The merit of loading in this way is to avoid css overhead in scene's that don't use controls. This would be typical of a splash screen, for example. Or maybe a game. This design choice was made a long time ago when CSS performance was a big issue, but it still makes sense for embedded devices.






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


In the case of your question, Tooltip is a control, so when you add it to the scene it implicitly triggers the default modena.css stylesheet to be loaded for the scene (which sets the background of the root node of the scene to gray rather than a null or transparent fill which is used when there are no controls in the scene). To retain the transparent background for the application when a control is used in the scene, it is necessary to explicitly set the scene root node background to transparent.

透明舞台的示例代码

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

这篇关于JavaFX 8工具提示删除了舞台的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆