JavaFX 警报及其大小

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

JavaFX Alerts and their size

javajavafxdialogalert

提问by Spiegelritter

Recently, JavaFX introduced Alerts (Java 8u40).

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

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

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

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.

在我使用 Oracle JDK 8u40 的 Linux 机器上,我只看到文本This is a long text. Lorem ipsum dolor sit amet,在某些情况下太短了。

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.

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

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

编辑:Windows 7 和 Linux(来自 Oracle 的 JDK)的屏幕截图: 视窗警报Linux警报

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.\nThis 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();
    }
}

采纳答案by Clairton Luz

I have made the following workaround:

我做了以下解决方法:

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.

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

回答by Maxim

I have made the following workaround sometime ago:

我前一段时间做了以下解决方法:

Alert dialog = new Alert(Alert.AlertType.ERROR);
dialog.setHeaderText("Connection Failed");
dialog.setContentText(this.getException().getMessage());

//FIXME: Remove after release 8u40
dialog.setResizable(true);
dialog.getDialogPane().setPrefSize(480, 320);

dialog.showAndWait();

As you can see I just set resizable flag and set preferred size.

如您所见,我只是设置了可调整大小的标志并设置了首选大小。

But this is strange because this bug should be fixedin 8u40. Are you using latest build of 8u40?

但这很奇怪,因为这个错误应该在 8u40 中修复。您使用的是最新版本的 8u40 吗?

UPDATE:

更新

Not fixed in 8u40. Should be fixedlater.

8u40 中未修复。以后应该修复

回答by Sergio

Here is the better workaround without magic numbers, resizing etc.:

这是没有幻数,调整大小等的更好的解决方法:

Alert alert = new Alert(AlertType.ERROR, "content text");
alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label)node).setMinHeight(Region.USE_PREF_SIZE));

This solution works under Windows, Linux and Mac.

此解决方案适用于 Windows、Linux 和 Mac。

回答by Alexiy

Another solution is subclassing the Alert and applying desired style there, for example:

另一个解决方案是子类化 Alert 并在那里应用所需的样式,例如:

class SubAlert extends Alert
{
    {
        setHeaderText("");
        getDialogPane().getStylesheets().add("some_stylesheet");
        getDialogPane().getStyleClass().add("style_class");
        getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
    }

    SubAlert(AlertType alertType)
    {
        super(alertType);
    }
    SubAlert(AlertType type,String title,String content)
    {
        super(type);
        setTitle(title);
        setContentText(content);
    }
}

This way you don't have to repeat actions for every Alert you create.

这样您就不必为您创建的每个警报重复操作。