gpt4 book ai didi

java - 如何在javaFx中的应用程序窗口之前制作加载场景?

转载 作者:行者123 更新时间:2023-12-02 03:17:00 25 4
gpt4 key购买 nike

我试图在真正的应用程序窗口打开之前创建一个加载窗口。加载场景中有一个进度条,并且是不确定的。

问题是;直到我执行程序时真正的窗口打开时,进度条才起作用。

顺便说一句,我尝试了预加载器类,但也不起作用。

这是我的代码;

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!"));
}


}


}

最佳答案

您可以使用预加载器。这是一个例子。取自source

public class FirstPreloader extends Preloader {
ProgressBar bar;
Stage stage;

private Scene createPreloaderScene() {
bar = new ProgressBar();
BorderPane p = new BorderPane();
p.setCenter(bar);
return new Scene(p, 300, 150);
}

public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}

@Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
}

@Override
public void handleStateChangeNotification(StateChangeNotification evt) {
if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
stage.hide();
}
}
}

关于java - 如何在javaFx中的应用程序窗口之前制作加载场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com