简体   繁体   English

JavaFX通过CSS设置背景

[英]JavaFX setting a background via CSS

I want to set a background to a group, which is the root for the scene. 我想为组设置背景,这是场景的根源。 I tried this code, but the image does not appear. 我尝试了此代码,但是图像没有出现。 The image all.jpg is in the same Sourcefolder than the Main.java file. 图像all.jpg与Main.java文件位于同一Sourcefolder中。 Could anyone help me, thank a lot 谁能帮我,非常感谢

Group root = new Group();
    String image = Main.class.getResource("all.jpg").toExternalForm();
    root.setStyle("-fx-background-image: url('" + image + "') ");

A Group does not provide the ability to set a background through CSS . Group无法通过CSS设置背景。 You need to use something which extends from Region to do that. 您需要使用从Region扩展来的功能。 Probably you can use a Pane which does more or like the same thing and can be styled with a background through CSS. 可能您可以使用做更多或喜欢相同事情的Pane ,并且可以通过CSS设置背景样式。

If you want to see whats possible for every component the CSS reference guide is the best way. 如果您想了解每个组件的功能,那么CSS参考指南就是最好的方法。

Java FX CSS Reference Guide Java FX CSS参考指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM