首页 > 解决方案 > showAndWait() 与动画,使用 Platform.runlater() 给出不同的行为

问题描述

当我不使用动画并且拥有 stage.showAndWait() 时,它会执行正确的行为(单击“是”按钮时,我会从方法中得到正确的结果)但是我想使用动画,因此我使用了解决方法

Platform.runLater(()-> {
            stage.setScene(new Scene(root));
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.showAndWait();
                }

但这一次,即使在单击任何按钮之前,该值也是错误的,并且我没有从该按钮中获得任何价值。

所以这是我显示确认对话框的方法。如果单击是按钮,我想重新启动游戏,如果没有,则退出程序。

public boolean display(String title, String message) throws IOException {

        FXMLLoader confirmViewLoader = new FXMLLoader(getClass().getResource("../main/ConfirmDialogBoxView.fxml"));
        confirmViewLoader.load();
        ConfirmDialogBoxController confirmDialogBoxController = confirmViewLoader.getController();
        confirmDialogBoxController.setConfirmLabelText(message);
        Parent root = confirmViewLoader.getRoot();
        Stage stage = new Stage();
        stage.setTitle(title);

        Platform.runLater(()-> {
            stage.setScene(new Scene(root));
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.showAndWait();
                }
        );
        return confirmDialogBoxController.getBtnChoice();
    }

这里是检查游戏是否结束的部分

...

TranslateTransition animation = new TranslateTransition(Duration.seconds(0.25), disc);  ///// Animation
        animation.setToY(row * (TILE_SIZE + 5) + TILE_SIZE / 4);

        if (checkConnections(column, currentRow)) {         
            gameOver();
        }else{
            playerRed = !playerRed;
        }

        animation.play();
    }

在我的确认对话框控制器中:

public boolean getBtnChoice(){
        return answer;
    }
@FXML
    public void yesButtonClicked(ActionEvent actionEvent) {

        answer = true;
        stage = (Stage) yesButton.getScene().getWindow();
        stage.close();

    }

    @FXML
    public void noButtonClicked(ActionEvent actionEvent) {

        answer = false;
        Stage confirmWindow = (Stage) noButton.getScene().getWindow();
        confirmWindow.close();
        Platform.exit();
    }

标签: animationjavafxtimer

解决方案


推荐阅读