繁体   English   中英

如何调整警报窗口的大小以匹配 JavaFX 8 中的内容

[英]How to resize an Alert window to match contents in JavaFX 8

有什么方法可以在 JavaFX 8 中调整警报大小以显示异常痕迹。我的代码是:

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setResizable(true);
alert.setTitle(e.getLocalizedMessage());
alert.setHeaderText(e.toString());
alert.setContentText(message); // I previously made this using StackTraceElement[] java.lang.Exception.getStackTrace()

然后我都试过:

alert.getDialogPane().getScene().getWindow().sizeToScene();

alert.getDialogPane().autosize();

两者都没有奏效。

目前,我使用的 JRE 是 Java 8 update 144,我的 JDK 是 Windows 10 Home 64 位版本上的 Java 8 update 101。 有什么方法可以让我使用 Alert 框架来替代 JOptionPane,因为 Swing 基本上已经死了,这样我就可以避免编写侦听器和按钮并保留 Alert 类“提供”的十字图标。

此代码是从Exception Dialog 中提取的。

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();

在此处输入图片说明

我找到了一种使用 Java 反射来做到这一点的方法。 首先必须设置(Ervin Szilagyi 的评论) alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);

然后使用这个方法:

void sizeToScene(Alert alert) {
    try {
        final Field field = Dialog.class.getDeclaredField("dialog");
        field.setAccessible(true);
        final Object object = field.get(alert);
        final Method method = object.getClass().getMethod("sizeToScene");
        method.setAccessible(true);
        method.invoke(object);
    } catch (SecurityException | IllegalArgumentException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            //  logger.catching(e);
    }
}

但最好使用异常对话框来显示异常的跟踪。

用法示例:

主线程

private static Alert alert;

public static Optional<Alert> getAlert(){
    return Optional.ofNullable(alert);
}

void alertMethod(){
    alert = new Alert(AlertType.ERROR);
    alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
    alert.setTitle("title");
    alert.setHeaderText(null);
    alert.setContentText("message");
    alert.showAndWait();
}

另一个线程

//...
void addAlertText(String text){
    MainThread.getAlert()
    .ifPresent(
        al->Platform.runLater(()->{
            al.setContentText(al.getContentText() + '\n' + text);
            sizeToScene(al);
        }));
}
//...

暂无
暂无

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

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