我正在尝试使用以下代码实现背景图像而不使用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)
壁纸的位置
答案 0 :(得分:1)
您的代码中存在错误。它应该是
String image = JavaFXApplication4.class.getResource("wallpaper4.jpg").toExternalForm();
答案 1 :(得分:0)
'根'应该声明为StackPane。试试这个:
StackPane root = new StackPane()