-1

我想创建一个应用程序启动器。ProgressIndicator 应该在新线程中的新阶段启动期间运行。但是,如果我单击开始按钮,ProgressIndicator 将停止运行。

如果我进行一些 I/O 并且 ProgressIndicator 显示进度,它就会起作用。显然不可能同时更新 JavaFX 中的两个阶段,或者是否有人为我提供解决方案?

   public class Main extends Application {
        public void start(Stage primaryStage) {
            primaryStage.setScene(new Scene(new UserVC().getView(), 600, 200));
            primaryStage.show();
            userVC.autologin();
        }

        public static void main(String[] args) { launch(args); }
    }

    public class UserView  extends BorderPane {
        private ProgressIndicator progressIndicator = new ProgressIndicator();

        public UserView() {
            super.setCenter(createContent());
        }

        public Node createContent() {
            HBox userbox = new HBox();
            userbox.getChildren().add(progressIndicator);

            progressIndicator.show();

            return userbox;
        }
    }

    public class UserVC {
        private UserView view = new UserView();

        public UserVC() {
        }

        public void autologin() {
            Task<Void> task = new Task<Void>() {
                @Override public Void call() throws InterruptedException {
                    try {
                        Thread.sleep(1000);

                        Platform.runLater(new Runnable() {
                            @Override public void run() {
                                Stage stage = new Stage();
                                stage.setScene(new Scene(new MainControlVC().getView(), 900, 300));
                                stage.show();
                            }
                         });
                     }
                     catch(Exception e) {
                        e.printStackTrace();
                     }
                     return null;
                }
            };

         view.getProgressIndicator().progressProperty().unbind();
         view.getProgressIndicator().progressProperty().bind(task.progressProperty());


            new Thread(task).start();
        }
    }
4

1 回答 1

0

我将任务更改为线程,现在可以正常工作:

 Thread thread = new Thread() {
            public void run() {

                mainController = new MainControlVC(dataDirectory, user);

                Platform.runLater(new Runnable() {
                    @Override public void run() {
                        Stage stage = new Stage();
                        Scene scene = new Scene(mainController.getView(), mainController.getWidth(), mainController.getHeight());
                        stage.setScene(scene);
                        stage.show();
                    }
                });
            }
        };

        thread.start(); 
于 2019-10-28T19:18:03.873 回答