设置imageview后JavaFX等待

时间:2017-05-12 07:21:17

标签: java multithreading javafx wait

我需要在设置Imageview后等待,但如果我使用

Thread.sleep(1000)

设置imageView后,整个应用程序会休眠一秒钟,但在此期间不会设置imageView。

我想知道如何在设置imageview后暂停整个应用程序。

例如,这是一张纸牌游戏(briscola),在电脑玩牌后,或者我玩了一张牌,应用程序应暂停以显示盘子上有哪些牌以及谁赢了牌。

提前致谢。

更新: 这是我尝试完成的任务,没有成功的图像

    private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();


    // set images

    Task<Integer> task = new Task<Integer>() {
        @Override protected Integer call() throws Exception {
            for(Map.Entry<Player, BriscolaCard> entry : entries){
                plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()),200,100,true, false)));
            }
            plate.getCards().getChildren().setAll(plate.getImageViews());
            return 0;
        }
    };

    Thread th = new Thread(task);
    th.start();

    // clear images 

    Task <Integer> clearTask = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            clearImages();
            return 0;
        }
    };
    Thread clearThread = new Thread(clearTask);
    if(entries.size()==2){
        Platform.runLater(clearTask);
    }else {
        clearThread.start();
    }
}

第二次更新:

这看起来效果更好(我会在清除之前随机查看图像几毫秒),但是如何设置在Platform.runLater()之前等待的最短时间?

private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();

    for (Map.Entry<Player, BriscolaCard> entry : entries) {
        plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()), 200, 100, true, false)));
    }
    plate.getCards().getChildren().setAll(plate.getImageViews());

    Task<Void> waitTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            clearImages();
            return null;
        }
    };

    if(entries.size() == 2){
        Platform.runLater(waitTask);
    }

}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,感谢Rahul Singh的建议!

private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();
    plate.getImageViews().clear();

    for (Map.Entry<Player, BriscolaCard> entry : entries) {
        plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()), 200, 100, true, false)));
    }
    plate.getCards().getChildren().setAll(plate.getImageViews());

    Task<Void> waitTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            Timeline timeline = new Timeline(new KeyFrame(
                    Duration.millis(2000),
                    ae -> clearImages()));
            timeline.play();
            return null;
        }
    };

    if(entries.size() == 2){
        Platform.runLater(waitTask);
    }

}