[英]Javafx : Dialog pane reference
I have a controller, which contains buttons, if I press one button an Alert
shows, with Yes and No options, and I want to write a test for this controller class testing the result of this Alert
, for example if I press the Yes button what happens. 我有一个包含按钮的控制器,如果按一个按钮, Alert
显示,带有“是”和“否”选项,并且我想为此控制器类编写测试此Alert
结果的测试,例如,如果按“是”按钮怎么了。 I could get the Window/Stage, and the Scene, but how can I get the Alert
from the controller which popped up. 我可以获取Window / Stage和Scene,但是如何从弹出的控制器中获取Alert
。 Is that the primaryStage or I don't really understand how can I get it. 是primaryStage还是我不太了解如何获得它。 I had a look at the hierarchy of the Alert
and it doesn't have any relation with the Stage
or Scene
or just I didn't observed it. 我查看了Alert
的层次结构,它与Stage
或Scene
没有任何关系,或者只是我没有观察到它。 Is there a possibility to get a reference of the Alert
or of its buttons? 是否有可能获得Alert
或其按钮的参考?
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}
This code above does what you need. 上面的这段代码可以满足您的需求。
And you can always consult java docs. 而且您可以随时查阅Java文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.