本文介绍了JavaFX 2.1 消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!
我正在使用 JavaFX SDK 开发程序.我想要一个像 C# 一样的消息框:

Good day!
I am developing a program using JavaFX SDK. I wanted to have a message box like in C#:

DialogResult rs = MessageBox.showDialog("Message Here...");
if (rs == ....) {
    // code
}

我想使用 JavaFX SDK 来实现这样的功能.非常感谢您的回答.

I want to have a functionality like this using JavaFX SDK. Answers are very much appreciated.

推荐答案

更新

从 Java8u40 开始,核心 JavaFX 库包括对话框(消息框)功能.请参阅以下类的文档:

As of Java8u40, the core JavaFX libraries include dialog (message box) functionality. Refer to the documentation for the following classes:

有关如何使用 Alert 类的快速信息,请参阅此问题的其他答案:

For quick info on how to use the Alert class, refer to other answers to this question:

如需更长的教程,请参阅Makery JavaFX 对话教程(强烈推荐本教程).

For a longer tutorial, refer to the Makery JavaFX dialog tutorial (this tutorial is highly recommended).

原始答案

以下是模态确认对话框的示例.它的工作原理是创建一个包含包含对话内容的场景的舞台,然后在场景上调用 show().

Here is an example of a Modal Confirm dialog. It works by creating a Stage containing a Scene with the dialog contents in it, and then calling show() on the Scene.

如果您希望主处理线程在新舞台显示时暂停,并且您使用的是 JavaFX 2.2+,那么您可以在舞台上调用 showAndWait() 而不是 show.修改为使用 show 和 wait 并只显示消息和确定按钮,然后处理的行为应该与 C# MessageBox 非常相似.

If you want the main processing thread to pause whilst the new Stage is shown and you are using JavaFX 2.2+, then you can call showAndWait() on the Stage rather than show. Modified to use show and wait and just display a message and ok button, then processing should act quite similar to a C# MessageBox.

如果您想要一个专业的 Java 8 消息框,我建议使用来自 ControlsFX 库,这是 blo0p3r 的回答中提到的 JavaFX UI Controls Sandbox 中对话框的后续迭代.

If you want a professional looking message box for Java 8, I recommend using the dialogs from the ControlsFX library, which is a later iteration of the dialogs in the JavaFX UI Controls Sandbox mentioned in blo0p3r's answer.

这篇关于JavaFX 2.1 消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:54
查看更多