当玩家赢得我创建的游戏时,我试图显示一个警报对话框。但是,我遇到了一个异常(exception):
java.lang.IllegalStateException: showAndWait is not allowed during animation or layout processing
我尝试在 AnimationTimer
中添加 stop()
但它不起作用,仍然抛出相同的异常:
if (ball.getBall().getCenterY() == 0) {
//computer lost!
stop();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(null);
alert.setHeaderText(null);
alert.setContentText("Good game. You won! Click OK to exit.");
alert.showAndWait(); //exception thrown here
System.exit(0);
}
最佳答案
您只能在事件处理程序中调用 showAndWait()
,而不能在动画中调用。尽管在 documentation for Stage
中记录了这一点,但 Alert
类中并未明确记录这一点。 .
改为调用 show()
,并使用警报的 onHidden
事件的处理程序,以便在警报关闭时调用某些内容:
if (ball.getBall().getCenterY() == 0) {
//computer lost!
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(null);
alert.setHeaderText(null);
alert.setContentText("Good game. You won! Click OK to exit.");
alert.setOnHidden(evt -> Platform.exit());
alert.show();
}
关于java - AnimationTimer - 动画处理期间不允许使用 showAndWait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742134/