java 那么如何在 JavaFX 中显示消息框呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38101332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:07:03  来源:igfitidea点击:

So how to display message box in JavaFX?

javajavafxmessageboxcontrolsfx

提问by Dims

I have read this: http://code.makery.ch/blog/javafx-dialogs-official/

我读过这个:http: //code.makery.ch/blog/javafx-dialogs-official/

I don't think 40 lines of code is acceptable to display simple exception message dialog box.

我认为显示简单的异常消息对话框不能接受 40 行代码。

So, how to display dialog boxes in JavaFX? May be ControlsFX can help?

那么,如何在JavaFX中显示对话框呢?也许 ControlsFX 可以提供帮助?

UPDATE

更新

Compare how it was done in Swing:

比较它是如何在 Swing 中完成的:

JOptionPane.showMessageDialog(frame, 
    "Eggs are not supposed to be green.",
    "Inane error",
    JOptionPane.ERROR_MESSAGE);

It is

它是

ONE

LINE

线

OF

CODE

代码

This is more than enough.

这已经绰绰有余了。

回答by explv

You just need to create a new Alertwith its content set to a TextAreainside of a ScrollPane, and then add your exception text to the TextArea

您只需要创建一个新的Alert,其内容设置为 a 的TextArea内部ScrollPane,然后将您的异常文本添加到TextArea

Exception e = new Exception("An exception!!!!!!!!!!!!!!!!!");
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("An exception occurred!");
alert.getDialogPane().setExpandableContent(new ScrollPane(new TextArea(sw.toString())));
alert.showAndWait();

UPDATEto match OP's update:

更新以匹配 OP 的更新:

The equivelant in JavaFX to your Swing example is:

JavaFX 中与 Swing 示例的等效项是:

new Alert(Alert.AlertType.ERROR, "This is an error!").showAndWait();