首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX 8如何设置程序图标提醒?

JavaFX 8如何设置程序图标提醒?
EN

Stack Overflow用户
提问于 2017-04-19 20:10:53
回答 4查看 5.4K关注 0票数 7

如何在不使用alert.initOwner()的情况下将程序图标设置为警报?为什么没有initOwner?这是因为在初始化整个窗口之前必须显示一些警告,所以没有场景可以放到initOwner函数中。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-04-20 13:55:53

您可以从警报实例中窃取DialogPane,并将其添加到常规阶段。节点一次只能是一个场景的根,因此您需要首先替换警报场景的根:

代码语言:javascript
复制
public class AlertWithIcon
extends Application {
    @Override
    public void start(Stage stage) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
            "Are you sure you want to delete this item?",
            ButtonType.YES, ButtonType.NO);
        alert.setHeaderText("Delete Item");

        DialogPane pane = alert.getDialogPane();

        ObjectProperty<ButtonType> result = new SimpleObjectProperty<>();
        for (ButtonType type : pane.getButtonTypes()) {
            ButtonType resultValue = type;
            ((Button) pane.lookupButton(type)).setOnAction(e -> {
                result.set(resultValue);
                pane.getScene().getWindow().hide();
            });
        }

        pane.getScene().setRoot(new Label());
        Scene scene = new Scene(pane);

        Stage dialog = new Stage();
        dialog.setScene(scene);
        dialog.setTitle("Delete Item");
        dialog.getIcons().add(new Image("GenericApp.png"));

        result.set(null);
        dialog.showAndWait();

        System.out.println("Result is " + result);
    }
}
票数 9
EN

Stack Overflow用户

发布于 2017-07-08 03:39:46

代码语言:javascript
复制
public class AlertWithIcon
extends Application {
    @Override
    public void start(Stage stage) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
            "Are you sure you want to delete this item?",
        ButtonType.YES, ButtonType.NO);    
       alert.setHeaderText("Delete Item"); 
   ((Stage)alert.getDialogPane().getScene().getWindow()).getIcons().add(new image("GenericApp.png"));
    alert.showAndWait();
}
}
票数 9
EN

Stack Overflow用户

发布于 2017-07-28 02:47:13

这是如何做到的:

代码语言:javascript
复制
// Get the Stage.
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();

// Add a custom icon.
stage.getIcons().stage.getIcons().add(new Image("images/logo_full3.png"));

上面的图像引用可能有问题。但只要它正常工作,就可以尝试配置。我就是这样做的(我使用maven)。如果您不使用maven,您的可能会有所不同。

这里的完整教程:提醒javafx教程

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43505213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档