javaFX 提示框

java FX提示框


            Alert alert = new Alert(AlertType.INFORMATION);
            alert.titleProperty().set("信息");
            alert.headerTextProperty().set("请先打开一个word文档再点编辑按钮");
            alert.showAndWait();
### 如何在JavaFX中创建消息框 为了实现消息框功能,在JavaFX应用程序中通常使用`Alert`类。此方法提供了一种简单而有效的方式来显示不同类型的对话框给用户。 下面是一个简单的例子,展示了如何利用`Alert`来展示一个带有确认按钮的信息提示框: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class MessageBoxExample extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Show Message Box"); // 创建场景并将按钮添加到其中 StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); // 设置按钮点击事件处理程序 btn.setOnAction(event -> showMessage()); primaryStage.setTitle("Message Box Example"); primaryStage.setScene(scene); primaryStage.show(); } private void showMessage() { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Information Dialog"); alert.setHeaderText(null); alert.setContentText("This is an information dialog!"); alert.showAndWait(); // 显示对话框并等待关闭 } } ``` 这段代码定义了一个名为`MessageBoxExample`的应用程序类,该类继承自`Application`。当启动这个应用时,会显示出一个包含单个按钮的窗口;一旦按下按钮,则调用`showMessage()`函数以弹出信息型的消息框[^1]。 此外,还可以通过更改传递给`Alert`构造器的第一个参数来自定义对话框类型,例如警告(`WARNING`)、错误(`ERROR`)或询问(`CONFIRMATION`)等不同类型的通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值