为了账号安全,请及时绑定邮箱和手机立即绑定

如何更改警报对话框的图标?

如何更改警报对话框的图标?

慕仙森 2021-09-29 15:11:08
我想更改以下警报消息的默认图标。我该怎么做?这就是我想要改变的:我想改变图标。这意味着我想将该蓝色图标更改为其他内容。不改变警报类型
查看完整描述

2 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

你有几个选择。

首先,Alert该类AlertType在创建警报时接受一个参数。有 5 个内置选项可供选择,每个选项都有自己的图标:

INFORMATIONCONFIRMATIONWARNINGERRORNONE(根本不提供图标)。

Alert通过将 传递AlertType给构造函数,您可以在创建 时选择以下图标之一:

Alert alert = new Alert(AlertType.ERROR);

//img1.sycdn.imooc.com//615411ae0001ebcb03750215.jpg

但是,如果您想提供自己的图标图像,则可以通过访问dialogPane的Alert并设置graphic属性来实现:


alert.getDialogPane().setGraphic(new ImageView("your_icon.png"));

下面是一个简单的应用程序,它演示了如何为 使用自定义图标图像Alert:


import javafx.application.Application;

import javafx.scene.control.Alert;

import javafx.scene.image.ImageView;

import javafx.stage.Stage;


public class Main extends Application {


    public static void main(String[] args) {

        launch(args);

    }


    @Override

    public void start(Stage primaryStage) {


        // Build the Alert

        Alert alert = new Alert(Alert.AlertType.ERROR);

        alert.setTitle("Alert Test");

        alert.setHeaderText("This uses a custom icon!");


        // Create the ImageView we want to use for the icon

        ImageView icon = new ImageView("your_icon.png");


        // The standard Alert icon size is 48x48, so let's resize our icon to match

        icon.setFitHeight(48);

        icon.setFitWidth(48);


        // Set our new ImageView as the alert's icon

        alert.getDialogPane().setGraphic(icon);

        alert.show();

    }

}

结果Alert:

//img1.sycdn.imooc.com//615411bd00016ea003710213.jpg

注意:正如 Sai Dandem 的同样有效的答案所说明的那样,您不仅限于对ImageView图形使用 。该setGraphic()方法接受任何Node对象,所以你可以很容易地通过一个ButtonHyperlink或其他UI组件。


查看完整回答
反对 回复 2021-09-29
  • 2 回答
  • 0 关注
  • 238 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信