首页 > 解决方案 > JavaFX 任务线程显然没有同时运行

问题描述

我一直试图在场景之间放置一个加载场景,我的想法如下:

  1. 启动一个线程来创建所需的场景;
  2. 创建场景的方法也将创建的场景设置为类的Scene属性;
  3. 在线程运行时,正在创建和设置加载场景。
  4. 线程完成后,将场景设置为所需的场景。

显然,整个加载场景被忽略了,所需的场景只是简单设置。我敢打赌,线程不会同时工作。

这是任务部分和相关属性。

private Stage pokeWindow = new Stage();
    private Scene currentScene;
    private boolean sceneReady = false;
    private String targetScene;
    private int genericInt;

    private final Task setGenericScene = new Task(){
        @Override
        protected Object call() throws Exception {
           sceneReady = false;

        if(targetScene.equals("summaryScene")){
            setSummaryScene(genericInt);
            sceneReady = true;
        }

        return null;
        }  
    };

这是装载场景。

public void setLoadingScene(){
       Thread loadScreen = new Thread(setGenericScene);
        loadScreen.start();


        if(!sceneReady){
        GridPane background = new GridPane();
        BackgroundImage loadingBG = new BackgroundImage(new Image((new File("src/resources/tiles/pokedexfullbg.png")).toURI().toString()), BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        background.setBackground(new Background(loadingBG));

    [...]
    [more javafx code regarding the loading scene]

    Scene loadingScene = new Scene(background, 512, 416);
        this.pokeWindow.setScene(loadingScene);

        animation1.play();
        animation2.play();
        animation3.play();
        animation4.play();
        animation5.play();
        animation6.play();
        animation7.play();
        animation8.play();

        while(!sceneReady){}
        }

        this.pokeWindow.setScene(currentScene);
    }

标签: javamultithreadingjavafx

解决方案


推荐阅读