如何设置Dialog控件Java FX/Java 8的图标

Bla*_*dor 17 java icons javafx javafx-8

我可能会遗漏一些非常明显的东西,但我无法找到如何为Dialog组件设置Icon(更准确的是ProgressDialog).我知道如何为舞台做这件事,__CODE__但我没有为Dialog家族找到任何东西.不知何故,设置舞台图标不会影响对话框图标.

谢谢

Jos*_*eda 27

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

对于新对话框(在JDK8u40早期版本中或使用带有JDK 8u25的openjfx 对话框)或者对于ControlsFX中的对话框,为了设置对话框的图标,您可以使用此解决方案:

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

此代码段显示如何使用ProgressDialogControlsFX中的a,并为对话框设置图标:

@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();
}
Run Code Online (Sandbox Code Playgroud)


小智 10

通过将应用程序窗口设置为警报框的所有者,您可以轻松地将应用程序的图标用作警报图标:

@FXML
Button buShow;
...

Alert alert = new Alert(AlertType.INFORMATION, "Nice Box.", ButtonType.CLOSE);
alert.initOwner(buShow.getScene().getWindow());   // Alert uses the Windows Icon
alert.show();
Run Code Online (Sandbox Code Playgroud)


Env*_*ion 8

就这样做:

Alert(AlertType.ERROR, "Erreur de connexion! Verifiez vos Identifiants",FINISH); //Cancel..
setTitle("XNotes FX Erreur");
stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("indiza/XnotesErrorIdz.png")); // To add an icon
showAndWait();
Run Code Online (Sandbox Code Playgroud)

这是结果

在此输入图像描述

**我的朋友,我们做的是计算机科学吗?:不,我们做工艺品**