简体   繁体   中英

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. 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 . You need to use something which extends from Region to do that. Probably you can use a Pane which does more or like the same thing and can be styled with a background through CSS.

If you want to see whats possible for every component the CSS reference guide is the best way.

Java FX CSS Reference Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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