新线程无法打开场景

时间:2017-03-29 14:34:07

标签: multithreading javafx

我第三次来这里,这次是关于JavaFX中的线程。

所以,我正在尝试加载屏幕,并且loadingscreen有一个控制器,用作条目和结果之间的接口(非实际接口)。

以下是代码:

private void createLoadingScreenScene(String id) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/loadingscreen/loadingscreen.fxml"));
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.setScene(new Scene(loader.load()));
        stage.show();
        Task task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                LoadingScreenController controller = loader.getController();
                controller.setSummoner(lookupTextField.getText(), regionsBox.getSelectionModel().getSelectedItem());
                switch (id.toLowerCase()) {
                    case "ag_lookup":
                        controller.loadActiveGame();
                        break;
                    case "sum_lookup":
                        controller.loadProfile();
                        break;
                }
                return null;
            }
        };
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<?> submit = executorService.submit(task);
    }

一切都很好,直到这个: 假设id为sum_lookup,则执行此方法:

public void loadProfile() {
        if (summoner != null) {
            try {
                System.out.println("Loading profile scene");
                createProfileScene();
            } catch (DataException | WrongRequestFormatException | ReplyException | IOException e) {
                e.printStackTrace();
            }
        }
    }

createProfileScene如下:

private void createProfileScene() throws IOException, DataException, WrongRequestFormatException, ReplyException {
    System.out.println("Going ham..");
    String summonerName = summoner.getName();
    System.out.println("Going ham..0.5");
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/summonerlookup/profile.fxml"));
    System.out.println("Going ham.. 0.8");
    Stage stage = new Stage(StageStyle.DECORATED);
    System.out.println("Going ham..1");
    stage.setScene(new Scene(loader.load()));
    ProfileController controller = loader.getController();
    List<ChampionMastery> championMasteries = leagueAPI.getChampionMasteries(summoner.getId()); //TODO here
    System.out.println("Going ham..2");
    stage.show()

现在一切正常,直到打印“Going ham 0.8”。该程序实际上停止输出数据。

这是输出:

Loading profile scene
Going ham..
Going ham..0.5
Going ham.. 0.8

我做错了什么?

编辑:

这就是我现在改变的代码:      private void createLoadingScreenScene(String id)throws Exception {         FXMLLoader loader = new FXMLLoader(getClass()。getResource(“/ com / lollookup / scene / loadingscreen / loadingscreen.fxml”));         舞台舞台=新舞台(StageStyle.DECORATED);         stage.setScene(new Scene(loader.load()));         LoadingScreenController controller = loader.getController();         controller.setSummoner(regionsTextField.getText(),regionsBox.getSelectionModel()。getSelectedItem());         switch(id.toLowerCase()){             案例“ag_lookup”:                 controller.loadActiveGame();

        case "sum_lookup":
            controller.loadProfile();

    }
    stage.show();
}

这是需要大量时间的实际任务: (现在在任务中)

Task task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            System.out.println("Going ham..");
            String summonerName = summoner.getName();
            System.out.println("Going ham..0.5");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/summonerlookup/profile.fxml"));
            System.out.println("Going ham.. 0.8");
            Stage stage = new Stage(StageStyle.DECORATED);
            System.out.println("Going ham..1");
            stage.setScene(new Scene(loader.load()));
            ProfileController controller = loader.getController();
            List<ChampionMastery> championMasteries = leagueAPI.getChampionMasteries(summoner.getId()); //TODO unused
            System.out.println("Going ham..2");
            ChampionInfoData[] championStatsSummary = leagueAPI.getChampionStatsRanked(summoner.getId(), Season.SEASON_7).getChampionStatsSummary().stream().filter(element -> element.getId() != 0).map(element -> {
                try {
                    ChampionStats championStats = element.getChampionStats();
                    int championId = element.getId();
                    return new ChampionInfoData(leagueAPI.getImageUrl(championId), leagueAPI.getChampionData(championId).getName(), championStats.displayAverageKDA(), championStats.displayWinrate(), championStats.displayAverageCreepScore(), "0");
                } catch (ReplyException | DataException | IOException | WrongRequestFormatException e) {
                    e.printStackTrace();
                }
                return null;
            }).toArray(ChampionInfoData[]::new);
            System.out.println("Did i come this far? Yes!" + championStatsSummary.length);
            controller.createProfile(new SummonerData("http://avatar.leagueoflegends.com/" + summoner.getRegion().getShortCode() + "/" + summonerName.replace(" ", "") + ".png", summonerName, String.valueOf(summoner.getSummonerLevel())), championStatsSummary);
            stage.show();
            return null;
        }
    };
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(task);

然而输出仍然是:

Loading profile scene
Going ham..
Going ham..0.5
Going ham.. 0.8

0 个答案:

没有答案