JavaFX 2.1 MessageBox [英] JavaFX 2.1 MessageBox

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

问题描述

美好的一天!

我正在使用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
  • Dialog (and subclasses)

原始答案

以下是模态确认对话框。它的工作原理是创建一个包含场景的舞台,其中包含对话框内容,然后在场景上调用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和wait,只显示一条消息和ok按钮,然后处理应该与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库中的对话框,这是JavaFX中对话框的后续迭代UI控件Sandbox在blo0p3r的答案中提到。

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 MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆