JavaFx之模态窗口(二十六)

JavaFx之模态窗口(二十六)

模态窗口:在场景A打开场景B,则A场景无法选择和操作,只能操作B
设置方式,在场景B初始化时设置
stage.initModality(Modality.APPLICATION_MODAL);
简单例示:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * @author lingkang
 * Created by 2022/8/15
 */
public class Demo02 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("主窗口");
        primaryStage.setHeight(200);
        primaryStage.setWidth(350);

        Button button = new Button("打开模态框!");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage dialog = new Stage();
                // 模态框基本设置
                dialog.setWidth(400);
                dialog.setHeight(300);
                dialog.setScene(new Scene(new HBox(new Label("模态框"))));

                dialog.initOwner(primaryStage);
                dialog.initModality(Modality.NONE);// 可以选择不同类型的模态框
                dialog.showAndWait();// 显示
            }
        });

        primaryStage.setScene(new Scene(button));
        // 显示主页面
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在这里插入图片描述
可以看到,并没有打开多个底部窗口。

posted @ 2022-09-16 00:07  凌康  阅读(116)  评论(0编辑  收藏  举报