javafx 自定义对话框

1、写fxml如下

本人css不怎么会玩,请见谅

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.control.TextField?>

<GridPane maxWidth="400" maxHeight="300" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.CheckActiveController">
   <children>
      <VBox prefWidth="400" style="-fx-padding:10;">
         <HBox VBox.vgrow="ALWAYS" alignment="CENTER_LEFT">
            <Label  prefHeight="23.0" prefWidth="400" text="授权验证" textAlignment="CENTER"
                   style="-fx-font-size:16px;-fx-text-fill:Black;"
            />
            <ImageView fx:id="iv_dialog_close" fitHeight="20" fitWidth="20" onMouseClicked="#dialogClose" >
               <image>
                  <Image url="@../images/close.png"/>
               </image>
            </ImageView>
         </HBox>

         <Label VBox.vgrow="ALWAYS" prefHeight="23.0"  text="活动授权码:" textAlignment="CENTER"
                 style="-fx-padding:10 0 10 20;-fx-font-size:16px;-fx-text-fill:#555;"
         />
         <HBox  prefWidth="400"  style="-fx-padding:10 20 10 20;">
            <TextField fx:id="tf_check" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" alignment="CENTER" promptText="请输入活动授权码"
                       style="-fx-font-size:14px;-fx-text-fill:#555;"
            />
         </HBox>
         <HBox VBox.vgrow="ALWAYS" alignment="CENTER_RIGHT" style="-fx-padding:10 20 10 20;">
            <JFXButton fx:id="btn_dialog_sure" onMouseClicked="#dialogSure"
                       buttonType="RAISED"  prefHeight="35" prefWidth="100" style="-fx-font-size:14px;-fx-background-color:#CCCCFF;" text="确定" HBox.hgrow="ALWAYS" VBox.vgrow="NEVER" />
         </HBox>
      </VBox>
   </children>
</GridPane>

2、创建对话框

本对话框使用的是第三方对话框,jar包导入请参看以前的文章
在这里插入图片描述
创建代码如下

 /**
     * 检测授权码
     */
    private void showCheckDialog() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Start.class.getResource("fxml/check_active.fxml"));  //静态读取,无法获取controller
        try {
            if(loader == null){
                return;
            }
            GridPane  rootLayout = loader.load();

            JFXAlert alert = new JFXAlert(primaryStage);
            alert.initModality(Modality.APPLICATION_MODAL);
            alert.setOverlayClose(false);
            alert.setContent(rootLayout);
            alert.show();

            CheckActiveController checkActiveController= loader.getController();
            checkActiveController.init(primaryStage);

            checkActiveController.close(alert);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

CheckActiveController代码如下

package sample.controller;

import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class CheckActiveController {
    private Stage primaryStage;

    @FXML
    private ImageView iv_dialog_close;
    @FXML
    private TextField tf_check;
    @FXML
    private JFXButton btn_dialog_sure;

    public void init(Stage primaryStage){
        this.primaryStage=primaryStage;
    }

    /**
     * 关闭对话框
     */
    @FXML
    public void dialogClose(){
        if(alert!=null){
            alert.hide();
        }
    }

    @FXML
    public void dialogSure(){
        if(alert!=null) {
            alert.hide();
        }
        check();
    }


    private JFXAlert alert;
    public void close(JFXAlert alert){
        this.alert=alert;
    }


    /**
     * 检查验证码
     */
    private void check() {
    }

}

效果如下

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值