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

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

问题描述

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



考虑下面的代码示例。如何显示一个不仅仅是几个字的完整信息?我的消息( contentText 属性)最后使用 ... 进行剪切,并且Alert不会正确调整其大小在我看来,



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



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



编辑:Windows 7和Linux(Oracle的JDK)截图:

  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(我的标题);
a.setHeaderText(我的标题文本);
a.setResizable(true);
String version = System.getProperty(java.version);
String content = String.format(Java:%s.\\\
This是一个长文本Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tim invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua。在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 tim 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();
}
}


解决方案

是没有魔术数字,调整大小等的更好的解决方法:

 警报警报=新警报(AlertType.ERROR,内容文本 ); 
alert.getDialogPane()。getChildren()。stream()。filter(node - > node instanceof Label).forEach(node - >((Label)node).setMinHeight(Region.USE_PREF_SIZE));

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


Recently, JavaFX introduced Alerts (Java 8u40).

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.

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.

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

解决方案

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.

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

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