用JavaFx实现一个登录器

业务场景:

公司要求一个账号绑定一台电脑,以后该账号只能通过绑定的这台电脑登录访问web


思路:

通过客户端登录器拿到用户电脑主板编号,登录的时候带着主板编号到服务端进行验证,对于只想实现这个功能而没有时间深入了解JavaFx的小伙伴非常适合。


开干:

1、写一个html页面,不过这里是基于JavaFx实现,文件名为sample.fxml,类似于.html

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

<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<Pane id="PaneB" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="470.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <TextField fx:id="userName" layoutX="163.0" layoutY="183.0" prefHeight="45.0" prefWidth="350.0" promptText="请输入手机号" />
      <Pane fx:id="bgp" prefHeight="146.0" prefWidth="640.0">
         <children>
            <Button fx:id="minimizeButton" layoutX="570.0" layoutY="-1.0" mnemonicParsing="false" text="一" />
            <Button fx:id="closeButton" layoutX="605.0" layoutY="-1.0" mnemonicParsing="false" text="X" />
         </children>
      </Pane>
      <PasswordField fx:id="pwd" layoutX="163.0" layoutY="250.0" prefHeight="45.0" prefWidth="350.0" promptText="请输入密码" />
      <Hyperlink fx:id="register" layoutX="368.0" layoutY="313.0" text="没有账号?立即注册" />
      <Button fx:id="login" layoutX="163.0" layoutY="370.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="350.0" styleClass="button-Login" text="登 录" />
      <CheckBox fx:id="remember" layoutX="163.0" layoutY="317.0" mnemonicParsing="false" text="记住密码"/>
      <Text fx:id="telTips" layoutX="28.0" layoutY="212.0" strokeType="OUTSIDE" strokeWidth="0.0" />
      <Text fx:id="pwdTips" layoutX="52.0" layoutY="279.0" strokeType="OUTSIDE" strokeWidth="0.0" />
   </children>
</Pane>

对于fxml不熟也没关系,我们可以用Scene Builder工具帮我们实现

2、编写fxml的css属性,看到了css是不是觉得很熟悉呢,没错就是你想想的那样,login.css

#bgp {-fx-background-image: url("6.png");}
#closeButton {-fx-background-color: transparent;-fx-text-fill: white;}
#minimizeButton {-fx-background-color: transparent;-fx-text-fill: white;}
#PaneB{-fx-border-color: #68ADE7;}
#closeButton:hover {-fx-font-size: 16px;}
#minimizeButton:hover {-fx-font-size: 16px;}

.button-Login {
    -fx-text-fill: white;
    -fx-font-family: "Arial Narrow";
    -fx-font-weight:bold;
    -fx-font-size: 30px;
    -fx-background-color: #69AEE8;
    -fx-effect: dropshadow(three-pass-box, rgba(0, 0, 0, 0.6), 5, 0.0, 0, 1);
}

.button-Login:hover {-fx-background-color: #2D8FDE;}

写完css就可以看到效果图了,不过这个时候还没有实现功能

3、实现相应的功能

定义相关事件,@FXML是javafx的特性

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;


public class Controller {

    @FXML
    Button closeButton;

    @FXML
    Button minimizeButton;

    @FXML
    Hyperlink register;

    @FXML
    TextField userName;

    @FXML
    PasswordField pwd;

    @FXML
    CheckBox remember;

    @FXML
    Button login;

    @FXML
    Text telTips;

    @FXML
    Text pwdTips;

    @FXML
    Pane bgp;

}

4、定义一个Main方法,在这里写具体的实现

public static void main(String[] args) {
    // 启动登录器
    launch(args);
}

  在启动的同时,获取计算机主板信息

public static String getMotherboardSN() {
    String result = "";
    try {
        File file = File.createTempFile("realhowto", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n"
                + "   (\"Select * from Win32_BaseBoard\") \n"
                + "For Each objItem in colItems \n"
                + "    Wscript.Echo objItem.SerialNumber \n"
                + "    exit for  ' do the first cpu only! \n" + "Next \n";

        fw.write(vbs);
        fw.close();
        String path = file.getPath().replace("%20", " ");
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + path);
        BufferedReader input = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            result += line;
        }
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

点击登录按钮,校验参数是否符合要求,符合通过http连接服务器验证 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值