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);
        });
    }
JavaFX提供了许多对话框类型,包括警告对话框、错误对话框、确认对话框、文本输入对话框等等。这些对话框都是通过JavaFX的Dialog类创建的。 下面是一个简单的示例代码,展示了如何创建一个警告对话框: ```java Alert alert = new Alert(AlertType.WARNING); alert.setTitle("警告对话框"); alert.setHeaderText("警告内容"); alert.setContentText("这是一个警告信息"); alert.showAndWait(); ``` 在这个例子中,我们首先创建了一个Alert对象,并将其类型设置为WARNING。然后设置对话框的标题、头部信息和内容。最后调用showAndWait()方法来显示对话框。 除了警告对话框,还可以创建其他类型的对话框。例如,要创建一个确认对话框,可以将Alert的类型设置为CONFIRMATION,并设置对话框的标题、头部信息和内容。然后,可以使用showAndWait()方法来显示对话框,并根据用户的响应来进行相应的操作。 ```java Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("确认对话框"); alert.setHeaderText("确认内容"); alert.setContentText("您确认要执行此操作吗?"); Optional<ButtonType> result = alert.showAndWait(); if (result.get() == ButtonType.OK){ // 用户点击了“确认”按钮 } else { // 用户点击了“取消”按钮 } ``` 在这个例子中,我们使用了一个Optional<ButtonType>类型的对象来存储用户的响应。如果用户点击了“确认”按钮,那么就可以执行相应的操作;如果用户点击了“取消”按钮,就不需要执行任何操作。 总的来说,JavaFX对话框功能非常强大,可以帮助我们方便地实现各种对话框的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值