JavaFX:显示简单消息的最佳方式是什么? [英] JavaFX: what is the best way to display a simple message?

查看:1475
本文介绍了JavaFX:显示简单消息的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有一个Popup类,但你需要设置很多参数来获得一个简单的布局/位置/背景颜色/等等...所以我想知道是否有一个简单的,已经实现的组件如果JavaFX没有提供任何合适的信息,可能会在第三方库中显示消息。

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.

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

Thanks in advance for any help/hints.

推荐答案

ControlsFX是JavaFX 8所以我不能使用它,其他选择几乎与我要做的事情的复杂程度相同。所以我使用实现了我自己的方法来显示信息/警告信息弹出窗口

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.

这是我实现的源代码。只是一些调整显示Popup以窗口为中心,使用CSS并在用户点击它时隐藏。

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.

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:

这篇关于JavaFX:显示简单消息的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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