java - 如何更改警报对话框的图标?
解决方案
你有几个选择。
首先,该类在创建警报时Alert
接受一个参数。AlertType
有 5 个内置选项可供选择,每个选项都有自己的图标:
INFORMATION
, CONFIRMATION
, WARNING
, ERROR
, and 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 组件。
推荐阅读
- node.js - Express 服务器:将现有文档克隆到另一个集合中
- vb.net - 如何获取字符串中的第一个数字和第二个数字?
- python - 如何检查 post_save 中的模型字段是否已更改?
- java - 如何为具有 Java Spring 安全性的用户授予权限
- node.js - 在 Azure SDK 中查询节点的可用位置
- ios - iOS 分发证书已被吊销。引起什么问题?
- c# - 时间:2019-05-10 标签:c#不断计算一些不同于它应该的东西
- c - 使用指针访问元素
- javascript - 从 HTML 下拉列表中选择值
- mysql - MYSQL查询以获取一列在不同组中多次出现并匹配某些条件的所有结果