为什么舞台没有开放?

时间:2016-02-03 09:00:25

标签: java javafx

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {

    public static Stage mainStage;
    private static Stage homeStage;
    @Override
    public void start(final Stage primaryStage) {

        Application.setUserAgentStylesheet(STYLESHEET_CASPIAN);
        splashStage(primaryStage);
        primaryStage.show();

        //mainStage();
        Task<Void> task = new Task<Void>() {

            @Override
            protected Void call() throws Exception {
                // TODO Auto-generated method stub
                for (int i = 1; i < 100; i++) {
                    try {

                        System.out.println(i);
                        if(i==99)
                        {
                            primaryStage.hide();
                            mainStage();
                            break;
                        }

                        Thread.sleep(100);

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                return null;

            }
        };      

        new Thread(task).start();




    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

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

    public void mainStage()
    {
        try {

            mainStage = new Stage(StageStyle.DECORATED);
            FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            mainStage.setScene(scene);
            mainStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void homeStage()
    {
        try {
            homeStage = new Stage(StageStyle.DECORATED);
            Parent root =  FXMLLoader.load(Main.class.getResource("Home.fxml"));
            Scene scene = new Scene(root);
            homeStage.setResizable(false);
            homeStage.setScene(scene);
            homeStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void splashStage(Stage primaryStage)
    {
        try {

            primaryStage.initStyle(StageStyle.TRANSPARENT);
            FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Splash.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

i达到99后,它将无法打开mainStageprimaryStage未被隐藏

功能详情如下 splashStage() - SplashScreen
homeStage() - 主屏幕
mainStage() - LoginScreen

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您应该只在javafx GUI线程中更新UI。

所以将你的代码包装在platform.runlater();

Platform.runLater(() -> { 
    primaryStage.hide();
    mainStage();
});

关心,这段代码需要java 8!

更多信息herehere。 小心,这段代码需要java 8!