2 回答
TA贡献1799条经验 获得超9个赞
你有几个选择。
首先,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组件。
添加回答
举报