JavaFX Alert对话框

  • Post author:
  • Post category:java


1. 标准对话框

消息对话框


Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8135/e28021e7-33d2-3e39-91a8-4f7295922d4c.png[/img]

没有标题的消息对话框


Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8137/20012fc7-50ca-3e9c-8e4b-f10babe88db4.png[/img]

2. 警告对话框


Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Warning Dialog");
alert.setHeaderText("Look, a Warning Dialog");
alert.setContentText("Careful with the next step!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8139/fd3ced30-382e-3727-a5e0-eed56acf594a.png[/img]

3. 错误对话框


Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("Look, an Error Dialog");
alert.setContentText("Ooops, there was an error!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8141/915bbdc7-c24a-308d-9e36-61ece564ed02.png[/img]

4. 异常对话框

这不是一个完整的异常对话框。但我们可以很容易地将 TextArea 作为可扩展的内容。


Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");

Exception ex = new FileNotFoundException("Could not find file blabla.txt");



版权声明:本文为waterseason原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。