代码
public class LoginController {
/*动态壁纸切换常量*/
private static int WALLPAPER_NUM = 1;
public Pane paneBody;
/*初始化*/
public void initialize() {
changeWallpaper();
}
/*动态切换壁纸*/
public void changeWallpaper() {
new Thread(() -> {
while (true){
if(WALLPAPER_NUM > 3){
WALLPAPER_NUM = 1;
}
/* WALLPAPER_NUM 引入的图片名称相同,以数字分隔*/
String i = Objects.requireNonNull(this.getClass().getResource("/image/img/login"+WALLPAPER_NUM+".png")).toExternalForm();
paneBody.setStyle("-fx-background-image: url("+ i +");");
System.out.println(WALLPAPER_NUM);
WALLPAPER_NUM++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}