ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaFX弹出窗口和消息对话框代码示例

2021-07-02 11:03:39  阅读:365  来源: 互联网

标签:示例 javafx 对话框 JavaFX scene vBox new import stage


弹出窗口

弹窗类

package cn.zxl.AlertWindow;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * @Description: //TODO 弹出窗口、消息对话框
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:50
 * @Version: 1.0
 **/
public class AlertWindow {
    private static boolean res;
    public static boolean display(String title,String msg){
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        Label label = new Label();
        label.setText(msg);
        Button btn1 = new Button("是");
        Button btn2 = new Button("否");
        btn1.setOnMouseClicked(event -> {
            res=true;
            System.out.println("你点击了是");
            stage.close();
        });
        btn2.setOnMouseClicked(event -> {
            res=false;
            System.out.println("你点击了否");
            stage.close();
        });
        VBox vBox = new VBox();
        vBox.getChildren().addAll(label,btn1,btn2);
        //设置居中
        vBox.setAlignment(Pos.CENTER);
        Scene scene = new Scene(vBox,200,200);
        stage.setScene(scene);
        stage.setTitle(title);
        stage.showAndWait();

        return res;
    }
}

弹窗启动类

package cn.zxl.AlertWindow;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @Description: //TODO 主类
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:50
 * @Version: 1.0
 **/
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button btn = new Button("弹出窗口");
        btn.setOnMouseClicked(event -> {
            System.out.println(AlertWindow.display("新窗口", "是否关闭窗口"));
        });
        VBox vBox = new VBox();
        vBox.getChildren().add(btn);
        //设置居中显示
        vBox.setAlignment(Pos.CENTER);
        Scene scene = new Scene(vBox, 400, 400);
        primaryStage.setTitle("弹出窗口示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

最终效果

单击弹出窗口按钮会弹出新窗口。
在这里插入图片描述

在这里插入图片描述

消息对话框

弹窗类

同上。

消息对话框启动类

package cn.zxl.AlertWindow;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @Description: //TODO 消息提示框
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:50
 * @Version: 1.0
 **/
public class Main2 extends Application {
    Stage stage;
    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        //单击系统自带的关闭按钮时也要弹出询问窗口
        stage.setOnCloseRequest(event -> {
            //点击了否,也会关闭窗口,所以要取消默认事件,单击否按钮不会关闭窗口
            event.consume();
            closeWindow();
        });
        Button btn = new Button("关闭窗口");
        btn.setOnMouseClicked(event -> closeWindow());
        VBox vBox = new VBox();
        vBox.getChildren().add(btn);
        //设置居中显示
        vBox.setAlignment(Pos.CENTER);
        Scene scene = new Scene(vBox, 400, 400);
        stage.setTitle("弹出窗口示例");
        stage.setScene(scene);
        stage.show();
    }
    /**
     * //TODO 如果点击了是按钮,就关闭所有窗口
     * @Description:  
     * @Create: 2021/5/27 10:59
     * @Author: zhangxueliang
     * @Param:
     * @Return: 
     */
    private void closeWindow() {
        boolean b = AlertWindow.display("新窗口", "是否关闭窗口");
        if (b) {
            stage.close();
        }
    }

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

最终效果

单击是关闭窗口,单击否不关闭。
并且单击X按钮效果一样。
在这里插入图片描述

在这里插入图片描述

 

标签:示例,javafx,对话框,JavaFX,scene,vBox,new,import,stage
来源: https://blog.51cto.com/u_7692005/2967936

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有