Sta*_*ust 2 java animation javafx exception
当玩家赢得我创建的游戏时,我正在尝试显示警告对话框.但是,我得到一个例外:
java.lang.IllegalStateException: showAndWait is not allowed during animation or layout processing
Run Code Online (Sandbox Code Playgroud)
我尝试添加stop()的AnimationTimer,但它没有工作,仍然抛出同样的异常:
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);
}
Run Code Online (Sandbox Code Playgroud)
您只能showAndWait()在事件处理程序中调用,而不能在动画中调用.这没有在Alert类中明确记录,尽管它在文档中有记录Stage.
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();
}
Run Code Online (Sandbox Code Playgroud)