java如何在javaFx中在应用程序窗口之前制作加载场景?
我正在尝试在真正的应用程序窗口打开之前创建一个加载窗口。加载场景中有一个进度条,未确定
问题是,;progressbar在我执行程序时真正的窗口打开之前不工作
顺便说一下,我尝试了预加载类,但也不太有效
这是我的密码
public class MainApp2 extends Application {
private Stage loadingStage = new Stage();
public static void main(String[] args) {
launch(args);
}
public void start(final Stage mainStage) throws Exception {
//loading..
loadingScreen();
//start...
appScreen();
}
private void loadingScreen() {
ProgressBar bar = new ProgressBar(ProgressIndicator.INDETERMINATE_PROGRESS);
bar.setPrefWidth(300);
bar.setPrefHeight(200);
loadingStage.initStyle(StageStyle.TRANSPARENT);
loadingStage.initStyle(StageStyle.UNDECORATED);
loadingStage.setScene(new Scene(bar));
loadingStage.show();
}
private void appScreen() {
new Task<Void>() {
@Override
protected Void call() throws Exception {
Stage mainStage = new Stage();
//get real window
Scene root = new Scene(new MyAppWindow().getAppWindow());
mainStage.setScene(root);
mainStage.centerOnScreen();
mainStage.show();
// loadingStage.close();
return null;
}
}.run();
}
public class MyAppWindow {
public BorderPane getAppWindow(){
System.out.println("may be initialize take a long time...");
for (int i = 0; i < 90000000; i++) {
System.out.print("");
}
return new BorderPane(new Label("Here is real application Window!"));
}
}
}
# 1 楼答案
你可以使用预加载。下面是一个例子。摘自source
# 2 楼答案
至少我发现了;无论如何,在JavaFx中都不需要初始化初始屏幕。因为JavaFx由一个线程管理。所以我尝试了这一步。这对我有用
还有我的闪屏