简体   繁体   English

Javafx:对话框窗格参考

[英]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的层次结构,它与StageScene没有任何关系,或者只是我没有观察到它。 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. 上面的这段代码可以满足您的需求。

Look here for more 在这里查看更多

And you can always consult java docs. 而且您可以随时查阅Java文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM