Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

1  引言

1.1项目背景

我校积极举办组织学生参加各级各种科技、文艺、体育等校园文化活动,寓思想品德教育于生动活泼、形象具体的校园文化活动中。由于学校各级通讯不太方便,活动筹备过程中,无论是活动参与者的报名、观众的报名还是组织者的协调都是一件费时费力并且枯燥的任务。

从活动参与者的角度来说,每次参加活动都要填写报名表,交报名表的地方往往不尽相同:邮箱、短信什么的五花八门。这就很耽误大家的时间,甚至有可能错失名额和机会;另一方面,从活动组织方的角度来说,收集的报名表并且统计总结这个事本身就非常的繁琐且枯燥,再加之学业压力,就在某种程度上有些打压活动组织者和参与者的积极性。总的来说,缺乏高效的报名信息管理系统。尽管很多学生电脑拥有电脑,但并没有发挥出计算机高效性的特点,闲置资源比较突出,这就是报名信息管理系统的开发的基于环境。

1.2项目意义

校园文化活动是学校发展的灵魂,是凝聚人心、展实学校形象、提高学校闻名程度的重要体现途径。它对于一所学校良好的校风和雪峰的形成和延续,对学上的人生观、价值观产生着潜移默化的影响,这种影响和作用往往是任何课程无法比拟的。我校大力发展校园文化活动的大潮下,同学们的积极性大大提高,但是活动量的增多使得组织活动的同学在负担学业的同时倍感压力。本文通过实际和开发校园活动报名信息管理系统的实践,阐述了此类信息管理系统中应具有的功能、设计、实现。介绍了校园活动报名信息管理系统的设计和系统功能的实现过程。该系统可以运用于本校的报名活动信息管理,由发布活动、查看活动、报名活动、查看并导出活动报名情况等多个功能模块组成,实现了此类信息的计算机网络管理。

同时,校园活动报名管理系统通过设置管理员等级的方式分级分块管理,可以极大程度的保护个人隐私,目前来讲市场上专门实现此类功能的相关项目或是系统很少,几乎没有什么相似的项目可以作为比对的,市场前景广阔。

1.3系统主要任务

使用面向对象编程的思想和Java程序设计语言,设计和开发校园活动报名信息管理系统。该系统能够

(1)向数据库录入、修改和从数据库读出个人账号密码信息(MySQL数据库)

(2)向数据库录入、修改和从数据库读出个人的基本信息等(MySQL数据库)。

(3)向数据库录入、修改活动信息;能够对从数据库读出的活动信息进行甄别、筛选进行显示(活动号、活动名、发布人等等)。

(4)能够对每条活动进行具体的显示,并完成以下功能。

① 报名活动:当发现报名活动不符合要求,如时间不符、范围不属等等,报不了,其他情况,可以报名成功。

② 根据报名顺序显示报名情况。

③ 发布者可以自定义生成并导出所发布活动的报名人员的个人信息登记表。

2  系统方案设计

2.1需求分析

在本校中存在着许多的社团、协会、俱乐部等。学校通过相关的管理,通过学生团体资质的方法来达到让学生全方面发展的目的,而各个社团举办各种活动也相应地提高了学生的组织、领导、交流等多方面的能力。但是随着社团队伍的壮大,举办的活动日益增多,日常生活中也产生了许多麻烦,学生需要机械而枯燥地一遍又一遍地填写自己的个人信息以完善报名表,组织者也需要一条一条的统计报名信息,而且由于通信手段的不同,又是会出现漏报、错报等不公平现象,急需有效的方法来解决此类问题。

总体设计目标:使用者可以利用校园活动报名管理系统进行学校活动的报名操作,如发布活动、报名活动、生成并导出报名表等等。大大方便使用者对于活动报名的操作,不仅节约时间,而且提高准确性,可以达到事半功倍的效果。

总体设计策略:校园活动报名管理系统采用的是自顶向下、层次化的模块结构。对高层模块,采用以事务为中心的设计策略,把一个大的模块逐步分解成较小的相对简单的模块。对低层模块,采用以变换为中心的设计策略。为了提高每个模块的内聚性、降低模块间的耦合性,将每个模块设计成具有单一功能的模块(即功能内聚),使模块间的联系完全是通过主模块的控制和调用来实现(即非直接耦合)。

校园活动报名管理系统要求能够对所有个人信息及报名信息进行统一管理,按条件(学院、专业、班级)查询浏览可报名事务,能将报名情况统计表生成并导出,能将查询、浏览、修改的结果汇总到数据库中。

具体主要功能如下:

(1)注册账号

(2)登录

(3)修改密码

(4)修改个人信息

(5)管理员有权限发布各级活动

(6)查看并报名活动

(7)活动发布者可以导出报名者信息等级详情表

2.2系统流程图

进入程序首先进入登录模块,可以实现用户的注册和登录。登陆成功后进入主菜单界面,普通用户与管理员用户有略微的区别,但大致使用方法相同。主要功能有发布活动、查看活动与报名活动等。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)图2.1 主流程图 

2.3功能结构分析

(1)注册账号

进入登录面板后,注册按钮,进入注册面板,可以填写注册账号(学号)以及姓名,并在右边的树中选择自己的学院、专业和班级,此时立即注册,如果数据库中此账号未注册过或者是这五项基本信息仍然有空项,那么弹出错误框,提示用户检查输入。直到输入满足以上条件,提交成功后将数据写入数据库,并返回登录界面。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.2 注册账号流程图

(2)登录

登录面板输入账号密码,可以手动输入,也可以根据记住的账号密码快速输入,登录。与数据库内比对,若账号密码无误返回用户等级,并且若记住密码则将登陆账号添加至账号密码配置文件中,然后进入主界面。反之,如果输入账号密码有误,则不会进行上述步骤,弹出窗口提示用户重新输入账号密码。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.3 登录信息流程图

(3)修改密码

修改密码,弹出修改密码窗口。输入原密码和新密码,修改密码,进行若干合理性检验,验证合理后写入数据库。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.4 修改密码流程图

(4)修改个人信息

修改按钮,将保存按钮置为可选,修改按钮设为不可选,将可修改信息部分置为修改模式,修改个人信息,保存后向数据库提交申请,成功后将保存按钮置为不可选,修改按钮设为可选,将修改部分模式置为不可修改模式。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.5 修改个人信息流程图

(5)管理员有权限发布各级活动

如果是管理员,那么可以发布各级活动,具体发布范围与自身用户等级有关。发布活动按钮,填写、选择相关的活动属性,如果没有空项,可以选择立即发布活动,将会由系统自动生成发布时间、活动号等内容。向数据库提交申请成功后关闭窗口,返回主菜单。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.6 发布活动流程图

(6)查看并报名活动

登陆成功进入面板后,会自动调用一次刷新活动列表的函数(之后也可以手动刷新),将自己所属范围可报名活动全部列出,可以点开处于报名时间内的活动查看详情与按报名时间顺序排好的报名情况简表并且进行报名,如果已经报过名了将显示报名失败。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.7 活动报名流程图 

(7)活动发布者可以导出报名者信息等级详情表

活动的发布者还可以导出本活动按照报名顺序排好报名表,可以选择需要的信息,导出,弹出窗口,选择报名者需要的报名者信息以及信息的顺序自动去掉相同项和空项之后导出一个Excel文件,自动生成的报名表。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图2.8 导出活动的报名

3  系统软件开发环境

3.1 Eclipse

Eclipse是著名的跨平台的自由集成开发环境(IDE)。最初主要用来Java语言开发,但是目前亦有人通过插件使其作为其他计算机语言比如C++和Python的开发工具。Eclipse的本身只是一个框架平台,但是众多插件的支持使得Eclipse拥有其他功能相对固定的IDE软件很难具有的灵活性。许多软件开发商以Eclipse为框架开发自己的IDE。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图3.1 eclipse IDE 2018-12

3.2 Navicat Premium

Navicat Premium 是一套数据库开发工具,可以从单一应用程序中同时连接 MySQL、MariaDB、MongoDB、SQL Server、Oracle、PostgreSQL 和 SQLite 数据库。它与 Amazon RDS、Amazon Aurora、Amazon Redshift、Microsoft Azure、Oracle Cloud、MongoDB Atlas、阿里云、腾讯云和华为云等云数据库兼容。可以快速轻松地创建、管理和维护数据库。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)图3.2 Navicat Premium 12.0.18

3.3 MySQL

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。

MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型和大型网站的开发都选择 MySQL 作为网站数据库。

MYSQL优势主要有:(1)运行速度快;(2)可移植性好,能够工作在众多不同的系统平台上。(3)提供了丰富的接口,如C++、Java、Php等等;(4)支持查询语言,MYSQL可以利用标准SQL语法和支持ODBC(开放式数据库连接)的应用程序;(5)网格控制,以及数据仓库方面也非常强大;(6)安全性和连接性好:十分灵活和安全的权限和密码系统,允许主机验证。连接到服务器时,所有的密码均采用加密方式,从而保证密码安全。并且由于MySQL是网络化的,因此可以在因特网网上的任何地方访问,提高数据共享效率。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)图3.3 MySQL

4  数据库设计

4.1主要数据结构

(1)账户表(tb_account)

4.1 账户表

名称

类型

长度

默认值

注释

admin_name

varchar

20

nul

主键

账号

admin_pwd

varchar

50

000

密码

admin_level

smallint

6

0

用户等级

(2)人物信息表(tb_person)

4.2 人物信息表

名称

类型

长度

默认值

注释

admin_name

varchar

20

nul

主键

账号

admin realname

varchar

20

nul

姓名

admin_academy

varchar

20

nul

学院

admin_major

varchar

20

nul

专业

admin_class

varchar

20

nul

班级

admin_TEL

varchar

20

nul

电话

admin_

varchar

20

nul

Qq

admin_WeChat

varchar

20

nul

微信

admin_IDnum

varchar

20

nul

身份证号

admin_politice

varchar

20

nul

政治面貌

admin_address

varchar

20

nul

住址

admin_hometown

varchar

20

nul

籍贯

admin_dormitory

varchar

20

nul

寝室号

other1

varchar

20

nul

其他1

other2

varchar

20

nul

其他2

(3)活动表(tb_activity)

4.3 活动表

名称

类型

长度

默认值

注释

activity_num

varchar

20

null

主键

活动号

activity_name

varchar

20

null

活动名

activity_range

varchar

20

null

发布范围

admin_name

varchar

20

null

发布者账号

activity_begintime

varchar

20

null

报名开始时间

activity_endtime

varchar

20

null

结束时间

activity_content

varchar

200

null

活动详细内容

(4)活动报名表(tb_enroll)

4.4 活动报名表

名称

类型

长度

默认值

注释

activity_num

varchar

20

nul

主键

活动号

admin_name

varchar

20

nul

账号

5  系统功能设计与实现

5.1 登录功能

5.1.1 登录界面

用户在进行系统登录时,需要通过该界面输入帐号、密码,然后“登录”按钮进行登录。注册界面设计如图5.1所示

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)图5.1 登陆界面设计 

5.1.2 该模块涉及到的文件和类

 通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.2所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.2 模块文件的组织

(1)实体类

该类主要用于封装用户的信息:帐号、密码和用户等级等信息,类图如图5.3所示。

Account

-admin_name:String

-admin_pwd:String

-admin_level:int

提供了每个成员变量的getter和setter方法,还有全参构造方法、三个参数(admin_name,admin_pwd,admin_level)的构造方法,并重写了toString方法

5.3 Account类图

核心代码如下:

package com.wd.registration.entity;

/

 * 账户实体类

*/

public class Account {

// 账号、密码

private String admin_name;

private String admin_pwd;

// 等级 0:普通用户 1-4 班系院校四级管理员

private int admin_level;

public Account() {}

public Account(String admin_name, String admin_pwd, int admin_level) {

super();

this.admin_name = admin_name;

this.admin_pwd = admin_pwd;

this.admin_level = admin_level;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getAdmin_pwd() {

return admin_pwd;

}

public void setAdmin_pwd(String admin_pwd) {

this.admin_pwd = admin_pwd;

}

public int getAdmin_level() {

return admin_level;

}

public void setAdmin_level(int admin_level) {

this.admin_level = admin_level;

}

@Override

public String toString() {

return “Account [admin_name=” + admin_name + “, admin_pwd=” + admin_pwd + “, admin_level=” + admin_level + “]”;

}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的AccountDao,主要用来完成对表tb_account的增删改查操作。登录主要用到里面的查询方法。核心代码如下:

/

 *  关于登录

 *  @param name 账号

 *  @param pwd 密码

 *  @return 返回账户信息,不正确返回null

 */

public Account login(String name, String pwd) {

Account acc = null;

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 创建语句执行器

Statement stat = conn.createStatement();

// 执行并返回结果

String sql = “select * from tb_account where admin_name='” + name + “‘ and admin_pwd='” + pwd + “‘”;

ResultSet rs = stat.executeQuery(sql);

// 迭代

if(rs.next()) {

String admin_name = rs.getString(“admin_name”);

String admin_pwd = rs.getString(“admin_pwd”);

int admin_level = rs.getInt(“admin_level”);

// 组装成对象

acc = new Account(admin_name, admin_pwd, admin_level);

}

// 关闭

rs.close();

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return acc;

}

(3)界面层

定义LoginFrame用于实现登陆界面,主要有登录按钮、注册按钮等。主要实现代码:new LoginFrame().setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图5.4 界面组件图

实现代码:

package com.wd.registration.view;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import javax.swing.DefaultComboBoxModel;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JRadioButton;

import javax.swing.JTextField;

import javax.swing.border.EmptyBorder;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

import com.wd.registration.dao.AccountDao;

import com.wd.registration.entity.Account;

import com.wd.registration.utils.ConfigUtils;

public class LoginFrame extends JFrame implements ActionListener { // 实现监听接口

private JPanel contentPane;

private static JPasswordField txtPwd;

private JTextField txtAdminName;

private JButton btnLogin;

private JButton btnRegister;

private JButton btnNewButton_1;

private JRadioButton rdbtnNewRadioButton;

private static HashMap<String,String> cutils = ConfigUtils.getUserMap();

/

 * Create the frame.

 */

public LoginFrame() {

setTitle(“校园活动报名系统V2.0”);

setBounds(100, 100, 600, 610);

// 不允许改变大小

setResizable(false);

// 默认居中

setLocationRelativeTo(null);

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel();

lblNewLabel.setIcon(new ImageIcon(LoginFrame.class.getResource(“/image/header.jpg”)));

lblNewLabel.setBounds(42, 13, 500, 315);

contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel(“账号”);

lblNewLabel_1.setIcon(new ImageIcon(LoginFrame.class.getResource(“/image/user.png”)));

lblNewLabel_1.setFont(new Font(“华文仿宋”, Font.BOLD, 24));

lblNewLabel_1.setBounds(52, 341, 72, 34);

contentPane.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel(“密码”);

lblNewLabel_2.setIcon(new ImageIcon(LoginFrame.class.getResource(“/image/pwd.png”)));

lblNewLabel_2.setFont(new Font(“华文仿宋”, Font.BOLD, 24));

lblNewLabel_2.setBounds(50, 388, 72, 34);

contentPane.add(lblNewLabel_2);

List<String> UID = new ArrayList<String>();

        for(String key : cutils.keySet()) {

         UID.add(key);

}

txtAdminName = new JTextField();

setupAutoComplete(txtAdminName, UID);

txtAdminName.setBounds(138, 341, 404, 34);

contentPane.add(txtAdminName);

txtAdminName.setColumns(10);

txtPwd = new JPasswordField();

txtPwd.setBounds(138, 391, 304, 34);

contentPane.add(txtPwd);

btnLogin = new JButton(“登录”);

btnLogin.setIcon(new ImageIcon(LoginFrame.class.getResource(“/image/log_in.png”)));

// 给登录按钮添加监听器

btnLogin.addActionListener(this);

btnLogin.setFont(new Font(“楷体”, Font.BOLD, 18));

btnLogin.setBounds(110, 450, 142, 40);

contentPane.add(btnLogin);

btnRegister = new JButton(“注册”);

btnRegister.setIcon(new ImageIcon(LoginFrame.class.getResource(“/image/log_in.png”)));

btnRegister.addActionListener(this);

btnRegister.setFont(new Font(“楷体”, Font.BOLD, 18));

btnRegister.setBounds(340, 450, 142, 40);

contentPane.add(btnRegister);

rdbtnNewRadioButton = new JRadioButton(“记住密码”);

rdbtnNewRadioButton.setFont(new Font(“仿宋”, Font.BOLD, 12));

rdbtnNewRadioButton.setBounds(452, 394, 90, 30);

contentPane.add(rdbtnNewRadioButton);

btnNewButton_1 = new JButton(“关于”);

btnNewButton_1.addActionListener(this);

btnNewButton_1.setBounds(470, 523, 72, 27);

contentPane.add(btnNewButton_1);

// 添加键盘监听

this.getRootPane().setDefaultButton(btnLogin);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “退出系统?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

        // 退出系统

         LoginFrame.this.dispose();

        }

    }

});

}

public void actionPerformed(ActionEvent e) {

// 如果按下的是登录

if(e.getSource()==btnLogin) {

login();

}

// 如果按下的是注册

else if (e.getSource()==btnRegister) {

register();

}

// 如果按下的是关于

else if (e.getSource()==btnNewButton_1) {

JOptionPane.showMessageDialog(this, “大数据1901出品”);

}

else {

JOptionPane.showMessageDialog(this, “未知的操作”, “错误”, JOptionPane.ERROR_MESSAGE);

}

}

private void register() {

try {

new RegisterFrame().setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

private void login() {

// 取得账号和密码

String admin_name = txtAdminName.getText();

String admin_pwd = txtPwd.getText();

// 调用DAO层login比对

AccountDao dao = new AccountDao();

Account account = dao.login(admin_name, admin_pwd);

// 根据对象是否为空来做下一步操作

if(account==null) {

JOptionPane.showMessageDialog(this, “账号或密码错误”, “错误”, JOptionPane.ERROR_MESSAGE);

}

else {

//如果有记住密码

if(rdbtnNewRadioButton.isSelected()) {

// 将新加账号密码加入HashMap

cutils.put(account.getAdmin_name(), account.getAdmin_pwd());

ConfigUtils.saveMapToFile(cutils);

}

this.dispose();

try {

// 生成主窗口,将用户名和用户等级传入

MainFrame frame = new MainFrame(account.getAdmin_name(), account.getAdmin_level());

frame.setVisible(true);

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

private static boolean isAdjusting(JComboBox cbInput) {

        if (cbInput.getClientProperty(“is_adjusting”) instanceof Boolean) { //返回具有指定键的属性的值。只有添加的属性将返回不空(non-null)的值

         return (Boolean) cbInput.getClientProperty(“is_adjusting”);

        }

        return false;

    }

private static void setAdjusting(JComboBox cbInput, boolean adjusting) {

        cbInput.putClientProperty(“is_adjusting”, adjusting); //对由另一个模块创建的组件进行注释

        //向此组件添加任意键/值”客户端属性”。

    }

public static void setupAutoComplete(JTextField txtInput, List<String> items) {

        DefaultComboBoxModel model = new DefaultComboBoxModel();

        JComboBox cbInput = new JComboBox(model) { //model与宽度设置(长度与文本框相当)

            @ Override

         public Dimension getPreferredSize() {

                return new Dimension(super.getPreferredSize().width, 0);

            }

        };

        

        setAdjusting(cbInput, false); //先默认全部都没有被adjust

        for (String item : items) { //把列表加到model中

            model.addElement(item);

        }

        

        cbInput.setSelectedItem(null); //将组合框显示区域中的选定项设置为参数中的对象,一开始没有

        

        //Combobox的监听

        cbInput.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                if (!isAdjusting(cbInput)) { //调整完了 已经补全的话进入

                    if (cbInput.getSelectedItem() != null) {

                     txtPwd.setText(cutils.get(cbInput.getSelectedItem().toString()));

                        txtInput.setText(cbInput.getSelectedItem().toString()); //把他显示在txtInput上

                    }

                }

            }

        });

        //监听JtextArea增添修改

        txtInput.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {

                updateList();

            }

            public void removeUpdate(DocumentEvent e) {

                updateList();

            }

            public void changedUpdate(DocumentEvent e) {

                updateList();

            }

            private void updateList() {

                setAdjusting(cbInput, true);

                model.removeAllElements();

                String input = txtInput.getText();

                if (!input.isEmpty()) {

                    for (String item : items) {

                        if (item.toLowerCase().startsWith(input.toLowerCase())) {

                            model.addElement(item);

                        }

                    }

                }

                cbInput.setPopupVisible(model.getSize() > 0);

                setAdjusting(cbInput, false);

            }

        });

        txtInput.setLayout(new BorderLayout());

        txtInput.add(cbInput, BorderLayout.SOUTH);

    }

}

5.2 注册功能

5.2.1 注册界面

用户在进行注册时,需要通过该界面输入学号/工号、姓名、学院、专业、班级,然后“立即注册”按钮进行注册,注册界面效果如图5.5所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.5 注册界面效果

5.2.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.6所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图5.6 模块文件的组织

(1)实体类

该类主要用于封装用户的信息:帐号、密码和用户等级等信息,类图如图5.7所示。

Account

-admin_name:String

-admin_pwd:String

-admin_level:int

提供了每个成员变量的getter和setter方法,还有全参构造方法、三个参数(admin_name,admin_pwd,admin_level)的构造方法,并重写了toString方法

5.7 Account类图

核心代码如下:

package com.wd.registration.entity;

/

 * 账户实体类

*/

public class Account {

// 账号、密码

private String admin_name;

private String admin_pwd;

// 等级 0:普通用户 1-4 班系院校四级管理员

private int admin_level;

public Account() {}

public Account(String admin_name, String admin_pwd, int admin_level) {

super();

this.admin_name = admin_name;

this.admin_pwd = admin_pwd;

this.admin_level = admin_level;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getAdmin_pwd() {

return admin_pwd;

}

public void setAdmin_pwd(String admin_pwd) {

this.admin_pwd = admin_pwd;

}

public int getAdmin_level() {

return admin_level;

}

public void setAdmin_level(int admin_level) {

this.admin_level = admin_level;

}

@Override

public String toString() {

return “Account [admin_name=” + admin_name + “, admin_pwd=” + admin_pwd + “, admin_level=” + admin_level + “]”;

}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的AccountDao,主要用来完成对表tb_account的增删改查操作。登录主要用到里面的查询方法。核心代码如下:

/

 *  插入账户信息

 *  @param name 账号

 */

public void insert(String name) {

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 编写SQL语句

String sql = “insert into tb_account(admin_name, admin_pwd, admin_level) value(?, “000”, 0)”;

// 创建预编译语句执行器

PreparedStatement stat = conn.prepareStatement(sql);

stat.setString(1, name);

// 执行

stat.executeUpdate();

// 关闭

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

(3)界面层

定义RegisterFrame用于实现注册界面,右边有一颗从滚动窗口里放的从文件里读出的一棵树,当选中叶子节点(班级)时会自动把路径上的学院、专业等填入框内,保证数据库内同一个信息的表达一致性。同时,还具有“空项提示”的功能,如果某一项信息为空,则在之下会显示一条提醒信息。

生成窗口代码为:new RegisterFrame().setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

5.8 界面组件图

实现代码:

package com.wd.registration.view;

import java.awt.Color;

import java.awt.Font;

import java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

import javax.swing.JTree;

import javax.swing.SwingConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeSelectionModel;

import com.wd.registration.dao.AccountDao;

import com.wd.registration.dao.PersonDao;

import com.wd.registration.entity.Person;

import com.wd.registration.utils.XMLUtils;

public class RegisterFrame extends JDialog implements TreeSelectionListener, ActionListener {

private JPanel contentPane;

private JTextField textField;

private JTextField textField_1;

private JTextField textField_2;

private JTextField textField_3;

private JTree jtree;

private JTextField textField_4;

private JLabel lblNewLabel_2_0;

private JLabel lblNewLabel_2_1;

private JLabel lblNewLabel_2_2;

private JLabel lblNewLabel_2_3;

private JLabel lblNewLabel_2_4;

private Person pr;

/

 * Create the frame.

 */

public RegisterFrame() {

setTitle(“用户注册”);

setBounds(100, 100, 590, 435);

setResizable(false);

setLocationRelativeTo(null);

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

// 创建一棵树

XMLUtils tree = new XMLUtils();

jtree = new JTree(tree.getRootTree());

jtree.addTreeSelectionListener(this);

//设置Tree的选择模式为一次只能选择一个节点

        jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

// 创建一个滚动面板用来装树

JScrollPane treeView = new JScrollPane(jtree);

treeView.setBounds(370, 13, 188, 362);

contentPane.add(treeView);

JPanel Infor_panel = new JPanel();

Infor_panel.setBounds(14, 13, 342, 310);

contentPane.add(Infor_panel);

Infor_panel.setLayout(null);

JLabel lblNewLabel = new JLabel(“学号/工号”);

lblNewLabel.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/user.png”)));

lblNewLabel.setFont(new Font(“华文新魏”, Font.PLAIN, 18));

lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel.setBounds(0, 36, 105, 18);

Infor_panel.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel(“姓名”);

lblNewLabel_1.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/realname.png”)));

lblNewLabel_1.setFont(new Font(“华文新魏”, Font.PLAIN, 18));

lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_1.setBounds(13, 90, 91, 18);

Infor_panel.add(lblNewLabel_1);

textField = new JTextField();

textField.setBounds(112, 31, 205, 24);

Infor_panel.add(textField);

textField.setColumns(10);

textField_1 = new JTextField();

textField_1.setBounds(112, 86, 205, 24);

Infor_panel.add(textField_1);

textField_1.setColumns(10);

JLabel lblNewLabel_1_1 = new JLabel(“学院”);

lblNewLabel_1_1.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/academy.png”)));

lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_1_1.setFont(new Font(“华文新魏”, Font.PLAIN, 18));

lblNewLabel_1_1.setBounds(12, 144, 91, 18);

Infor_panel.add(lblNewLabel_1_1);

JLabel lblNewLabel_1_2 = new JLabel(“专业”);

lblNewLabel_1_2.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/major.png”)));

lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_1_2.setFont(new Font(“华文新魏”, Font.PLAIN, 18));

lblNewLabel_1_2.setBounds(11, 198, 91, 18);

Infor_panel.add(lblNewLabel_1_2);

JLabel lblNewLabel_1_3 = new JLabel(“班级”);

lblNewLabel_1_3.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/class.png”)));

lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_1_3.setFont(new Font(“华文新魏”, Font.PLAIN, 18));

lblNewLabel_1_3.setBounds(10, 252, 91, 18);

Infor_panel.add(lblNewLabel_1_3);

textField_2 = new JTextField();

textField_2.setEditable(false);

textField_2.setColumns(10);

textField_2.setBounds(112, 141, 205, 24);

Infor_panel.add(textField_2);

textField_3 = new JTextField();

textField_3.setEditable(false);

textField_3.setColumns(10);

textField_3.setBounds(112, 196, 205, 24);

Infor_panel.add(textField_3);

textField_4 = new JTextField();

textField_4.setEditable(false);

textField_4.setColumns(10);

textField_4.setBounds(112, 251, 205, 24);

Infor_panel.add(textField_4);

lblNewLabel_2_0 = new JLabel();

lblNewLabel_2_0.setForeground(Color.RED);

lblNewLabel_2_0.setBounds(112, 55, 125, 18);

Infor_panel.add(lblNewLabel_2_0);

lblNewLabel_2_1 = new JLabel();

lblNewLabel_2_1.setForeground(Color.RED);

lblNewLabel_2_1.setBounds(112, 110, 125, 18);

Infor_panel.add(lblNewLabel_2_1);

lblNewLabel_2_2 = new JLabel();

lblNewLabel_2_2.setForeground(Color.RED);

lblNewLabel_2_2.setBounds(112, 165, 125, 18);

Infor_panel.add(lblNewLabel_2_2);

lblNewLabel_2_3 = new JLabel();

lblNewLabel_2_3.setForeground(Color.RED);

lblNewLabel_2_3.setBounds(112, 220, 125, 18);

Infor_panel.add(lblNewLabel_2_3);

lblNewLabel_2_4 = new JLabel();

lblNewLabel_2_4.setForeground(Color.RED);

lblNewLabel_2_4.setBounds(112, 275, 125, 18);

Infor_panel.add(lblNewLabel_2_4);

JButton btnRegisterNow = new JButton(“立即注册”);

btnRegisterNow.addActionListener(this);

btnRegisterNow.setIcon(new ImageIcon(RegisterFrame.class.getResource(“/image/log_in.png”)));

btnRegisterNow.setBounds(14, 336, 342, 39);

contentPane.add(btnRegisterNow);

// 模态框

this.setModal(true);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “退出注册界面?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

        // 退出

         RegisterFrame.this.dispose();

        }

    }

});

}

// 在JTree上点选任何一个节点,都会触发TreeSelectionEvent事件

@Override

public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode)jtree.getLastSelectedPathComponent();

// 防止啥也不选

if(node == null) {

return;

}

// 如果是树叶结点,把信息显示出来

if(node.isLeaf()){

textField_2.setText(node.getPath()[1].toString());

textField_3.setText(node.getPath()[2].toString());

textField_4.setText(node.getPath()[3].toString());

}

}

@Override

public void actionPerformed(ActionEvent e) {

// 实例化Person实体类

pr = new Person();

pr.setAdmin_name(textField.getText());

pr.setAdmin_realname(textField_1.getText());

pr.setAdmin_academy(textField_2.getText());

pr.setAdmin_major(textField_3.getText());

pr.setAdmin_class(textField_4.getText());

// 可执行标志true可以写入false未填完全

boolean notEmpty = true;

boolean[] temp = new PersonDao().canRegister(pr);

// 遍历数组,显示标签

for(int i = 0; i < temp.length; i++) {

if(temp[i] == false) {

notEmpty = false;

showLbls(i, “此项不可为空”);

} else {

showLbls(i, “”);

}

}

// 全部填满,可以继续

if (notEmpty) {

toDB(pr);

}

}

private void showLbls(int i, String content) {

switch(i) { // 显示label提示输入

case 0: lblNewLabel_2_0.setText(content); break;

case 1: lblNewLabel_2_1.setText(content); break;

case 2: lblNewLabel_2_2.setText(content); break;

case 3: lblNewLabel_2_3.setText(content); break;

case 4: lblNewLabel_2_4.setText(content); break;

}

}

// 写入数据库

private void toDB(Person pr) throws HeadlessException {

// 检测是否添加过数据库

if(new PersonDao().getInformation(pr.getAdmin_name()) != null) {

JOptionPane.showMessageDialog(this, “此账号已经注册过了,如有疑问请联系开发人员”, “错误”, JOptionPane.ERROR_MESSAGE);

}

else {

// 插入Person数据库

PersonDao newPrDao = new PersonDao();

newPrDao.insert(pr);

// 插入Account数据库

AccountDao newAccDao = new AccountDao();

newAccDao.insert(pr.getAdmin_name());

// 显示注册成功

JOptionPane.showMessageDialog(this, “账户注册成功,账号为学(工)号,初始密码为000,欢迎使用本系统”);

this.dispose();

}

}

}

5.3 主界面功能

5.3.1 注册界面

用户在登录成功时,进入系统主页面,主界面包括个人信息修改、活动发布、活动列表,主界面效果如图5.9所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)图5.9 界面效果

5.3.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.10所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图5.10 模块文件的组织

(1)界面层

定义MainFrame用于实现生成主界面与引导,包含个人信息面板部分(内含个人信息修改以及密码修改功能)、发布活动功能(用户等级大于0才能选择)和活动展实板块(右),也是程序的核心界面,是完成系统几乎所有功能的上级窗口。生成主窗口,需要将用户名和用户等级传入。

生成窗口代码为:MainFrame frame = new MainFrame(account.getAdmin_name(), account.getAdmin_level());frame.setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.11 主界面组件

核心代码:

package com.wd.registration.view;

import java.awt.Color;

import java.awt.Font;

import java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Collections;

import java.util.Date;

import java.util.List;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.ListSelectionModel;

import javax.swing.border.EmptyBorder;

import javax.swing.border.LineBorder;

import javax.swing.border.TitledBorder;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import javax.swing.table.DefaultTableModel;

import com.wd.registration.dao.ActivityDao;

import com.wd.registration.dao.PersonDao;

import com.wd.registration.dao.PostDao;

import com.wd.registration.entity.Activity;

import com.wd.registration.entity.Person;

/

 * 显示主界面

*/

public class MainFrame extends JFrame implements ActionListener {

private JPanel contentPane;

private JTextField textField;

private JTextField textField_1;

private JTextField textField_2;

private JTextField textField_3;

private JTextField textField_4;

private JTextField textField_5;

private JTextField textField_6;

private JTextField textField_7;

private JTextField textField_8;

private JTextField textField_9;

private JTextField textField_10;

private JTextField textField_11;

private JTextField textField_12;

private JTextField textField_13;

private JTextField textField_14;

private JButton btnNewButton_2;

private JTable table;

private JButton btnStartChange;

private JButton btnRefresh;

private JLabel lblNewLabel_15;

private JLabel lblNewLabel_16;

private JLabel lblNewLabel_17;

private JLabel lblNewLabel_18;

private JButton btnStopChange;

private PersonDao pdao;

private Person pr;

private JButton btnpubButton;

private JLabel lblNewLabel_19;

private JLabel lblNewLabel_20;

private JLabel lblNewLabel_21;

private JPanel panel_refreshtb;

private DefaultTableModel dtm; // 表单

private int level;

/

 * Create the frame.

 */

public MainFrame(String admin_name, int admin_level) {

// 获取个人信息

pdao = new PersonDao();

pr = pdao.getInformation(admin_name);

level = admin_level;

// 设置窗口

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle(“校园活动报名系统客户端V2.0”);

setBounds(300, 300, 1350, 900);

// 不允许改变大小

setResizable(false);

// 默认居中

setLocationRelativeTo(null);

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JPanel panel_account = new JPanel();

panel_account.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2, true), “个人信息部分”, TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));

panel_account.setBounds(40, 30, 350, 545);

contentPane.add(panel_account);

panel_account.setLayout(null);

JLabel lblNewLabel = new JLabel(“学号”);

lblNewLabel.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/user.png”)));

lblNewLabel.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel.setBounds(30, 50, 57, 18);

panel_account.add(lblNewLabel);

textField = new JTextField();

textField.setText(pr.getAdmin_name());

textField.setBounds(94, 45, 152, 24);

textField.setEditable(false);

panel_account.add(textField);

textField.setColumns(10);

btnNewButton_2 = new JButton(“修改密码”);

btnNewButton_2.setFont(new Font(“宋体”, Font.BOLD, 12));

btnNewButton_2.setBounds(258, 45, 78, 27);

btnNewButton_2.addActionListener(this);

panel_account.add(btnNewButton_2);

JLabel lblNewLabel_1 = new JLabel(“姓名”);

lblNewLabel_1.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/realname.png”)));

lblNewLabel_1.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_1.setBounds(30, 81, 57, 18);

panel_account.add(lblNewLabel_1);

textField_1 = new JTextField();

textField_1.setText(pr.getAdmin_realname());

textField_1.setBounds(94, 76, 152, 24);

textField_1.setEditable(false);

panel_account.add(textField_1);

textField_1.setColumns(10);

JLabel lblNewLabel_2 = new JLabel(“学院”);

lblNewLabel_2.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/academy.png”)));

lblNewLabel_2.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_2.setBounds(25, 110, 57, 18);

panel_account.add(lblNewLabel_2);

textField_2 = new JTextField();

textField_2.setText(pr.getAdmin_academy());

textField_2.setBounds(94, 105, 152, 24);

textField_2.setEditable(false);

panel_account.add(textField_2);

textField_2.setColumns(10);

JLabel lblNewLabel_3 = new JLabel(“专业”);

lblNewLabel_3.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/major.png”)));

lblNewLabel_3.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_3.setBounds(23, 139, 61, 18);

panel_account.add(lblNewLabel_3);

textField_3 = new JTextField();

textField_3.setText(pr.getAdmin_major());

textField_3.setBounds(94, 134, 152, 24);

textField_3.setEditable(false);

panel_account.add(textField_3);

textField_3.setColumns(10);

JLabel lblNewLabel_4 = new JLabel(“班级”);

lblNewLabel_4.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/class.png”)));

lblNewLabel_4.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_4.setBounds(19, 168, 61, 18);

panel_account.add(lblNewLabel_4);

textField_4 = new JTextField();

textField_4.setText(pr.getAdmin_class());

textField_4.setBounds(94, 163, 152, 24);

textField_4.setEditable(false);

panel_account.add(textField_4);

textField_4.setColumns(10);

JLabel lblNewLabel_5 = new JLabel(“电话”);

lblNewLabel_5.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/telephone.png”)));

lblNewLabel_5.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_5.setBounds(30, 197, 57, 18);

panel_account.add(lblNewLabel_5);

textField_5 = new JTextField();

textField_5.setText(pr.getAdmin_TEL());

textField_5.setBounds(94, 192, 152, 24);

textField_5.setEditable(false);

panel_account.add(textField_5);

textField_5.setColumns(10);

JLabel lblNewLabel_6 = new JLabel(“”);

lblNewLabel_6.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/.png”)));

lblNewLabel_6.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_6.setBounds(30, 226, 53, 18);

panel_account.add(lblNewLabel_6);

textField_6 = new JTextField();

textField_6.setText(pr.getAdmin_());

textField_6.setBounds(94, 221, 152, 24);

textField_6.setEditable(false);

panel_account.add(textField_6);

textField_6.setColumns(10);

JLabel lblNewLabel_7 = new JLabel(“微信”);

lblNewLabel_7.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/Wechat.png”)));

lblNewLabel_7.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_7.setBounds(26, 255, 57, 18);

panel_account.add(lblNewLabel_7);

textField_7 = new JTextField();

textField_7.setText(pr.getAdmin_WeChat());

textField_7.setBounds(94, 250, 152, 24);

textField_7.setEditable(false);

panel_account.add(textField_7);

textField_7.setColumns(10);

JLabel lblNewLabel_8 = new JLabel(“身份证”);

lblNewLabel_8.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/id_card.png”)));

lblNewLabel_8.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_8.setBounds(15, 284, 77, 18);

panel_account.add(lblNewLabel_8);

textField_8 = new JTextField();

textField_8.setText(pr.getAdmin_IDnum());

textField_8.setBounds(94, 279, 152, 24);

textField_8.setEditable(false);

panel_account.add(textField_8);

textField_8.setColumns(10);

JLabel lblNewLabel_9 = new JLabel(“政治面貌”);

lblNewLabel_9.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/Flag_red.png”)));

lblNewLabel_9.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_9.setBounds(10, 313, 80, 18);

panel_account.add(lblNewLabel_9);

textField_9 = new JTextField();

textField_9.setText(pr.getAdmin_politice());

textField_9.setBounds(94, 308, 152, 24);

textField_9.setEditable(false);

panel_account.add(textField_9);

textField_9.setColumns(10);

JLabel lblNewLabel_10 = new JLabel(“家庭住址”);

lblNewLabel_10.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/map.png”)));

lblNewLabel_10.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_10.setBounds(8, 342, 88, 18);

panel_account.add(lblNewLabel_10);

textField_10 = new JTextField();

textField_10.setText(pr.getAdmin_address());

textField_10.setBounds(94, 337, 152, 24);

textField_10.setEditable(false);

panel_account.add(textField_10);

textField_10.setColumns(10);

JLabel lblNewLabel_11 = new JLabel(“籍贯”);

lblNewLabel_11.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/map2.png”)));

lblNewLabel_11.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_11.setBounds(25, 371, 61, 18);

panel_account.add(lblNewLabel_11);

textField_11 = new JTextField();

textField_11.setText(pr.getAdmin_hometown());

textField_11.setBounds(94, 366, 152, 24);

textField_11.setEditable(false);

panel_account.add(textField_11);

textField_11.setColumns(10);

JLabel lblNewLabel_12 = new JLabel(“寝室号”);

lblNewLabel_12.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/sleep.png”)));

lblNewLabel_12.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_12.setBounds(19, 400, 70, 18);

panel_account.add(lblNewLabel_12);

textField_12 = new JTextField();

textField_12.setText(pr.getAdmin_dormitory());

textField_12.setBounds(94, 395, 152, 24);

textField_12.setEditable(false);

panel_account.add(textField_12);

textField_12.setColumns(10);

JLabel lblNewLabel_13 = new JLabel(“其他1”);

lblNewLabel_13.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/Misc_Designs.png”)));

lblNewLabel_13.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_13.setBounds(26, 429, 61, 18);

panel_account.add(lblNewLabel_13);

textField_13 = new JTextField();

textField_13.setText(pr.getOther1());

textField_13.setBounds(94, 424, 152, 24);

textField_13.setEditable(false);

panel_account.add(textField_13);

textField_13.setColumns(10);

JLabel lblNewLabel_14 = new JLabel(“其他2”);

lblNewLabel_14.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/Misc_Designs.png”)));

lblNewLabel_14.setFont(new Font(“华文琥珀”, Font.PLAIN, 15));

lblNewLabel_14.setBounds(26, 458, 61, 18);

panel_account.add(lblNewLabel_14);

textField_14 = new JTextField();

textField_14.setText(pr.getOther2());

textField_14.setBounds(94, 453, 152, 24);

textField_14.setEditable(false);

panel_account.add(textField_14);

textField_14.setColumns(10);

btnStartChange = new JButton(“修改”);

btnStartChange.setFont(new Font(“宋体”, Font.BOLD, 12));

btnStartChange.setBounds(46, 489, 105, 38);

btnStartChange.addActionListener(this);

panel_account.add(btnStartChange);

lblNewLabel_15 = new JLabel(“例:湖南浏阳”);

lblNewLabel_15.setFont(new Font(“宋体”, Font.BOLD, 12));

lblNewLabel_15.setBounds(252, 371, 84, 18);

panel_account.add(lblNewLabel_15);

lblNewLabel_16 = new JLabel(“例:行四A109”);

lblNewLabel_16.setFont(new Font(“宋体”, Font.BOLD, 12));

lblNewLabel_16.setBounds(252, 400, 84, 18);

panel_account.add(lblNewLabel_16);

lblNewLabel_18 = new JLabel(“<-以下可修改”);

lblNewLabel_18.setFont(new Font(“宋体”, Font.BOLD, 12));

lblNewLabel_18.setBounds(252, 178, 84, 18);

panel_account.add(lblNewLabel_18);

btnStopChange = new JButton(“修改完成”);

btnStopChange.setEnabled(false);

btnStopChange.setFont(new Font(“宋体”, Font.BOLD, 12));

btnStopChange.setBounds(197, 490, 105, 37);

btnStopChange.addActionListener(this);

panel_account.add(btnStopChange);

JPanel panel_announcement = new JPanel();

panel_announcement.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2, true), “活动发布板块”, TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));

panel_announcement.setBounds(40, 588, 350, 252);

contentPane.add(panel_announcement);

panel_announcement.setLayout(null);

btnpubButton = new JButton(“发布活动”);

// 如果具有管理员权限,开放btnpubButton按钮

if(admin_level == 0) {

btnpubButton.setEnabled(false);

}

btnpubButton.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/plus.png”)));

btnpubButton.setFont(new Font(“华文彩云”, Font.BOLD, 30));

btnpubButton.addActionListener(this);

btnpubButton.setBounds(14, 132, 322, 107);

panel_announcement.add(btnpubButton);

lblNewLabel_19 = new JLabel(“获取发布活动权限请联系项目开发人员”);

lblNewLabel_19.setFont(new Font(“幼圆”, Font.PLAIN, 18));

lblNewLabel_19.setBounds(14, 35, 322, 32);

panel_announcement.add(lblNewLabel_19);

lblNewLabel_20 = new JLabel(“电话:”);

lblNewLabel_20.setFont(new Font(“幼圆”, Font.PLAIN, 16));

lblNewLabel_20.setBounds(14, 65, 160, 32);

panel_announcement.add(lblNewLabel_20);

lblNewLabel_21 = new JLabel(“:”);

lblNewLabel_21.setFont(new Font(“幼圆”, Font.PLAIN, 16));

lblNewLabel_21.setBounds(30, 90, 135, 32);

panel_announcement.add(lblNewLabel_21);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(405, 30, 834, 810);

contentPane.add(scrollPane);

// 建立表格

String[] header = {“活动编号”, “活动名称”, “活动发布者”, “报名开始时间”, “报名截止时间”};

dtm = new DefaultTableModel(null, header) {

// 禁止编辑表格

    @Override

    public boolean isCellEditable(int row, int column) {

        return false;

    }

};

refreshtb(); // 初始化表格

table = new JTable(dtm);

scrollPane.setViewportView(table);

// 设置表格内容颜色

        table.setForeground(Color.BLACK);                   // 字体颜色

        table.setFont(new Font(null, Font.PLAIN, 13));      // 字体样式

        table.setSelectionForeground(Color.DARK_GRAY);      // 选中后字体颜色

        table.setSelectionBackground(Color.LIGHT_GRAY);     // 选中后字体背景

        table.setGridColor(Color.GRAY);                     // 网格颜色

        // 设置表头

        table.getTableHeader().setFont(new Font(null, Font.BOLD, 18));  // 设置表头名称字体样式

        table.getTableHeader().setForeground(Color.RED);                // 设置表头名称字体颜色

        table.getTableHeader().setResizingAllowed(false);               // 设置不允许手动改变列宽

        table.getTableHeader().setReorderingAllowed(false);             // 设置不允许拖动重新排序各列

        // 设置行高

        table.setRowHeight(30);

        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        ListSelectionModel rowSM = table.getSelectionModel();

        //监听选中行

        rowSM.addListSelectionListener(new ListSelectionListener() {

         public void valueChanged(ListSelectionEvent e) {

         //只处理鼠标按下

         if(e.getValueIsAdjusting() == false) {

         return;

         }

         ListSelectionModel lsm = (ListSelectionModel) e.getSource();

         // 没有选择过

         if(!lsm.isSelectionEmpty()) {

         int selectedRow = lsm.getMinSelectionIndex();

         String nowT = new PostDao().formatTime(new Date());

         toEnrollFrame(selectedRow, nowT);

         }

         }

         // 打开活动查看面板

private void toEnrollFrame(int selectedRow, String nowT) throws HeadlessException {

if((nowT.compareTo(table.getValueAt(selectedRow, 3).toString()) >= 0 && nowT.compareTo(table.getValueAt(selectedRow, 4).toString()) <= 0)) {

try {

ActivityFrame frame = new ActivityFrame(new ActivityDao().getActivity(table.getValueAt(selectedRow, 0).toString()), pr.getAdmin_name());

frame.setVisible(true);

} catch (Exception e2) {

e2.printStackTrace();

}

}

else {

JOptionPane.showMessageDialog(null, “不在可报名时间内,不可查询”, “”, JOptionPane.ERROR_MESSAGE);

}

}

        });

panel_refreshtb = new JPanel();

panel_refreshtb.setBounds(1253, 30, 77, 88);

contentPane.add(panel_refreshtb);

panel_refreshtb.setLayout(null);

btnRefresh = new JButton();

btnRefresh.setBounds(0, 0, 65, 65);

panel_refreshtb.add(btnRefresh);

btnRefresh.addActionListener(this);

btnRefresh.setIcon(new ImageIcon(MainFrame.class.getResource(“/image/refresh.png”)));

lblNewLabel_17 = new JLabel(“刷新列表”);

lblNewLabel_17.setBounds(10, 70, 67, 18);

panel_refreshtb.add(lblNewLabel_17);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “退出登录?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

        // 退出系统

         MainFrame.this.dispose();

         // 显示登录界面

         new LoginFrame().setVisible(true);

        }

    }

});

}

@Override

public void actionPerformed(ActionEvent e) {

// 刷新表格

if (e.getSource()==btnRefresh) {

refreshtb();

}

// 开放编辑

if (e.getSource()==btnStartChange) {

changes(true);

}

// 结束编辑,保存数据

if (e.getSource()==btnStopChange) {

changes(false);

//写入数据库

pdao.update(pr);

JOptionPane.showMessageDialog(this, “修改成功”);

}

// 发布活动

if (e.getSource()==btnpubButton) {

try {

PostFrame frame = new PostFrame(level, pr);

frame.setVisible(true);

} catch (Exception e1) {

e1.printStackTrace();

}

}

// 修改密码

if (e.getSource()==btnNewButton_2) {

try {

new ChangePwdFrame(pr.getAdmin_name()).setVisible(true);

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

private void refreshtb() {

// 删除原有的数据

while(true) {

try {

dtm.removeRow(0);

} catch (Exception e1) {

break;

}

}

// 输入新表数据

List<Activity> temp = new ActivityDao().getInformation(pr.getAdmin_academy(), pr.getAdmin_major(), pr.getAdmin_class());

Collections.reverse(temp);

for(Activity act : temp) {

dtm.addRow(new Object[]{act.getActivity_num(), act.getActivity_name(), new PersonDao().getInformation(act.getAdmin_name()).getAdmin_realname(), act.getActivity_begintime(), act.getActivity_endtime()});

}

}

// 修改个人信息

private void changes(boolean flag) {

btnStopChange.setEnabled(flag);

btnStartChange.setEnabled(!flag);

textField_5.setEditable(flag);

pr.setAdmin_TEL(textField_5.getText());

textField_6.setEditable(flag);

pr.setAdmin_(textField_6.getText());

textField_7.setEditable(flag);

pr.setAdmin_WeChat(textField_7.getText());

textField_8.setEditable(flag);

pr.setAdmin_IDnum(textField_8.getText());

textField_9.setEditable(flag);

pr.setAdmin_politice(textField_9.getText());

textField_10.setEditable(flag);

pr.setAdmin_address(textField_10.getText());

textField_11.setEditable(flag);

pr.setAdmin_hometown(textField_11.getText());

textField_12.setEditable(flag);

pr.setAdmin_dormitory(textField_12.getText());

textField_13.setEditable(flag);

pr.setOther1(textField_13.getText());

textField_14.setEditable(flag);

pr.setOther2(textField_14.getText());

}

}

5.4 更改密码功能

5.4.1 更改密码界面

用户在需要修改密码时,修改密码,输入原密码、新密码,然后确认修改即可修改成功。更改密码界面效果如图5.12所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.12 更改密码界面效果

5.4.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.13所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

图5.13 模块文件的组织

(1)实体类

该类主要用于封装用户的信息:帐号、密码和用户等级等信息,类图如图5.14所示。

Account

-admin_name:String

-admin_pwd:String

-admin_level:int

提供了每个成员变量的getter和setter方法,还有全参构造方法、三个参数(admin_name,admin_pwd,admin_level)的构造方法,并重写了toString方法

图5.14 Account类图

核心代码如下:

package com.wd.registration.entity;

/

 * 账户实体类

*/

public class Account {

// 账号、密码

private String admin_name;

private String admin_pwd;

// 等级 0:普通用户 1-4 班系院校四级管理员

private int admin_level;

public Account() {}

public Account(String admin_name, String admin_pwd, int admin_level) {

super();

this.admin_name = admin_name;

this.admin_pwd = admin_pwd;

this.admin_level = admin_level;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getAdmin_pwd() {

return admin_pwd;

}

public void setAdmin_pwd(String admin_pwd) {

this.admin_pwd = admin_pwd;

}

public int getAdmin_level() {

return admin_level;

}

public void setAdmin_level(int admin_level) {

this.admin_level = admin_level;

}

@Override

public String toString() {

return “Account [admin_name=” + admin_name + “, admin_pwd=” + admin_pwd + “, admin_level=” + admin_level + “]”;

}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的AccountDao,主要用来完成对表tb_account的增删改查操作。登录主要用到里面的查询方法。核心代码如下:

/

 *  修改密码

 *  @param name 账号

 *  @param oldpwd

 *  @param newpwd

 *  @return boolean 是否成功

 */

public boolean changePwd(String name, String oldpwd, String newpwd) {

if(login(name, oldpwd) != null) {

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 编写SQL语句

String sql = “update tb_account set admin_pwd=? where admin_name=?”;

// 创建预编译语句执行器

PreparedStatement stat = conn.prepareStatement(sql);

stat.setString(1, newpwd);

stat.setString(2, name);

// 执行

stat.executeUpdate();

// 关闭

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return true;

}else {

return false;

}

}

(3)界面层

定义ChangePwdFrame用于更改密码界面与引导。具体生成窗口的代码为:new ChangePwdFrame(pr.getAdmin_name()).setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.15 界面组件图

核心代码:

package com.wd.registration.view;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.border.EmptyBorder;

import com.wd.registration.dao.AccountDao;

public class ChangePwdFrame extends JDialog implements ActionListener {

private JPanel contentPane;

private JPasswordField passwordField;

private JPasswordField passwordField_1;

private String username;

/

 * Create the frame.

 */

public ChangePwdFrame(String username) {

this.username = username;

setBounds(50, 50, 225, 200);

setResizable(false);

setLocationRelativeTo(null);

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel(“原密码:”);

lblNewLabel.setBounds(14, 33, 72, 18);

contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel(“新密码:”);

lblNewLabel_1.setBounds(14, 64, 72, 18);

contentPane.add(lblNewLabel_1);

passwordField = new JPasswordField();

passwordField.setBounds(78, 30, 115, 24);

contentPane.add(passwordField);

passwordField_1 = new JPasswordField();

passwordField_1.setBounds(78, 61, 115, 24);

contentPane.add(passwordField_1);

JButton btnNewButton = new JButton(“确认修改”);

btnNewButton.setBounds(48, 113, 113, 27);

this.getRootPane().setDefaultButton(btnNewButton);

contentPane.add(btnNewButton);

btnNewButton.addActionListener(this);

// 模态框

this.setModal(true);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “退出?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

         // 退出

         ChangePwdFrame.this.dispose();

        }

    }

});

}

@Override

public void actionPerformed(ActionEvent arg0) {

String oldpwd = String.valueOf(passwordField.getPassword());

String newpwd = String.valueOf(passwordField_1.getPassword());

if(newpwd.equals(“”)) {

JOptionPane.showMessageDialog(null, “密码不能为空”);

} else if (newpwd.contains(“=”)){

JOptionPane.showMessageDialog(null, “密码不能含有字符‘=’”);

} else if (newpwd.equals(oldpwd)){

JOptionPane.showMessageDialog(null, “密码未改变”);

} else {

if(new AccountDao().changePwd(username, oldpwd, newpwd)) {

JOptionPane.showMessageDialog(null, “密码修改成功”);

dispose();

} else {

JOptionPane.showMessageDialog(null, “密码修改失败,请检查原密码是否输入错误”, “”, JOptionPane.ERROR_MESSAGE);

}

}

}

}

5.5 个人信息修改功能

5.5.1 更改个人信息界面

用户在需要修改信息时,修改按钮,输入需修改的电话、、微信、身份证、政治面貌、家庭地址、籍贯、寝室号、其他1、其他2信息,然后修改完成。修改个人信息界面效果如图5.16所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.16 修改个人信息界面效果

5.5.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.17所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.17 模块文件的组织

(1)实体类

该类主要用于封装用户个人的信息:账号(学号)、姓名、学院、专业、班级、电话、、微信号、身份证号、政治面貌、家庭地址、籍贯、寝室、其他1、其他2信息,类图如图5.18所示。

Person

-admin_name: String

-admin_realname: String

-admin_academy:String

-admin_major:String

-admin_class:String

-admin_TEL:String

-admin_:String

–admin_IDnum:String

-admin_politice:String

-admin_address:String

-admin_hometown:String

-admin_dormitory:String

-other1:String

-other2:String

提供了每个成员变量的getter和setter方法,还有全参构造方法、十四个参数(admin_name、admin_realname等)的构造方法,并重写了toString方法

图5.18 Person类图

核心代码如下:

package com.wd.registration.entity;

/

 * 个人信息实体类

*/

public class Person {

// 账号(学号)

private String admin_name;

// 姓名

private String admin_realname;

// 学院

private String admin_academy;

// 专业

private String admin_major;

// 班级

private String admin_class;

// 电话

private String admin_TEL;

//

private String admin_;

// 微信号

private String admin_WeChat;

// 身份证号

private String admin_IDnum;

// 政治面貌

private String admin_politice;

// 家庭地址

private String admin_address;

// 籍贯

private String admin_hometown;

// 寝室

private String admin_dormitory;

// 其他1

private String other1;

// 其他2

private String other2;

@Override

public String toString() {

return “Person [admin_name=” + admin_name + “, admin_realname=” + admin_realname + “, admin_academy=”

+ admin_academy + “, admin_major=” + admin_major + “, admin_class=” + admin_class + “, admin_TEL=”

+ admin_TEL + “, admin_=” + admin_ + “, admin_WeChat=” + admin_WeChat + “, admin_IDnum=”

+ admin_IDnum + “, admin_politice=” + admin_politice + “, admin_address=” + admin_address

+ “, admin_hometown=” + admin_hometown + “, admin_dormitory=” + admin_dormitory + “, other1=” + other1

+ “, other2=” + other2 + “]”;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getAdmin_realname() {

return admin_realname;

}

public void setAdmin_realname(String admin_realname) {

this.admin_realname = admin_realname;

}

public String getAdmin_academy() {

return admin_academy;

}

public void setAdmin_academy(String admin_academy) {

this.admin_academy = admin_academy;

}

public String getAdmin_major() {

return admin_major;

}

public void setAdmin_major(String admin_major) {

this.admin_major = admin_major;

}

public String getAdmin_class() {

return admin_class;

}

public void setAdmin_class(String admin_class) {

this.admin_class = admin_class;

}

public String getAdmin_TEL() {

return admin_TEL;

}

public void setAdmin_TEL(String admin_TEL) {

this.admin_TEL = admin_TEL;

}

public String getAdmin_() {

return admin_;

}

public void setAdmin_(String admin_) {

this.admin_ = admin_;

}

public String getAdmin_WeChat() {

return admin_WeChat;

}

public void setAdmin_WeChat(String admin_WeChat) {

this.admin_WeChat = admin_WeChat;

}

public String getAdmin_IDnum() {

return admin_IDnum;

}

public void setAdmin_IDnum(String admin_IDnum) {

this.admin_IDnum = admin_IDnum;

}

public String getAdmin_politice() {

return admin_politice;

}

public void setAdmin_politice(String admin_politice) {

this.admin_politice = admin_politice;

}

public String getAdmin_address() {

return admin_address;

}

public void setAdmin_address(String admin_address) {

this.admin_address = admin_address;

}

public String getAdmin_hometown() {

return admin_hometown;

}

public void setAdmin_hometown(String admin_hometown) {

this.admin_hometown = admin_hometown;

}

public String getAdmin_dormitory() {

return admin_dormitory;

}

public void setAdmin_dormitory(String admin_dormitory) {

this.admin_dormitory = admin_dormitory;

}

public String getOther1() {

return other1;

}

public void setOther1(String other1) {

this.other1 = other1;

}

public String getOther2() {

return other2;

}

public void setOther2(String other2) {

this.other2 = other2;

}

public Person(String admin_name, String admin_realname, String admin_academy, String admin_major,

String admin_class, String admin_TEL, String admin_, String admin_WeChat, String admin_IDnum,

String admin_politice, String admin_address, String admin_hometown, String admin_dormitory, String other1,

String other2) {

super();

this.admin_name = admin_name;

this.admin_realname = admin_realname;

this.admin_academy = admin_academy;

this.admin_major = admin_major;

this.admin_class = admin_class;

this.admin_TEL = admin_TEL;

this.admin_ = admin_;

this.admin_WeChat = admin_WeChat;

this.admin_IDnum = admin_IDnum;

this.admin_politice = admin_politice;

this.admin_address = admin_address;

this.admin_hometown = admin_hometown;

this.admin_dormitory = admin_dormitory;

this.other1 = other1;

this.other2 = other2;

}

public Person() {}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的PersonDao,主要用来完成对表tb_person的增删改查操作。登录主要用到里面的查询方法。核心代码如下:

package com.wd.registration.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.List;

import com.wd.registration.entity.Person;

import com.wd.registration.utils.DBUtils;

/

 * 个人数据处理层

*/

public class PersonDao {

/

 *  获取个人信息

 *  @param name 账号

 *  @return 返回个人信息,不正确返回null

 */

public Person getInformation(String name) {

Person pr = null;

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 创建语句执行器

Statement stat = conn.createStatement();

// 执行并返回结果

String sql = “select * from tb_person where admin_name=” + name;

ResultSet rs = stat.executeQuery(sql);

// 迭代

if(rs.next()) {

String admin_name = rs.getString(“admin_name”);

String admin_realname = rs.getString(“admin_realname”);

String admin_academy = rs.getString(“admin_academy”);

String admin_major = rs.getString(“admin_major”);

String admin_class = rs.getString(“admin_class”);

String admin_TEL = rs.getString(“admin_TEL”);

String admin_ = rs.getString(“admin_”);

String admin_WeChat = rs.getString(“admin_WeChat”);

String admin_IDnum = rs.getString(“admin_IDnum”);

String admin_politice = rs.getString(“admin_politice”);

String admin_address = rs.getString(“admin_address”);

String admin_hometown = rs.getString(“admin_hometown”);

String admin_dormitory = rs.getString(“admin_dormitory”);

String other1 = rs.getString(“other1”);

String other2 = rs.getString(“other2”);

// 组装成对象

pr = new Person(admin_name, admin_realname, admin_academy, admin_major, admin_class, admin_TEL, admin_, admin_WeChat, admin_IDnum, admin_politice, admin_address, admin_hometown, admin_dormitory, other1, other2);

}

// 关闭

rs.close();

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return pr;

}

/

 *  存储个人信息

 *  @param Person 信息

 */

public void insert(Person pr) {

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 编写SQL语句

String sql = “insert into tb_person(admin_name, admin_realname, admin_academy, admin_major, admin_class) value(?, ?, ?, ?, ?)”;

// 创建预编译语句执行器

PreparedStatement stat = conn.prepareStatement(sql);

stat.setString(1, pr.getAdmin_name());

stat.setString(2, pr.getAdmin_realname());

stat.setString(3, pr.getAdmin_academy());

stat.setString(4, pr.getAdmin_major());

stat.setString(5, pr.getAdmin_class());

// 执行

stat.executeUpdate();

// 关闭

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/

 *  能否具备五大基本信息,可否注册成功注册

 *  @param name 五个信息String[]

 *  @return boolean数组表示几个成功

 */

public boolean[] canRegister(Person pr) {

boolean[] flag = {true, true, true, true, true};

if(pr.getAdmin_name().equals(“”)) flag[0] = false;

if(pr.getAdmin_realname().equals(“”)) flag[1] = false;

if(pr.getAdmin_academy().equals(“”)) flag[2] = false;

if(pr.getAdmin_major().equals(“”)) flag[3] = false;

if(pr.getAdmin_class().equals(“”)) flag[4] = false;

return flag;

}

/

 *  更新个人信息

 *  @param Person 信息

 */

public void update(Person pr) {

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 编写SQL语句

String sql = “update tb_person set admin_TEL=?, admin_=?, admin_WeChat=?,”

+ ” admin_IDnum=?, admin_politice=?, admin_address=?, admin_hometown=?,”

+ ” admin_dormitory=?, other1=?, other2=? where admin_name=?”;

// 创建预编译语句执行器

PreparedStatement stat = conn.prepareStatement(sql);

stat.setString(1, pr.getAdmin_TEL());

stat.setString(2, pr.getAdmin_());

stat.setString(3, pr.getAdmin_WeChat());

stat.setString(4, pr.getAdmin_IDnum());

stat.setString(5, pr.getAdmin_politice());

stat.setString(6, pr.getAdmin_address());

stat.setString(7, pr.getAdmin_hometown());

stat.setString(8, pr.getAdmin_dormitory());

stat.setString(9, pr.getOther1());

stat.setString(10, pr.getOther2());

stat.setString(11, pr.getAdmin_name());

// 执行

stat.executeUpdate();

// 关闭

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

5.6 发布活动功能

5.6.1 发布活动界面

用户需要发布活动时,发布活动按钮,跳转至活动发布界面。在界面输入发布范围、发布名称、发布时间、活动地点、报名持续时间、加分情况、其他事项,然后立即发布即可发布成功。发布活动界面效果如图5.19所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.19 发布活动界面效果

5.6.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.20所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.20 模块文件的组织

(1)实体类

该类主要用于封装发布活动的信息:活动号、活动名、活动发布范围、发布人、活动开始时间、活动结束时间等,类图如图5.21所示。

Activity

-activity_num: String

-activity_name: String

-activity_range:String

-admin_name:String

-activity_begintime:String

-activity_endtime:String

-activity_content:String

提供了每个成员变量的getter和setter方法,还有全参构造方法、七参数(activity_num、activity_name等)的构造方法,并重写了toString方法

图5.21 Activity类图

核心代码如下:

package com.wd.registration.entity;

/

 * 活动信息实体类

*/

public class Activity {

private String activity_num;

private String activity_name;

private String activity_range;

private String admin_name;

private String activity_begintime;

private String activity_endtime;

private String activity_content;

public String getActivity_num() {

return activity_num;

}

public void setActivity_num(String activity_num) {

this.activity_num = activity_num;

}

public String getActivity_name() {

return activity_name;

}

public void setActivity_name(String activity_name) {

this.activity_name = activity_name;

}

public String getActivity_range() {

return activity_range;

}

public void setActivity_range(String activity_range) {

this.activity_range = activity_range;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getActivity_begintime() {

return activity_begintime;

}

public void setActivity_begintime(String activity_begintime) {

this.activity_begintime = activity_begintime;

}

public String getActivity_endtime() {

return activity_endtime;

}

public void setActivity_endtime(String activity_endtime) {

this.activity_endtime = activity_endtime;

}

public String getActivity_content() {

return activity_content;

}

public void setActivity_content(String activity_content) {

this.activity_content = activity_content;

}

public Activity(String activity_num, String activity_name, String activity_range, String admin_name,

String activity_begintime, String activity_endtime, String activity_content) {

super();

this.activity_num = activity_num;

this.activity_name = activity_name;

this.activity_range = activity_range;

this.admin_name = admin_name;

this.activity_begintime = activity_begintime;

this.activity_endtime = activity_endtime;

this.activity_content = activity_content;

}

public Activity() {}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的ActivityDao,主要用来完成对表tb_activity的增删改查操作。发布活动主要用到里面的新增方法。核心代码如下:

/

 *  发布新活动信息

 *  @param 信息

 */

public void insert(Activity act) {

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 编写SQL语句

String sql = “insert into tb_activity(activity_num, activity_name, activity_range, admin_name, activity_begintime, activity_endtime, activity_content) value(?, ?, ?, ?, ?, ?, ?)”;

// 创建预编译语句执行器

PreparedStatement stat = conn.prepareStatement(sql);

stat.setString(1, act.getActivity_num());

stat.setString(2, act.getActivity_name());

stat.setString(3, act.getActivity_range());

stat.setString(4, act.getAdmin_name());

stat.setString(5, act.getActivity_begintime());

stat.setString(6, act.getActivity_endtime());

stat.setString(7, act.getActivity_content());

// 执行

stat.executeUpdate();

// 关闭

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

(3)界面层

定义PostFrame用于实现发布活动界面与引导,可以选择发布范围(根据发布者的所在学院专业班级以及发布者的用户等级而定)、发布活动具体信息(如:名称、时间、地点、持续时间、加分情况等等)。生成窗口的代码:PostFrame frame = new PostFrame(level, pr);frame.setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.22 界面组件图

核心代码:

package com.wd.registration.view;

import java.awt.BorderLayout;

import java.awt.HeadlessException;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Calendar;

import java.util.Date;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.JSpinner.DefaultEditor;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingConstants;

import javax.swing.border.EmptyBorder;

import com.wd.registration.dao.ActivityDao;

import com.wd.registration.dao.PostDao;

import com.wd.registration.entity.Activity;

import com.wd.registration.entity.Person;

import com.wd.registration.entity.Post;

public class PostFrame extends JDialog {

private JPanel contentPane;

private JTextField textField_1;

private JTextField textField_3;

private JTextField textField;

private JTextArea textArea;

private JComboBox comboBox;

private JSpinner spinner;

private JSpinner spinner_1;

private DefaultEditor editor;

private JSpinner spinner_2_0, spinner_2_1, spinner_2_2, spinner_2_3;

/

 * Create the frame.

 */

public PostFrame(int level, Person pr) {

setTitle(“活动发布”);

setBounds(100, 100, 450, 400);

setResizable(false);

setLocationRelativeTo(null);

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new BorderLayout(0, 0));

setContentPane(contentPane);

JButton btnNewButton = new JButton(“立即发布”);

contentPane.add(btnNewButton, BorderLayout.SOUTH);

btnNewButton.addActionListener((event)->{

if(textArea.getText().length() >= 130) {

JOptionPane.showMessageDialog(null, “其他事项字数过多”, “”, JOptionPane.ERROR_MESSAGE);

}

else {

// 发布活动至数据库

postActivity(pr);

}

});

JPanel main_panel = new JPanel();

contentPane.add(main_panel, BorderLayout.CENTER);

main_panel.setLayout(null);

JLabel lblNewLabel_1 = new JLabel(“发布范围:”);

lblNewLabel_1.setBounds(38, 10, 75, 18);

main_panel.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel(“活动名称:”);

lblNewLabel_2.setBounds(38, 41, 75, 18);

main_panel.add(lblNewLabel_2);

textField_1 = new JTextField();

textField_1.setBounds(121, 38, 301, 24);

main_panel.add(textField_1);

textField_1.setColumns(10);

JLabel lblNewLabel_3 = new JLabel(“活动时间:”);

lblNewLabel_3.setBounds(40, 72, 75, 18);

main_panel.add(lblNewLabel_3);

JLabel lblNewLabel_3_1 = new JLabel(“活动地点:”);

lblNewLabel_3_1.setBounds(38, 103, 75, 18);

main_panel.add(lblNewLabel_3_1);

textField_3 = new JTextField();

textField_3.setBounds(121, 100, 301, 24);

main_panel.add(textField_3);

textField_3.setColumns(10);

JLabel lblNewLabel_5 = new JLabel(“报名持续时间:”);

lblNewLabel_5.setBounds(8, 134, 105, 18);

main_panel.add(lblNewLabel_5);

JLabel lblNewLabel_4 = new JLabel(“其他事项:”);

lblNewLabel_4.setBounds(38, 221, 75, 18);

main_panel.add(lblNewLabel_4);

spinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 0.5));

spinner.setBounds(121, 131, 48, 24);

main_panel.add(spinner);

editor = (DefaultEditor) spinner.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_6 = new JLabel(“天”);

lblNewLabel_6.setBounds(176, 134, 21, 18);

main_panel.add(lblNewLabel_6);

comboBox = new JComboBox(getList(level, pr));

comboBox.setBounds(121, 7, 301, 24);

main_panel.add(comboBox);

JLabel lblNewLabel_8 = new JLabel(“加分情况:”);

lblNewLabel_8.setBounds(224, 134, 75, 18);

main_panel.add(lblNewLabel_8);

spinner_1 = new JSpinner(new SpinnerNumberModel(0, 0, 5, 0.5));

spinner_1.setBounds(306, 131, 48, 24);

main_panel.add(spinner_1);

editor = (DefaultEditor) spinner_1.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_6_1 = new JLabel(“分”);

lblNewLabel_6_1.setBounds(363, 134, 21, 18);

main_panel.add(lblNewLabel_6_1);

textArea = new JTextArea();

textArea.setBounds(121, 168, 301, 124);

main_panel.add(textArea);

textArea.setLineWrap(true);

spinner_2_0 = new JSpinner(new SpinnerNumberModel(2020, 2020, 2050, 1));

spinner_2_0.setBounds(121, 69, 56, 24);

main_panel.add(spinner_2_0);

editor = (DefaultEditor) spinner_2_0.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_9 = new JLabel(“年”);

lblNewLabel_9.setBounds(180, 72, 21, 18);

main_panel.add(lblNewLabel_9);

spinner_2_1 = new JSpinner(new SpinnerNumberModel(1, 1, 12, 1));

spinner_2_1.setBounds(200, 69, 36, 24);

main_panel.add(spinner_2_1);

editor = (DefaultEditor) spinner_2_1.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_9_1 = new JLabel(“月”);

lblNewLabel_9_1.setBounds(235, 72, 21, 18);

main_panel.add(lblNewLabel_9_1);

spinner_2_2 = new JSpinner(new SpinnerNumberModel(1, 1, 31, 1));

spinner_2_2.setBounds(255, 69, 36, 24);

main_panel.add(spinner_2_2);

editor = (DefaultEditor) spinner_2_2.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_9_1_1 = new JLabel(“日”);

lblNewLabel_9_1_1.setBounds(290, 72, 21, 18);

main_panel.add(lblNewLabel_9_1_1);

spinner_2_3 = new JSpinner(new SpinnerNumberModel(1, 1, 24, 1));

spinner_2_3.setBounds(310, 69, 36, 24);

main_panel.add(spinner_2_3);

editor = (DefaultEditor) spinner_2_3.getEditor();

editor.getTextField().setEditable(false);

JLabel lblNewLabel_9_1_1_1 = new JLabel(“时”);

lblNewLabel_9_1_1_1.setBounds(350, 72, 21, 18);

main_panel.add(lblNewLabel_9_1_1_1);

JButton btnNewButton_1 = new JButton(“now”);

btnNewButton_1.setBounds(374, 68, 48, 27);

main_panel.add(btnNewButton_1);

btnNewButton_1.addActionListener((event)->{

Calendar now = Calendar.getInstance();

spinner_2_0.setValue(now.get(Calendar.YEAR));

spinner_2_1.setValue(now.get(Calendar.MONTH) + 1);

spinner_2_2.setValue(now.get(Calendar.DAY_OF_MONTH));

spinner_2_3.setValue(now.get(Calendar.HOUR_OF_DAY));

});

JPanel notice_panel = new JPanel();

contentPane.add(notice_panel, BorderLayout.NORTH);

notice_panel.setLayout(new BoxLayout(notice_panel, BoxLayout.X_AXIS));

JLabel lblNewLabel = new JLabel(“您拥有”);

notice_panel.add(lblNewLabel);

textField = new JTextField();

textField.setHorizontalAlignment(SwingConstants.CENTER);

textField.setEditable(false);

//根据管理员等级不同switch不同的权限

switch(level) {

case 1:textField.setText(“班级”);break;

case 2:textField.setText(“专业”);break;

case 3:textField.setText(“学院”);break;

default:textField.setText(“学校”);break; //case 4

}

notice_panel.add(textField);

textField.setColumns(10);

JLabel lblNewLabel_7 = new JLabel(“活动发布权限,获取更高级权限请联系项目开发人员”);

notice_panel.add(lblNewLabel_7);

// 模态框

this.setModal(true);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “确认取消发布?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

        // 退出

         PostFrame.this.dispose();

        }

    }

});

}

private void postActivity(Person pr) throws NumberFormatException, HeadlessException {

Date nowTime = new Date();

PostDao pod = new PostDao();

Post po = pod.setPost(nowTime, String.valueOf(comboBox.getSelectedItem()), textField_1.getText(),

String.valueOf(spinner_2_0.getValue()), String.valueOf(spinner_2_1.getValue()), String.valueOf(spinner_2_2.getValue()),

String.valueOf(spinner_2_3.getValue()), textField_3.getText(), Double.parseDouble(String.valueOf(spinner.getValue())),

String.valueOf(spinner_1.getValue()), textArea.getText());

if(pod.canPost(po) == true) {

// 将Post对象改装成Activity对象

Activity act = new Activity();

act.setActivity_num(pod.newOrderNum(nowTime));

act.setActivity_name(po.getActivity_name());

act.setActivity_range(po.getActivity_range());

act.setAdmin_name(pr.getAdmin_name());

act.setActivity_begintime(pod.formatTime(nowTime));

act.setActivity_endtime(pod.formatTime(pod.addDate(nowTime, po.getActivity_duration())));

act.setActivity_content(po.toString());

// 将Activity发布到数据库

new ActivityDao().insert(act);

JOptionPane.showMessageDialog(null, “发布成功,主面板刷新可查看所需内容”);

this.dispose();

}

else {

JOptionPane.showMessageDialog(null, “内容均不可为空”, “”, JOptionPane.ERROR_MESSAGE);

}

}

private String[] getList(int level, Person pr) {

String[] list = new String[level];

switch(level) {

case 4: list[3] = “中北大学”;

case 3: list[2] = pr.getAdmin_academy();

case 2: list[1] = pr.getAdmin_major();

case 1: list[0] = pr.getAdmin_class();

}

return list;

}

}

5.7 查看活动详情功能

5.7.1 查看活动详情界面

用户在需要看发布的新的活动时,活动进入活动详情,在里面可以看到活动的具体信息,如果想要报名可以报名按钮完成报名。查看活动详情界面效果如图5.23所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.23 查看活动详情界面效果

5.7.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.24所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.24 模块文件的组织

(1)实体类

该类主要用于封装活动的信息:活动号、活动名、活动发布范围、发布人、活动开始时间、活动结束时间等,类图如图5.25所示。

Activity

-activity_num: String

-activity_name: String

-activity_range:String

-admin_name:String

-activity_begintime:String

-activity_endtime:String

-activity_content:String

提供了每个成员变量的getter和setter方法,还有全参构造方法、七参数(activity_num、activity_name等)的构造方法,并重写了toString方法

图5.25 Activity类图

核心代码如下:

package com.wd.registration.entity;

/

 * 活动信息实体类

*/

public class Activity {

private String activity_num;

private String activity_name;

private String activity_range;

private String admin_name;

private String activity_begintime;

private String activity_endtime;

private String activity_content;

public String getActivity_num() {

return activity_num;

}

public void setActivity_num(String activity_num) {

this.activity_num = activity_num;

}

public String getActivity_name() {

return activity_name;

}

public void setActivity_name(String activity_name) {

this.activity_name = activity_name;

}

public String getActivity_range() {

return activity_range;

}

public void setActivity_range(String activity_range) {

this.activity_range = activity_range;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public String getActivity_begintime() {

return activity_begintime;

}

public void setActivity_begintime(String activity_begintime) {

this.activity_begintime = activity_begintime;

}

public String getActivity_endtime() {

return activity_endtime;

}

public void setActivity_endtime(String activity_endtime) {

this.activity_endtime = activity_endtime;

}

public String getActivity_content() {

return activity_content;

}

public void setActivity_content(String activity_content) {

this.activity_content = activity_content;

}

public Activity(String activity_num, String activity_name, String activity_range, String admin_name,

String activity_begintime, String activity_endtime, String activity_content) {

super();

this.activity_num = activity_num;

this.activity_name = activity_name;

this.activity_range = activity_range;

this.admin_name = admin_name;

this.activity_begintime = activity_begintime;

this.activity_endtime = activity_endtime;

this.activity_content = activity_content;

}

public Activity() {}

}

该类主要用于封装报名的信息:活动号、报名者账号,类图如图5.26所示。

Enroll

-activity_num: String

-admin_name: String

提供了每个成员变量的getter和setter方法,还有全参构造方法、两个参数(activity_num、admin_name)的构造方法,并重写了toString方法

图5.26 Enroll类图

核心代码如下:

package com.wd.registration.entity;

/

 * 报名实体类

*/

public class Enroll {

// 活动号

private String activity_num;

// 报名者账号

private String admin_name;

public Enroll(String activity_num, String admin_name) {

super();

this.activity_num = activity_num;

this.admin_name = admin_name;

}

public String getActivity_num() {

return activity_num;

}

public void setActivity_num(String activity_num) {

this.activity_num = activity_num;

}

public String getAdmin_name() {

return admin_name;

}

public void setAdmin_name(String admin_name) {

this.admin_name = admin_name;

}

public Enroll() {}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的ActivityDao,主要用来完成对表tb_activity的增删改查操作。活动详情查看主要用到里面的查询方法。核心代码如下:

/

 *  获取列表信息

 *  @param name 账号

 *  @return 返回个人信息,不正确返回null

 */

public List<Activity> getInformation(String admin_academy, String admin_major, String admin_class) {

List<Activity> actArr = new ArrayList();

Activity act = null;

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 创建语句执行器

Statement stat = conn.createStatement();

// 执行并返回结果

String sql = “select * from tb_activity where activity_range='” + admin_academy + “‘ or activity_range='”

+ admin_major + “‘ or activity_range='” + admin_class + “‘ or activity_range=’长沙理工大学'”;

ResultSet rs = stat.executeQuery(sql);

// 迭代

while(rs.next()) {

String activity_num = rs.getString(“activity_num”);

String activity_name = rs.getString(“activity_name”);

String activity_range = rs.getString(“activity_range”);

String admin_name = rs.getString(“admin_name”);

String activity_begintime = rs.getString(“activity_begintime”);

String activity_endtime = rs.getString(“activity_endtime”);

String activity_content = rs.getString(“activity_content”);

// 组装成对象

act = new Activity(activity_num, activity_name, activity_range, admin_name, activity_begintime, activity_endtime, activity_content);

actArr.add(act);

}

// 关闭

rs.close();

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return actArr;

}

/

 *  关于活动

 *  @param name 账号

 *  @return 返回活动信息

 */

public Activity getActivity(String num) {

Activity act = null;

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 创建语句执行器

Statement stat = conn.createStatement();

// 执行并返回结果

String sql = “select * from tb_activity where activity_num='” + num + “‘”;

ResultSet rs = stat.executeQuery(sql);

// 迭代

if(rs.next()) {

String activity_num = rs.getString(“activity_num”);

String activity_name = rs.getString(“activity_name”);

String activity_range = rs.getString(“activity_range”);

String admin_name = rs.getString(“admin_name”);

String activity_begintime = rs.getString(“activity_begintime”);

String activity_endtime = rs.getString(“activity_endtime”);

String activity_content = rs.getString(“activity_content”);

// 组装成对象

act = new Activity(activity_num, activity_name, activity_range, admin_name, activity_begintime, activity_endtime, activity_content);

}

// 关闭

rs.close();

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return act;

}

Dao层用来定义操作数据库的EnrollDao,主要用来完成对表tb_enroll的增删改查操作。活动详情查看主要用到里面的查询方法。核心代码如下:

package com.wd.registration.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import com.wd.registration.entity.Enroll;

import com.wd.registration.entity.Person;

import com.wd.registration.utils.DBUtils;

/

 * 报名数据处理

*/

public class EnrollDao {

/

 *  插入报名信息,如果有过就不能加

 *  @param name 账号

 *  @return 是否插入成功

 */

public boolean insert(Enroll en) {

boolean flag = true;

Connection conn = null;

Statement stat = null;

ResultSet rs = null;

PreparedStatement stat1 = null;

try {

// 利用数据库工具获取连接

conn = DBUtils.getConnection();

// 创建语句执行器

stat = conn.createStatement();

// 执行并返回结果

String sql = “select * from tb_enroll where activity_num='” + en.getActivity_num() + “‘and admin_name='” + en.getAdmin_name() + “‘”;

rs = stat.executeQuery(sql);

// 迭代

if(rs.next()) {

flag = false;

} else {

String sql1 = “insert into tb_enroll(activity_num, admin_name) value(?, ?)”;

// 创建预编译语句执行器

stat1 = conn.prepareStatement(sql1);

stat1.setString(1, en.getActivity_num());

stat1.setString(2, en.getAdmin_name());

// 执行

stat1.executeUpdate();

stat1.close();

}

} catch (Exception e) {

flag = false;

} finally {

// 关闭连接等

try {

rs.close();

stat.close();

conn.close();

} catch (Exception e1) {

e1.printStackTrace();

}

}

return flag;

}

/

 *  获取报名人员列表

 *  @param 活动号

 *  @return 报名人员列表

 */

public List<Person> getInformation(String activity_num) {

List<Person> prArr = new ArrayList();

Person pr = null;

try {

// 利用数据库工具获取连接

Connection conn = DBUtils.getConnection();

// 创建语句执行器

Statement stat = conn.createStatement();

// 执行并返回结果

String sql = “select admin_name from tb_enroll where activity_num='” + activity_num + “‘”;

ResultSet rs = stat.executeQuery(sql);

// 迭代

while(rs.next()) {

pr = new PersonDao().getInformation(rs.getString(“admin_name”));

prArr.add(pr);

}

// 关闭

rs.close();

stat.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return prArr;

}

}

(3)界面层

定义ActivityFrame用于实现查看活动详情页面与引导,用户活动列表上的相关活动,如果不在可报名时间内则无法点入,否则进入查看活动详情页面。可显示活动的详细信息以及截至目前的报名情况简表,若用户即为此活动的发布者,还可以选择导出活动报名表的详表。生成窗口的代码:ActivityFrame frame = new ActivityFrame(new ActivityDao().getActivity(table.getValueAt(selectedRow, 0).toString()), pr.getAdmin_name());frame.setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.27 界面组件图

核心代码:

package com.wd.registration.view;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.List;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextPane;

import javax.swing.SwingConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.border.LineBorder;

import javax.swing.border.TitledBorder;

import javax.swing.table.DefaultTableModel;

import com.wd.registration.dao.EnrollDao;

import com.wd.registration.entity.Activity;

import com.wd.registration.entity.Enroll;

import com.wd.registration.entity.Person;

public class ActivityFrame extends JDialog implements ActionListener {

private JPanel contentPane;

private JTable table;

private DefaultTableModel dtm; // 表单

private JButton btnNewButton_1;

private JButton btnNewButton;

private String admin_name;

private Activity act;

private List<Person> temp;

/

 * Create the frame.

 */

public ActivityFrame(Activity act, String admin_name) {

this.act = act;

this.admin_name = admin_name;

// 设置窗口默认关闭动作为执行window事件中定义的操作

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

setTitle(act.getActivity_name());

setBounds(100, 100, 640, 650);

setResizable(false);

setLocationRelativeTo(null);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JPanel activity_panel = new JPanel();

activity_panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), “活动详情面板”, TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));

activity_panel.setBounds(15, 7, 595, 245);

contentPane.add(activity_panel);

activity_panel.setLayout(null);

JTextPane textPane = new JTextPane();

textPane.setFont(new Font(“方正粗黑宋简体”, Font.PLAIN, 18));

textPane.setText(act.getActivity_content());

textPane.setEditable(false);

textPane.setBounds(15, 25, 475, 205);

activity_panel.add(textPane);

btnNewButton_1 = new JButton(“报名”);

btnNewButton_1.addActionListener(this);

btnNewButton_1.setFont(new Font(“华文新魏”, Font.BOLD, 20));

btnNewButton_1.setBounds(500, 25, 80, 205);

activity_panel.add(btnNewButton_1);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(15, 280, 595, 280);

contentPane.add(scrollPane);

// 建立表格

String[] header = {“序号”, “姓名”, “学院”, “专业”, “班级”};

dtm = new DefaultTableModel(null, header) {

// 禁止编辑表格

    @Override

    public boolean isCellEditable(int row, int column) {

        return false;

    }

};

// 生成数据

temp = new EnrollDao().getInformation(act.getActivity_num());

int count = 0;

for(Person p : temp) {

dtm.addRow(new Object[] {++count, p.getAdmin_realname(), p.getAdmin_academy(), p.getAdmin_major(), p.getAdmin_class()});

}

table = new JTable(dtm);

scrollPane.setViewportView(table);

// 设置表格内容颜色

        table.setForeground(Color.BLACK);                   // 字体颜色

        table.setFont(new Font(null, Font.PLAIN, 13));      // 字体样式

        table.setSelectionForeground(Color.DARK_GRAY);      // 选中后字体颜色

        table.setSelectionBackground(Color.LIGHT_GRAY);     // 选中后字体背景

        table.setGridColor(Color.GRAY);                     // 网格颜色

        // 设置表头

        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));  // 设置表头名称字体样式

        table.getTableHeader().setForeground(Color.BLACK);                // 设置表头名称字体颜色

        table.getTableHeader().setResizingAllowed(false);               // 设置不允许手动改变列宽

        table.getTableHeader().setReorderingAllowed(false);             // 设置不允许拖动重新排序各列

        // 设置行高

        table.setRowHeight(30);

        // 设置列宽

        table.getColumnModel().getColumn(0).setPreferredWidth(10);

        table.getColumnModel().getColumn(1).setPreferredWidth(10);

        table.getColumnModel().getColumn(4).setPreferredWidth(10);

JLabel lblNewLabel = new JLabel(“报名情况”);

lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel.setFont(new Font(“华文新魏”, Font.PLAIN, 20));

lblNewLabel.setBounds(260, 250, 105, 30);

contentPane.add(lblNewLabel);

btnNewButton = new JButton(“导出详细情况表”);

btnNewButton.addActionListener(this);

btnNewButton.setFont(new Font(“华文新魏”, Font.PLAIN, 15));

btnNewButton.setBounds(450, 565, 145, 30);

contentPane.add(btnNewButton);

if(!admin_name.equals(act.getAdmin_name())) {

btnNewButton.setEnabled(false);

}

// 模态框

this.setModal(true);

// 添加确认关闭代码

addWindowListener (new WindowAdapter() {

    @Override

    public void windowClosing(WindowEvent e) {

        int result = JOptionPane.showConfirmDialog(null, “确定关闭?”, “”,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {

        // 退出系统

         ActivityFrame.this.dispose();

        }

    }

});

}

public void actionPerformed(ActionEvent e) {

// 添加报名信息

if(e.getSource()==btnNewButton_1) {

if(new EnrollDao().insert(new Enroll(act.getActivity_num(), admin_name))) {

JOptionPane.showMessageDialog(this, “报名成功”);

} else {

JOptionPane.showMessageDialog(this, “报名失败,可能已经报过名了”, “错误”, JOptionPane.ERROR_MESSAGE);

}

}

// 导出Excel

if(e.getSource()==btnNewButton) {

try {

MakeExcelFrame frame = new MakeExcelFrame(act.getActivity_name(), temp);

frame.setVisible(true);

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

}

5.8 导出Excel表格页面设计

5.8.1 导出Excel表格界面

用户在需要导出信息时,活动进入活动详情,在里面可以看到活动的具体信息,如果需要导出信息,下面的导出成excel表格。导出页面如图5.28所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.28导出Excel界面设计

5.8.2 该模块涉及到的文件和类

通过详细的分析,该模块涉及到的文件和类在工程中的组织如图5.29所示。

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.29 模块文件的组织

(1)实体类

该类主要用于封装表格的信息:表格名称、表格路径、表格标题、内容,类图如图5.30所示。

Excel

-excel_name: String

-excel_path: String

-table_name:String

-table_titles:List

-table_content:List

提供了每个成员变量的getter和setter方法,还有全参构造方法、五个参数(excel_name、excel_path等)的构造方法,并重写了toString方法

图5.30 Excel类图

核心代码:

package com.wd.registration.entity;

import java.util.List;

public class Excel {

private String excel_name; // .xls

private String excel_path;

private String table_name;

private List<String> table_titles;

private List<Person> table_content;

public Excel() {}

public String getExcel_name() {

return excel_name;

}

public void setExcel_name(String excel_name) {

this.excel_name = excel_name;

}

public String getExcel_path() {

return excel_path;

}

public void setExcel_path(String excel_path) {

this.excel_path = excel_path;

}

public String getTable_name() {

return table_name;

}

public void setTable_name(String table_name) {

this.table_name = table_name;

}

public List<String> getTable_titles() {

return table_titles;

}

public void setTable_titles(List<String> table_titles) {

this.table_titles = table_titles;

}

public List<Person> getTable_content() {

return table_content;

}

public void setTable_content(List<Person> table_content) {

this.table_content = table_content;

}

public Excel(String excel_name, String excel_path, String table_name, List<String> table_titles,

List<Person> table_content) {

super();

this.excel_name = excel_name;

this.excel_path = excel_path;

this.table_name = table_name;

this.table_titles = table_titles;

this.table_content = table_content;

}

}

(2)文件操作层(Dao层)

Dao层用来定义操作数据库的ExcelDao,主要用来完成对生成excel表格生成的方法。核心代码如下:

package com.wd.registration.dao;

import java.io.File;

import com.wd.registration.entity.Excel;

import com.wd.registration.entity.Person;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

public class ExcelDao {

// 生成Excel

public void CreatExcel(Excel exc) throws Exception {

File file=new File(exc.getExcel_path() + exc.getExcel_name());

file.createNewFile();

WritableWorkbook workbook=Workbook.createWorkbook(file);

WritableSheet sheet=workbook.createSheet(exc.getTable_name(), 0);

Label label=null;

for(int i=0;i<exc.getTable_titles().size();i++){

label=new Label(i,0,exc.getTable_titles().get(i));

sheet.addCell(label);

}

int row = 1;

for(Person arow : exc.getTable_content()) {

int i = 0;

for(String item: exc.getTable_titles()) {

if(item.equals(“学号”)){

sheet.addCell(new Label(i,row,arow.getAdmin_name()));

} else if (item.equals(“姓名”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_realname()));

} else if (item.equals(“学院”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_academy()));

} else if (item.equals(“专业”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_major()));

} else if (item.equals(“班级”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_class()));

} else if (item.equals(“电话”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_TEL()));

} else if (item.equals(“”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_()));

} else if (item.equals(“微信”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_WeChat()));

} else if (item.equals(“身份证”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_IDnum()));

} else if (item.equals(“政治面貌”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_politice()));

} else if (item.equals(“家庭住址”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_address()));

} else if (item.equals(“籍贯”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_hometown()));

} else if (item.equals(“寝室号”)) {

sheet.addCell(new Label(i,row,arow.getAdmin_dormitory()));

} else if (item.equals(“其他1”)) {

sheet.addCell(new Label(i,row,arow.getOther1()));

} else if (item.equals(“其他2”)) {

sheet.addCell(new Label(i,row,arow.getOther2()));

}

i++; // 自增

}

row += 1;

}

workbook.write();

workbook.close();

}

}

(3)界面层

定义MakeExcelFrame用于实现生成画Excel界面与引导,可以根据本活动组织方的需要自行收集报名者信息生成报名统计表。生成窗口代码有:MakeExcelFrame Navicat Premium 12.0.26激活 frame = new MakeExcelFrame(act.getActivity_name(), temp);frame.setVisible(true);

Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)

 图5.31 导出Excel界面组件

今天的文章
Navicat Premium 12.0.26激活(基于Swing的校园活动管理系统)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/119039.html

(0)
编程小号编程小号
上一篇 2024-07-11
下一篇 2024-07-11

相关推荐