繁体   English   中英

JavaFX(Java 8 Update 40)警报对话框无法成功重绘和调整大小

[英]JavaFX(Java 8 Update 40) Alert Dialog can't redraw and resize successfully

Java 8 Update 40添加了许多新的JavaFX UI控件,包括警报对话框。 在发布Java 8 Update 40之前,我使用ControlsFX在JavaFX中显示对话框。 在ControlsFX中,有一个“异常对话框”可以显示异常的消息,但是在Java 8官方对话框中,没有任何“异常对话框”。 因此,我想用Java官方对话框自己制作一个“异常对话框”。

我在下面找到了代码:

Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");

Exception ex = new FileNotFoundException("Could not find file blabla.txt");

// Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();

Label label = new Label("The exception stacktrace was:");

TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);

textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);

GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);

// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);
alert.showAndWait();

运行代码时,将显示一个对话框:

没有扩大

然后单击展开按钮,它应显示如下:

正常扩展

但实际上,它是:

错误扩大

此对话框在重绘和调整大小时遇到​​麻烦。

我的操作系统是带有MATE桌面的Linux Mint 17.1 64位。

我不知道是什么导致了这个问题,也许我的X-Window有错误,或者Java 8有错误,或者上面的代码不够严格。 有人遇到过同样的问题吗?

我遇到了同样的问题。 我的丑陋技巧是添加以下代码:

alert.getDialogPane().expandedProperty().addListener((l) -> {
    Platform.runLater(() -> {
        alert.getDialogPane().requestLayout();
        Stage stage = (Stage)alert.getDialogPane().getScene().getWindow();
        stage.sizeToScene();
    });
});

我认为这可能与您编写代码的顺序有关。

尝试这样的事情:

GridPane expContent = new GridPane(); //Initialize the GridPane

//Add children
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);

//set growth
GridPane.setVgrow(exceptionTextArea, Priority.ALWAYS);
GridPane.setHgrow(exceptionTextArea, Priority.ALWAYS);

//Add GridPane to Alert
alert.getDialogPane().setExpandableContent(expContent);

另外,不确定是否需要此功能:

expContent.setMaxWidth(Double.MAX_VALUE);

因为您已经设置了VGrowth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM