使用css setstyle方式
但使用IntelliJ IDEA时,在写路径时会提示错误,但实际上是可以不管的
路径写项目的工作目录的相对路径即可

项目的路径如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
├── pom.xml
├── src
│ ├── data
│ │ ├── Book_opened.png
│ │ ├── Book_opened.svg
│ │ └── illust_75942276_20200606_004558.jpg
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── loli
│ │ └── library
│ │ ├── App.java
│ │ ├── LoginStage.java
│ │ ├── ManagerStage.java
│ │ └── SQLPlugin.java
│ ├── style
│ │ ├── background.css
│ │ ├── background_test.css
│ │ └── test_css.html
│ └── test
│ └── java
│ └── com
│ └── loli
│ └── library
│ └── AppTest.java

setsyle方式:

1
2
3
4
5
BorderPane backpane = new BorderPane();
backpane.setStyle("-fx-background-image: url(" + "file:src/data/illust_75942276_20200606_004558.jpg" + "); " +
"-fx-background-position: center center; " +
"-fx-background-repeat: stretch;" +
"-fx-background-color: transparent;");

在这里插入图片描述错误的提示不用管

css文件方式[报错]:

这是在JavaFX How to set scene background image上抄的方法,但奈何无论怎么改图片的位置,改图片的相对/绝对路径,或者改成链接,都不管用

1
2
3
4
5
6
7
#pane{
-fx-background-image: url(file:src/data/illust_75942276_20200606_004558.jpg);
-fx-background-repeat: stretch;
/*-fx-background-size: 900 506;*/
-fx-background-position: center center;
/*-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);*/
}
1
2
3
4
StackPane stackPane = new StackPane();
stackPane.setId("pane");
Scene scene = new Scene(stackPane,600,400);
scene.getStylesheets().add(getClass().getResource("file:src/style/background.css").toExternalForm());

报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$412(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at com.loli.library.LoginStage.<init>(LoginStage.java:54)
at com.loli.library.App.start(App.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$419(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$399(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$397(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$398(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$203(GtkApplication.java:139)
... 1 more
Exception running application com.loli.library.App