我正在尝试使用以下没有 CSS 的代码实现背景图像,但它对我不起作用。
下面是代码和错误
代码
Group root = new Group();
Scene scene = new Scene(root, 650, 1180); //1180, 650
String image = JavaFXApplication4.class.getClass().getResource("wallpaper4.jpg").toExternalForm();
root.setStyle("-fx-background-image: url('" + image + "'); -fx-background-repeat: stretch; -fx-background-size: 650 1180;-fx-background-position: top left;");
System.out.println("path: " + image);
stage.setScene(scene);
错误:
Caused by: java.lang.NullPointerException
at javafxapplication4.JavaFXApplication4.start(JavaFXApplication4.java:381)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:811)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:261)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:258)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:258)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
壁纸的位置