JavaFX对话框控件-Alert
- 一种可以定制多种行为、外观的弹出框
- 按钮可以控制
- 类型可控制
- 支持展开详情信息展示
常用属性
alertType
弹框类型,不同类型会出现不同小图标,通过构造方法赋值
可以设置graphic,自定义图标
Alert alert = new Alert(Alert.AlertType.ERROR);
// 类型枚举
public static enum AlertType {
NONE,
INFORMATION,
WARNING,
CONFIRMATION,
ERROR
}
title
弹出框标题,标题太长会把超长部分截取,后面加上
……
alert.setTitle("标题");
contentText
弹出框内容,内容太多,不会被截取,会把弹出框撑变形
alert.setContentText("内容信息");
buttonTypes
设置弹框按钮类型列表,可以添加、删除或重新排序按钮,通过构造方法赋值
Alert alert = new Alert(Alert.AlertType.ERROR, "内容信息", ButtonType.OK, ButtonType.CLOSE);
// 按钮类型枚举
public final class ButtonType {
public static final ButtonType APPLY = new ButtonType(
"Dialog.apply.button", null, ButtonData.APPLY);
public static final ButtonType OK = new ButtonType(
"Dialog.ok.button", null, ButtonData.OK_DONE);
public static final ButtonType CANCEL = new ButtonType(
"Dialog.cancel.button", null, ButtonData.CANCEL_CLOSE);
public static final ButtonType CLOSE = new ButtonType(
"Dialog.close.button", null, ButtonData.CANCEL_CLOSE);
public static final ButtonType YES = new ButtonType(
"Dialog.yes.button", null, ButtonData.YES);
public static final ButtonType NO = new ButtonType(
"Dialog.no.button", null, ButtonData.NO);
public static final ButtonType FINISH = new ButtonType(
"Dialog.finish.button", null, ButtonData.FINISH);
public static final ButtonType NEXT = new ButtonType(
"Dialog.next.button", null, ButtonData.NEXT_FORWARD);
public static final ButtonType PREVIOUS = new ButtonType(
"Dialog.previous.button", null, ButtonData.BACK_PREVIOUS);
}
initOwner
设置弹框的所有者,用于确定对话框的位置和模式
alert.initOwner(stage);
graphic
设置弹出框的小图标,否则会采用AlertType默认图标
alert.setGraphic(new ImageView("icon.png"));
headerText
设置弹框的头部信息,否则会使用AlertType默认信息
alert.setHeaderText("headerText信息");
dialogPane
获取 Alert 的内部 DialogPane,允许进一步定制对话框的布局和内容,其中最重要的是
expandableContent
TextArea textArea = new TextArea("展开显示详细内容");
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
alert.getDialogPane().setExpandableContent(textArea);
常用事件
显示事件setOnShowing
alert.setOnShowing((e) -> {
System.out.println(e.getEventType() + " ->" + e.getSource().getClass() + " -> " + e.getTarget().getClass());
});
显示事件setOnShown
alert.setOnShown((e) -> {
System.out.println(e.getEventType() + " ->" + e.getSource().getClass() + " -> " + e.getTarget().getClass());
});
弹框按钮点击
Optional<ButtonType> optional = alert.showAndWait();
System.out.println(optional.map(ButtonType::getText).orElse("无"));
综合案例
public static Parent demo1(Window owner) {
FlowPane flowPane = new FlowPane();
flowPane.setHgap(10);
flowPane.setVgap(10);
flowPane.setOrientation(Orientation.VERTICAL);
Button button1 = new Button("demo1");
button1.setOnMouseClicked((event) -> {
if (MouseButton.PRIMARY.equals(event.getButton())) {
Alert alert = new Alert(Alert.AlertType.ERROR, "内容信息", ButtonType.OK, ButtonType.CLOSE);
alert.initOwner(owner);
alert.setTitle("title信息");
alert.setHeaderText("headerText信息");
alert.setContentText("contentText信息");
alert.setGraphic(new ImageView("icon.png"));
TextArea textArea = new TextArea("展开显示详细内容");
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
alert.getDialogPane().setExpandableContent(textArea);
alert.setOnShowing((e) -> {
System.out.println(e.getEventType() + " ->" + e.getSource().getClass() + " -> " + e.getTarget().getClass());
});
alert.setOnShown((e) -> {
System.out.println(e.getEventType() + " ->" + e.getSource().getClass() + " -> " + e.getTarget().getClass());
});
Optional<ButtonType> optional = alert.showAndWait();
System.out.println(optional.map(ButtonType::getText).orElse("无"));
}
});
flowPane.getChildren().add(button1);
return flowPane;
}
设置弹框在owner的中间
private static void setCenterInOwner(Alert alert, Window owner) {
alert.heightProperty().addListener(it -> {
double centerX = owner.getX() + owner.getWidth() / 2;
double centerY = owner.getY() + owner.getHeight() / 2;
double alertWidth = alert.getWidth();
double alertHeight = alert.getHeight();
double alertScreenX = centerX - alert.getWidth() / 2;
double alertScreenY = centerY - alert.getHeight() / 2;
// 获取屏幕的宽度和高度
double width = Screen.getPrimary().getVisualBounds().getWidth();
double height = Screen.getPrimary().getVisualBounds().getHeight();
if (alertScreenX + alertWidth > width)
alertScreenX = (int) (width - alertWidth);
else if (alertScreenX < 0) {
alertScreenX = 0;
}
if (alertScreenY + alertHeight > height) {
alertScreenY = (int) (height - alertHeight);
} else if (alertScreenY < 0) {
alertScreenY = 0;
}
alert.setX(alertScreenX);
alert.setY(alertScreenY);
});
}