如何设置Dialog控件Java FX / Java 8的图标 [英] How do you set the icon of a Dialog control Java FX/Java 8

查看:1072
本文介绍了如何设置Dialog控件Java FX / Java 8的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会遗漏一些非常明显的东西,但我无法找到如何为Dialog组件设置Icon(更准确地说ProgressDialog)。我知道如何为舞台做这件事, this.primaryStage.getIcons()。add(new Image(getClass()。getResourceAsStream(/ icon / Logo.png))); 但我找不到Dialog系列的任何内容。不知何故,设置舞台图标不会影响对话框图标。

I might be missing something very obvious, but I can't find out how to set the Icon for a Dialog component (ProgressDialog to be more precise). I know how to do that for a Stage, this.primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/Logo.png"))); but I don't find anything for the Dialog family. And somehow, setting the Stage Icon does not influence the Dialog Icon.

谢谢

推荐答案

Marco Jakob有一个很好的教程这里,你不仅可以找到如何使用对话框,以及如何解决您的问题。

There's an excellent tutorial here by Marco Jakob, where you can find not only how to use dialogs, but also how to solve your problem.

两个新对话框(在JDK8u40早期版本中或使用 openjfx-dialogs ,或 ControlsFX 中的那些为了设置对话框的图标,您可以使用此解决方案

Both for the new dialogs (in JDK8u40 early versions or with openjfx-dialogs with JDK 8u25), or for those in ControlsFX, in order to set the icon of your dialog, you can use this solution:

Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(
    new Image(this.getClass().getResource("<image>.png").toString()));

此代码段显示如何使用 ProgressDialog ,来自ControlsFX,并为对话框设置一个图标:

This code snippet shows how to use a ProgressDialog, from ControlsFX, and set an icon for the dialog:

@Override
public void start(Stage primaryStage) {

    Service<Void> service = new Service<Void>() {
        @Override protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override protected Void call() throws InterruptedException {
                    updateMessage("Message . . .");
                    updateProgress(0, 10);
                    for (int i = 0; i < 10; i++) {
                        Thread.sleep(300);
                        updateProgress(i + 1, 10);
                        updateMessage("Progress " + (i + 1) + " of 10");
                    }
                    updateMessage("End task");
                    return null;
                }
            };
        }
    };

    Button btn = new Button("Start Service");
    btn.setOnAction(e -> {
        ProgressDialog dialog = new ProgressDialog(service);
        dialog.setTitle("Progress Dialog");
        dialog.setHeaderText("Header message");
        Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("<image>.png").toString()));
        service.start();
    });

    Scene scene = new Scene(new StackPane(btn), 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于如何设置Dialog控件Java FX / Java 8的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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