授权工具开发

开发一个授权工具
一、需求

开发一个用于授权的小工具,由于项目部署需要进行授权,生成授权文件的代码是一个main方法,所以有这么一个需求,要把授权的功能做成一个授权工具,以后移交给实施人员进行授权,因此需要开发一个授权工具

二、设计思路

本人熟悉java语言,并且授权功能是基于java开发的,所以我们需要利用java的awt功能开发一个授权界面,并且将授权功能迁移到该项目中即可

三、项目搭建
1、创建一个java项目

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

2、构建artifacts

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

3、打包

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

四、核心代码
package com.superred.th.license.active;


import com.superred.th.license.LicGen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author hdqzz
 */
public class ToolFrame extends JFrame implements ActionListener {
    private static final long serialVersionUID = -1L;
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private StringBuffer helpMessage = new StringBuffer()
            .append("操作步骤.\n")
            .append("1. 填写机器码\n")
            .append("2. 修改授权日志如下2024-12-31\n")
            .append("3. 点击按钮生成授权码\n")
            .append("4. 点击Tool中生成授权文件按钮生成授权文件\n\n");


    /**
     * 定义输入框字段
     *
     **/
    JTextField tfSystemId = new JTextField();
    JTextField tfExpireTime = new JTextField();
    JTextField tfActivationCode = new JTextField();

    /**
     * 定义按钮和菜单按钮
     **/
    JButton jbActive = new JButton("生成授权码");
    JTextArea taInfo = new JTextArea(helpMessage.toString());

    JMenuItem runMenu = new JMenuItem("生成授权码");
    JMenuItem saveProperitiesMenu = new JMenuItem("保存License");
    JMenuItem exitMenu = new JMenuItem("Exit");


    private String lastLicenseData = null;

    /**
     * 创建授权工具框架
     * @Author zxy
     * @Date 17:43 2023/3/8
     * @return
     **/
    public ToolFrame() {
        super("授权工具");
        initFrame();
        setDefaultCloseOperation(3);
        setSize(800, 600);
        setVisible(true);
    }

    /**
     * 初始化菜单上部授权区域和下部中心区域
     * @Author zxy
     * @Date 17:43 2023/3/8
     * @return void
     **/
    public void initFrame() {
        initMenu();
        initTopPanel();
        initCenterPanel();
    }

    /**
     * 组装菜单列表Operate和Tools菜单列表
     * @Author zxy
     * @Date 17:44 2023/3/8
     * @return void
     **/
    public void initMenu() {
        JMenuBar menubar = new JMenuBar();

        JMenu menu1 = new JMenu("Operate");
        JMenu menu2 = new JMenu("Tools");

        menu1.add(runMenu);
        menu1.addSeparator();
        menu1.add(exitMenu);

        menu2.add(this.saveProperitiesMenu);

        runMenu.addActionListener(this);
        exitMenu.addActionListener(this);
        saveProperitiesMenu.addActionListener(this);

        menubar.add(menu1);
        menubar.add(menu2);

        setJMenuBar(menubar);
    }

    public void initCenterPanel() {
        JScrollPane jsp = new JScrollPane(this.taInfo);
        jsp.setBounds(100, 190, 400, 300);
        getContentPane().add(jsp, "Center");
    }

    public void initTopPanel() {
        JPanel toppanel = new JPanel();
        toppanel.setLayout(null);
        JLabel lSystemId = new JLabel("机器编码:", 4);
        JLabel lExpireTime = new JLabel("授权期限:", 4);
        JLabel lActivationCode = new JLabel("生成授权码:", 4);


        lSystemId.setBounds(10, 50, 85, 25);
        tfSystemId.setBounds(100, 50, 300, 30);

        lExpireTime.setBounds(10, 90, 85, 25);
        tfExpireTime.setBounds(100, 90, 300, 30);
        tfExpireTime.setText("2024-12-31");


        lActivationCode.setBounds(10, 130, 85, 25);
        tfActivationCode.setBounds(100, 130, 300, 30);
        jbActive.setBounds(410, 130, 120, 30);

        jbActive.addActionListener(this);

        toppanel.add(lSystemId);
        toppanel.add(tfSystemId);

        toppanel.add(lExpireTime);
        toppanel.add(tfExpireTime);


        toppanel.add(lActivationCode);
        toppanel.add(tfActivationCode);
        toppanel.add(jbActive);

        JPanel tmpPanel = new JPanel();
        tmpPanel.add(toppanel, "Center");

        toppanel.setPreferredSize(new Dimension(600, 240));
        getContentPane().add(tmpPanel, "North");
    }

    /**
     * ActionListener监听器处理方法,用于处理不同按钮触发的监听方法
     * @Author zxy
     * @Date 17:47 2023/3/8
     * @param paramActionEvent 
     * @return void 
     **/
    @Override
    public void actionPerformed(ActionEvent paramActionEvent) {
        Object source = paramActionEvent.getSource();

        if ((source == this.jbActive) || (source == this.runMenu)) {
            /**
             * 生成授权码
             **/
            try {
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                String activeCode = LicGen.genLicStr(tfExpireTime.getText().trim(), tfSystemId.getText().trim());
                tfActivationCode.setText(activeCode == null ? "--" : activeCode);
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                taInfo.append("授权码:" + activeCode + "\n");
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (source == saveProperitiesMenu) {
            /**
             *  生成授权文件
             **/
            try {
                String licPath = LicGen.genLic("C:\\license\\", tfExpireTime.getText().trim(), tfSystemId.getText().trim());
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                taInfo.append("授权文件路径:" + licPath + "\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (source == exitMenu) {
            /**
             * 退出功能
             **/
            System.exit(0);
        }
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        new ToolFrame();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值