如何在 JavaFX 中创建具有透明背景的启动画面 [英] How to create Splash screen with transparent background in JavaFX

查看:197
本文介绍了如何在 JavaFX 中创建具有透明背景的启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像我提供的示例一样创建启动画面.似乎 AnchorPane 不允许透明背景,我尝试将 AnchorPane 的 css 设置为 -fx-background-color: rgba(255,0,255,0.1) ; 但白色背景仍然出现.

I am trying to create a splash screen like the example I've provded. It seems that AnchorPane does not allow transparent background, I've tried setting the css of the AnchorPane to -fx-background-color: rgba(255,0,255,0.1) ; but the white background still shows up.

我的 fxml 文件中只有一个带有 ImageView 的 AnchorPane,其中包含 png 图像

All I have in my fxml file is a AnchorPane with ImageView with contain the png image

我到处找,但找不到任何解决方案,任何帮助将不胜感激.谢谢

I've looked everywhere but can't find any solution, any help would be appreciated. Thanks

推荐答案

试试这个 JavaFX 启动示例 为 Stackoverflow 问题创建:设计启动画面 (java).还有一个后续示例,它还提供了应用程序初始化进度反馈.

Try this JavaFX splash sample created for the Stackoverflow question: Designing a splash screen (java). And a follow up sample which also provides application initialization progress feedback.

JavaFX 确实提供了 Preloader 接口,用于从启动到平滑传输应用程序,但上面的示例没有使用它.

JavaFX does offer the Preloader interface for smooth transfer from splash to application, but the above samples don't make use of it.

上面的飞溅示例也没有做透明效果,但是这个对话框示例显示你怎么做,你可以把它和之前的飞溅样本结合起来,得到你想要的效果.

The splash samples above also don't do the transparent effect, but this dialog sample shows you how to do that and you can combine it with the previous splash samples to get the effect you want.

透明效果是由:

  1. stage.initStyle(StageStyle.TRANSPARENT).
  2. scene.setFill(Color.TRANSPARENT).
  3. 确保您的根节点不是一个不透明的方形矩形.

这一切都在 Sergey 的示例中进行了演示.

Which is all demonstrated in Sergey's sample.

相关问题:

根据其他问题更新 2016 年 4 月

预加载器图像不在前台.我试过 stage.toFront(),但没有帮助.

the preloader image isnt in the foreground. I have tried stage.toFront(), but doesnt help.

在 Java 8u20 中创建了一个新的 API stage.setAlwaysOnTop(true).我更新了 链接示例 以在初始启动屏幕上使用它,这有助于实现更平滑的过渡到主屏幕.

A new API was created in Java 8u20 stage.setAlwaysOnTop(true). I updated the linked sample to use this on the initial splash screen, which helps aid in a smoother transition to the main screen.

Java8+

对于 modena.css(Java 8 中默认的 JavaFX 观感定义),为所有控件(如果加载了控件,也会为窗格)引入了轻微的阴影背景.

For modena.css (the default JavaFX look and feel definition in Java 8), a slight shaded background was introduced for all controls (and also to panes if a control is loaded).

您可以通过指定默认背景为透明来删除它.这可以通过将以下行添加到应用程序的 CSS 文件中来完成:

You can remove this by specifying that the default background is transparent. This can be done by adding the following line to your application's CSS file:

.root { -fx-background-color: transparent; }

如果您愿意,您可以使用 CSS 样式类和规则或 setStyle 调用(如 Sergey 的回答所示)来确保该设置仅适用于启动画面的根目录,而不是所有应用程序屏幕.

If you wish, you can use CSS style classes and rules or a setStyle call (as demonstrated in Sergey's answer) to ensure that the setting only applies to the root of your splash screen rather than all of your app screens.

查看相关内容:

这篇关于如何在 JavaFX 中创建具有透明背景的启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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