猿问

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

我想更改以下警报消息的默认图标。我该怎么做?

这就是我想要改变的:

我想改变图标。这意味着我想将该蓝色图标更改为其他内容。不改变警报类型


慕仙森
浏览 239回答 2
2回答

扬帆大鱼

你有几个选择。首先,Alert该类AlertType在创建警报时接受一个参数。有 5 个内置选项可供选择,每个选项都有自己的图标:INFORMATION、CONFIRMATION、WARNING、ERROR和NONE(根本不提供图标)。Alert通过将 传递AlertType给构造函数,您可以在创建 时选择以下图标之一:Alert alert = new Alert(AlertType.ERROR);但是,如果您想提供自己的图标图像,则可以通过访问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:注意:正如 Sai Dandem 的同样有效的答案所说明的那样,您不仅限于对ImageView图形使用 。该setGraphic()方法接受任何Node对象,所以你可以很容易地通过一个Button,Hyperlink或其他UI组件。
随时随地看视频慕课网APP

相关分类

Java
我要回答