繁体   English   中英

你如何设置一个对话框控件的图标 Java FX/Java 8

[英]How do you set the icon of a Dialog control Java FX/Java 8

我可能遗漏了一些非常明显的东西,但我不知道如何为 Dialog 组件设置图标(更准确地说是 ProgressDialog)。 我知道如何为 Stage 做到这一点:

this.primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/Logo.png")));

但我没有为 Dialog 家族找到任何东西。 不知何故,设置舞台图标不会影响对话框图标。

谢谢

Marco Jakob 在这里有一个很棒的教程,您不仅可以在其中找到如何使用对话框,还可以找到如何解决您的问题。

无论是对于新对话框(在 JDK8u40 早期版本中还是在 JDK 8u25 中使用 openjfx - dialogs),或者对于那些在ControlsFX中的对话框,为了设置对话框的图标,您都可以使用以下解决方案

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

此代码片段显示了如何使用 ControlsFX 中的ProgressDialog并为对话框设置图标:

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

就这样做:

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

这是结果

在此处输入图片说明

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

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

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

这是我在 JavaFX 项目中包含的一种方法,只需调用此方法并将 Alert 作为参数传递即可设置标题栏图标和标题图形。

public class Msg {

public void showInfo(String title, String header, String message) {
    Alert alertShowInfo = new Alert(Alert.AlertType.INFORMATION);
    addDialogIconTo(alertShowInfo); //add icon and header graphic
    alertShowInfo.setTitle(title);
    alertShowInfo.setHeaderText(header);
    alertShowInfo.setContentText(message);
    alertShowInfo.showAndWait();
}

//this adds images to Alert
public void addDialogIconTo(Alert alert) {
    // Add custom Image to Dialog's title bar
    final Image APPLICATION_ICON = new Image("icon.png");
    Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
    dialogStage.getIcons().add(APPLICATION_ICON);

    // Add custom ImageView to Dialog's header pane.        
    final ImageView DIALOG_HEADER_ICON = new ImageView("icon.png");
    DIALOG_HEADER_ICON.setFitHeight(48); // Set size to API recommendation.
    DIALOG_HEADER_ICON.setFitWidth(48);
    alert.getDialogPane().setGraphic(DIALOG_HEADER_ICON);                       
    }
}

然后,在我希望使用警报的任何类中,它都已经具有自定义图标和标题图形。

public static void main(String[] args){
        Msg msg = new Msg();

        // Alert will now include custom icon and header graphic.
        msg.showInfo("Sucess!", "Program succeeded", "Now exiting program");
} 

与任何对话框类似,而是在按钮处理程序中。

    Alert alert = new Alert(
        AlertType.WARNING,
        "Alert message here.",
        ButtonType.OK
    );
    alert.initOwner(((Button)event.getSource()).getScene().getWindow());
    alert.setTitle("Alert window title");
    alert.showAndWait();

暂无
暂无

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

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