JavaFX:显示简单消息的最佳方式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18669209/
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-08-12 10:00:57  来源:igfitidea点击:

JavaFX: what is the best way to display a simple message?

javauser-interfacejavafx-2javafx

提问by ceklock

In my application I need to display a warning/info message, but I don't know a simple way to do that because there is no JOptionPane or similar component on JavaFX.

在我的应用程序中,我需要显示一条警告/信息消息,但我不知道一种简单的方法来做到这一点,因为 JavaFX 上没有 JOptionPane 或类似的组件。

There is a Popup class, but you have to set many parameters to get a decent layout/position/background color/etc for a simple message... So I want to know if there is a simple, already implemented component to display messages, maybe in a third party library if JavaFX does not offer anything decent.

有一个 Popup 类,但是您必须设置许多参数才能为简单的消息获得合适的布局/位置/背景颜色/等......所以我想知道是否有一个简单的,已经实现的组件来显示消息,如果 JavaFX 不提供任何体面的东西,则可能在第三方库中。

Thanks in advance for any help/hints.

提前感谢您的任何帮助/提示。

采纳答案by ceklock

ControlsFX is JavaFX 8 so I can't use it, and the other alternatives are almost the same level of complexity of what I was going to do. So I implemented my own methods to display an info/warning message using a Popup.

ControlsFX 是 JavaFX 8,所以我无法使用它,其他替代方案的复杂程度几乎与我要做的相同。所以我实现了我自己的方法来使用Popup显示信息/警告消息。

Here is the source code of what I implemented. Just some tweaks to show the Popup centered on the window, use CSS and hide when the user clicks on it.

这是我实现的源代码。只需进行一些调整即可显示以窗口为中心的弹出窗口,使用 CSS 并在用户单击它时隐藏。

public static Popup createPopup(final String message) {
    final Popup popup = new Popup();
    popup.setAutoFix(true);
    popup.setAutoHide(true);
    popup.setHideOnEscape(true);
    Label label = new Label(message);
    label.setOnMouseReleased(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            popup.hide();
        }
    });
    label.getStylesheets().add("/css/styles.css");
    label.getStyleClass().add("popup");
    popup.getContent().add(label);
    return popup;
}

public static void showPopupMessage(final String message, final Stage stage) {
    final Popup popup = createPopup(message);
    popup.setOnShown(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent e) {
            popup.setX(stage.getX() + stage.getWidth()/2 - popup.getWidth()/2);
            popup.setY(stage.getY() + stage.getHeight()/2 - popup.getHeight()/2);
        }
    });        
    popup.show(stage);
}


CSS:


CSS:

.popup {
    -fx-background-color: cornsilk;
    -fx-padding: 10;
    -fx-border-color: black; 
    -fx-border-width: 5;
    -fx-font-size: 16;
}


The pop-up message:

enter image description here


弹出消息:

在此处输入图片说明

回答by user299565

With Java 8u40 and upwards, you don't need to worry. It has JavaFX Dialogs.

使用 Java 8u40 及更高版本,您无需担心。它有JavaFX 对话框

Here is a Link to Learn: http://code.makery.ch/blog/javafx-dialogs-official/

这是一个学习链接:http: //code.makery.ch/blog/javafx-dialogs-official/

Just as simple as:

就这么简单:

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();

回答by Pawe?

Small improvement of ceklock answer
added:
-can't dimiss by click
-auto hide
-change popup position to bottom of window
-you dont have to looking for stage in file, you can pass any control view (button, label, container or what do you want)

JUST CALL:

添加了ceklock 答案的小改进

- 无法通过单击
关闭 - 自动隐藏 - 将
弹出位置更改为窗口底部
- 您不必在文件中查找阶段,您可以传递任何控制视图(按钮、标签、容器或什么你想要)只需

拨打:

Toast.show("YEEEEEEEEEEE", anyControl);



import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;

    public class Toast {

        private static int TOAST_TIMEOUT = 1400;

        private static Popup createPopup(final String message) {
            final Popup popup = new Popup();
            popup.setAutoFix(true);
            Label label = new Label(message);
            label.getStylesheets().add("/css/mainStyles.css");
            label.getStyleClass().add("popup");
            popup.getContent().add(label);
            return popup;
        }

        public static void show(final String message, final Control control) {
            Stage stage = (Stage) control.getScene().getWindow();
            final Popup popup = createPopup(message);
            popup.setOnShown(e -> {
                popup.setX(stage.getX() + stage.getWidth() / 2 - popup.getWidth() / 2);
                popup.setY(stage.getY() + stage.getHeight() / 1.2 - popup.getHeight() / 2);
            });
            popup.show(stage);

            new Timeline(new KeyFrame(
                    Duration.millis(TOAST_TIMEOUT),
                    ae -> popup.hide())).play();
        }

css:

css:

.popup {
    -fx-background-color: cornsilk;
    -fx-padding: 10;
    -fx-border-color: black; 
    -fx-border-width: 5;
    -fx-font-size: 16;
}