JavaFX 警报及其大小 [英] JavaFX Alerts and their size

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

问题描述

最近,JavaFX 引入了警报 (Java 8u40).

Recently, JavaFX introduced Alerts (Java 8u40).

考虑下面的代码示例.如何显示比几个单词还长的完整消息?我的消息(contentText 属性)最后被 ... 截断,我认为警报没有正确调整其大小.

Consider the code example below. How can I display a full message that is longer than just a few words? My messages (contentText property) get cut at the end with ... and the Alert does not adjust its size properly in my opinion.

在我使用 Oracle JDK 8u40 的 Linux 机器上,我只看到文本 这是一个长文本.Lorem ipsum dolor sat amet,在某些情况下太短了.

On my Linux machine with Oracle JDK 8u40, I only see the text This is a long text. Lorem ipsum dolor sit amet, which is too short in some cases.

当然,用户可以手动调整警报窗口的大小并相应地显示文本,但这对用户来说根本不友好.

Of course, the user can resize the Alert window manually and the text will be displayed accordingly, but that is not user-friendly at all.

Windows 7 和 Linux(来自 Oracle 的 JDK)的屏幕截图:

Screenshots for Windows 7 and Linux (JDK from Oracle):

import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;


public class TestAlert extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Alert a = new Alert(AlertType.INFORMATION);
        a.setTitle("My Title");
        a.setHeaderText("My Header Text");
        a.setResizable(true);
        String version = System.getProperty("java.version");
        String content = String.format("Java: %s.
This is a long text. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", version);
        a.setContentText(content);
        a.showAndWait();
    }
}

推荐答案

我做了以下解决方法:

Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();

所以窗口会根据内容自动调整大小.

So the window will resize automatically according to the content.

这篇关于JavaFX 警报及其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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