如何更改警报对话框的图标?
How do I change the icon of an Alert dialog?
我想更改以下警告消息的默认图标。我该怎么做?
这是我要更改的内容:
我想更改图标。这意味着我想将那个蓝色图标更改为其他图标。不改变警报类型
除了@Zephyr 已经提到的,如果您想在屏幕截图中指向的地方设置自己的自定义 icon/graphic,请使用 javafx.scene.control.Dialog
[= 的 setGraphic() 方法20=]。
在下面的代码中,虽然 alertType 是 INFORMATION,它会用提供的图形节点覆盖预定义的图标。
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("My Title");
alert.setContentText("My Content text");
alert.setHeaderText("My Header");
alert.setGraphic(new Button("My Graphic"));
alert.show();
你有几个选择。
首先,Alert
class 在创建警报时接受一个 AlertType
参数。有 5 个内置选项可供选择,每个选项都有自己的图标:
INFORMATION
、CONFIRMATION
、WARNING
、ERROR
和 NONE
(根本不提供任何图标)。
通过将 AlertType
传递给构造函数来创建 Alert
时,您可以 select 这些图标之一:
Alert alert = new Alert(AlertType.ERROR);
但是,如果您想提供自己的图标图像,可以通过访问 Alert
的 dialogPane
并设置 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 组件。
我想更改以下警告消息的默认图标。我该怎么做?
这是我要更改的内容:
我想更改图标。这意味着我想将那个蓝色图标更改为其他图标。不改变警报类型
除了@Zephyr 已经提到的,如果您想在屏幕截图中指向的地方设置自己的自定义 icon/graphic,请使用 javafx.scene.control.Dialog
[= 的 setGraphic() 方法20=]。
在下面的代码中,虽然 alertType 是 INFORMATION,它会用提供的图形节点覆盖预定义的图标。
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("My Title");
alert.setContentText("My Content text");
alert.setHeaderText("My Header");
alert.setGraphic(new Button("My Graphic"));
alert.show();
你有几个选择。
首先,Alert
class 在创建警报时接受一个 AlertType
参数。有 5 个内置选项可供选择,每个选项都有自己的图标:
INFORMATION
、CONFIRMATION
、WARNING
、ERROR
和 NONE
(根本不提供任何图标)。
通过将 AlertType
传递给构造函数来创建 Alert
时,您可以 select 这些图标之一:
Alert alert = new Alert(AlertType.ERROR);
但是,如果您想提供自己的图标图像,可以通过访问 Alert
的 dialogPane
并设置 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 组件。