***创建应用程序类 ***
创建应用程序类,存在全局数据,所有的窗口界面都可以共享应用程序类里存放的数据。
1、在net.cqf.student包里创建app子包
2、在net.cqf.student.app包里创建Application类
package net.cqf.student.app;
import net.cqf.student.gui.LoginFrame;
import net.cqf.student.gui.MainFrame;
import net.cqf.student.gui.RegisterFrame;
/**
* 功能:应用程序类,存放全局数据
* 作者:陈秋帆
* 日期:2020年07月12日
*/
public class Application {
public static int id; // 登录用户标识符
public static String username = "admin"; // 登录用户名
public static String password; // 登录用户密码
public static LoginFrame loginFrame; // 登录窗口
public static MainFrame mainFrame; // 主窗口
public static RegisterFrame registerFrame; // 注册窗口
}
创建net.cqf.student.gui包,在里面创建三个类LoginFrame、RegisterFrame和MainFrame
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.bean.Status;
import net.cqf.student.bean.User;
import net.cqf.student.service.StatusService;
import net.cqf.student.service.UserService;
import net.cqf.student.service.impl.StatusServiceImpl;
import net.cqf.student.service.impl.UserServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginFrame extends JFrame{
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnRegister;
private JPanel panel,panel1,panel2,panel3;
public LoginFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定");
btnCancel = new JButton("取消");
btnRegister = new JButton("注册");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
panel.setLayout(new GridLayout(3,1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnRegister.setMnemonic(KeyEvent.VK_R);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login();
}
});
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtUsername.requestFocus();
}
});
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Application.loginFrame.setVisible(false);
Application.registerFrame = new RegisterFrame("用户注册");
}
});
}
private void login() {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserServiceImpl();
User user = userService.login(username, password);
if (user != null) {
Application.loginFrame.setVisible(false);
StatusService statusService = new StatusServiceImpl();
Status status = statusService.findStatusById(1);
Application.id = user.getId();
Application.username = user.getUsername();
Application.password = user.getPassword();
JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + status.getVersion(),"用户登录",JOptionPane.INFORMATION_MESSAGE);
Application.mainFrame = new MainFrame("");
Application.loginFrame.dispose();
}else {
Application.loginFrame.setVisible(false);
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入","用户登录",JOptionPane.ERROR_MESSAGE);
Application.loginFrame.setVisible(true);
txtUsername.selectAll();
txtPassword.selectAll();
txtUsername.requestFocus();
}
}
public static void main(String[] args) {
Application.loginFrame = new LoginFrame("用户登录");
}
}
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.bean.User;
import net.cqf.student.service.UserService;
import net.cqf.student.service.impl.UserServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
public class RegisterFrame extends JFrame {
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblPhone;
private JTextField txtUsername;
private JTextField txtPhone;
private JPasswordField txtPassword;
private JButton btnSubmit;
private JButton btnCancel;
private JButton btnLogin;
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private String username;
private String password;
private String phone;
private Date registerTime;
public RegisterFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
//创建组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
lblUsername = new JLabel("用户名: ");
lblPassword = new JLabel("密 码: ");
lblPhone = new JLabel("电 话: ");
txtUsername = new JTextField("",15);
txtPassword = new JPasswordField("",15);
txtPhone = new JTextField("",15);
btnSubmit = new JButton("提交[S]");
btnCancel = new JButton("取消[C]");
btnLogin = new JButton("登录[L]");
//将控件添加到四个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(lblPhone);
panel3.add(txtPhone);
panel4.add(btnSubmit);
panel4.add(btnCancel);
panel4.add(btnLogin);
//设置大面板布局,将四个小面板添加到大面板
panel.setLayout(new GridLayout(4, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
//设置窗口大小
setSize(250,200);
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口标题
setTitle("用户注册");
//设置窗口可见
setVisible(true);
//注册窗口监听器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//卸载当前窗口---注册可见
dispose();
//让登录窗口可见
Application.loginFrame.setVisible(true);
}
});
//[提交]按钮单击事件处理
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
register();
}
});
//[提交]按钮按键事件处理
btnSubmit.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
register();
}
}
});
//[取消]按钮单击事件处理
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//显示登陆窗口
Application.loginFrame.setVisible(true);
//卸载当前窗口
dispose();
}
});
//[登陆]按钮单击事件处理
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//显示登陆窗口
Application.loginFrame.setVisible(true);
//卸载当前窗口
dispose();
}
});
//[用户名]文本框按键事件处理
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
//[密码]文本框按键事件处理
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtPhone.requestFocus();
}
}
});
//[电话]文本框按键事件处理
txtPhone.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
btnSubmit.requestFocus();
}
}
});
}
private void register() {
//获取用户名
username = txtUsername.getText().trim();
//获取密码
password = new String(txtPassword.getPassword());
//获取电话
phone = txtPhone.getText().trim();
//定义当前时间为注册时间
registerTime = new Date(System.currentTimeMillis());
//对用户名进行非空校验
if(username.equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!","学生信息管理系统",JOptionPane.WARNING_MESSAGE);
txtUsername.requestFocus();
return;
}
//对密码进行非空校验
if (txtPassword.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!","学生信息管理系统",JOptionPane.WARNING_MESSAGE);
txtPassword.requestFocus();
return;
}
//定义用户服务对象
UserService userService = new UserServiceImpl();
//创建用户
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setTelephone(phone);
user.setRegisterTime(registerTime);
//添加用户
int count = userService.addUser(user);
//判断是否添加成功
if (count > 0) {
setVisible(false);
JOptionPane.showMessageDialog(null, "恭喜!注册成功!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
setVisible(true);
}else {
JOptionPane.showMessageDialog(null, "遗憾!注册失败!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
Application.registerFrame = new RegisterFrame("用户注册");
}
}
```java
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.service.StatusService;
import net.cqf.student.bean.Status;
import net.cqf.student.service.impl.StatusServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
/**
* 功能:主界面窗口
* 通过菜单系统调用各功能模块
* 作者:陈秋帆
* 日期:2020年07月13日
*/
public class MainFrame extends JFrame {
private JMenuBar mnbMain; // 主菜单栏
private JMenu mnuSet; // 设置菜单
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
private JMenu mnuOperate; // 操作菜单
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
private JMenu mnuDelStu; // 删除菜单
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
private JMenu mnuFind; // 查询菜单
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
private JMenu mnuCount; // 统计菜单
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
private JMenu mnuHelp; // 帮助菜单
private JMenuItem mniHelp;
private JMenuItem mniAbout;
private JPanel panel; // 面板
private JPanel pnlCenter;
private JPanel pnlSouth;
private JLabel lblStatusBar; // 状态栏标签
private JLabel lblBackground; // 背景标签
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
private JToolBar toolbar; // 工具栏
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
private Status status; // 状态对象
private StatusService statusService; // 状态服务对象
public MainFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化图形用户界面
*/
private void initGUI() {
// 创建主菜单
mnbMain = new JMenuBar();
// 创建【设置】菜单及其菜单项
mnuSet = new JMenu("系统设置[S]");
mnuSet.setMnemonic(KeyEvent.VK_S);
mniSetCollegeInfo = new JMenuItem("学校信息");
mniSetStatusBar = new JMenuItem("状态栏信息");
mniChangePassword = new JMenuItem("修改密码");
mniExit = new JMenuItem("退出系统");
// 创建【操作】菜单及其菜单项
mnuOperate = new JMenu("数据操作[O]");
mnuOperate.setMnemonic(KeyEvent.VK_O);
mniAddStudent = new JMenuItem("增加学生表记录");
mnuDelStu = new JMenu("删除学生表记录");
mniEditStudent = new JMenuItem("编辑学生表记录");
mniBrowseStudent = new JMenuItem("浏览学生表记录");
// 创建【删除学生表记录】的子菜单
mniDelStudentById = new JMenuItem("按学号删除");
mniDelStudentsByClass = new JMenuItem("按班级删除");
mniDelStudentsByDepartment = new JMenuItem("按系部删除");
// 创建【查询】菜单及其菜单项
mnuFind = new JMenu("查询学生[Q]");
mnuFind.setMnemonic(KeyEvent.VK_Q);
mniFindStudentById = new JMenuItem("按学号查询");
mniFindStudentsByName = new JMenuItem("按姓名查询");
mniFindStudentsByClass = new JMenuItem("按班级查询");
mniFindStudentsByDepartment = new JMenuItem("按系部查询");
// 创建【统计】菜单及其菜单项
mnuCount = new JMenu("人数统计[C]");
mnuCount.setMnemonic(KeyEvent.VK_C);
mniCountStudentsBySex = new JMenuItem("按性别统计");
mniCountStudentsByClass = new JMenuItem("按班级统计");
mniCountStudentsByDepartment = new JMenuItem("按系部统计");
// 创建【帮助】菜单及其菜单项
mnuHelp = new JMenu("帮助[H]");
mnuHelp.setMnemonic(KeyEvent.VK_H);
mniHelp = new JMenuItem("帮助");
mniAbout = new JMenuItem("关于");
// 创建图标对象
imgCollege = new ImageIcon("images/college.png");
imgPassword = new ImageIcon("images/password.png");
imgQuery = new ImageIcon("images/query.png");
imgBrowse = new ImageIcon("images/browse.png");
imgCount = new ImageIcon("images/count.png");
imgExit = new ImageIcon("images/exit.png");
// 创建工具栏
toolbar = new JToolBar();
btnSetCollege = new JButton("设置学校", imgCollege);
btnSetCollege.setToolTipText("设置学校信息");
btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
btnChangePassword = new JButton("修改密码", imgPassword);
btnChangePassword.setToolTipText("修改用户密码");
btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
btnBrowseStudent = new JButton("浏览学生", imgBrowse);
btnBrowseStudent.setToolTipText("浏览学生记录");
btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
btnFindStudentById = new JButton("查询学生", imgQuery);
btnFindStudentById.setToolTipText("按学号查询学生记录");
btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
btnCountByDepartment = new JButton("统计人数", imgCount);
btnCountByDepartment.setToolTipText("按系部统计学生人数");
btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
btnExit = new JButton("退出系统", imgExit);
btnExit.setToolTipText("退出系统");
btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
toolbar.add(btnSetCollege);
toolbar.add(btnChangePassword);
toolbar.add(btnBrowseStudent);
toolbar.add(btnFindStudentById);
toolbar.add(btnCountByDepartment);
toolbar.add(btnExit);
// 创建面板
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));
// 创建背景图片
imgBackground = new ImageIcon("images/background.jpg");
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblStatusBar = new JLabel();
// 设置菜单栏
setJMenuBar(mnbMain);
// 添加【设置】菜单
mnbMain.add(mnuSet);
mnuSet.add(mniSetCollegeInfo);
mnuSet.add(mniSetStatusBar);
mnuSet.add(mniChangePassword);
mnuSet.addSeparator();
mnuSet.add(mniExit);
// 添加【删除学生表记录】菜单
mnuDelStu.add(mniDelStudentById);
mnuDelStu.add(mniDelStudentsByClass);
mnuDelStu.add(mniDelStudentsByDepartment);
// 添加【操作】菜单
mnbMain.add(mnuOperate);
mnuOperate.add(mniAddStudent);
mnuOperate.add(mniEditStudent);
mnuOperate.add(mnuDelStu);
mnuOperate.add(mniBrowseStudent);
// 添加【查询】菜单
mnbMain.add(mnuFind);
mnuFind.add(mniFindStudentById);
mnuFind.add(mniFindStudentsByName);
mnuFind.add(mniFindStudentsByClass);
mnuFind.add(mniFindStudentsByDepartment);
// 添加【统计】菜单
mnbMain.add(mnuCount);
mnuCount.add(mniCountStudentsBySex);
mnuCount.add(mniCountStudentsByClass);
mnuCount.add(mniCountStudentsByDepartment);
// 添加【帮助】菜单
mnbMain.add(mnuHelp);
mnuHelp.add(mniHelp);
mnuHelp.add(mniAbout);
// 添加面板
panel.setLayout(new BorderLayout());
panel.add(toolbar, "North");
panel.add(pnlCenter, "Center");
panel.add(pnlSouth, "South");
pnlCenter.add(lblBackground);
pnlSouth.add(lblStatusBar);
// 非管理员不能设置状态栏
if (!Application.username.equals("admin")) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}
// 设置状态栏信息
setStatusBar();
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态对象
status = statusService.findStatusById(1);
// 设置窗口尺寸
setSize(800, 640);
// 设置窗口可见
setVisible(true);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("学生信息管理系统" + status.getVersion());
// 注册窗口监听器,继承窗口适配器,编写事件处理方法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【设置状态栏信息】菜单项单击事件
mniSetStatusBar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetStatusBarFrame("");
}
});
// 【修改密码】菜单项单击事件
mniChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});
// 【退出系统】菜单项单击事件
mniExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
// 查询菜单
// 【按学号查询】菜单项单击事件
mniFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});
// 【按姓名查询】菜单项单击事件
mniFindStudentsByName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByNameFrame("");
}
});
// 【按班级查询】菜单项单击事件
mniFindStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByClassFrame("");
}
});
// 【按系部查询】菜单项单击事件
mniFindStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByDepartmentFrame("");
}
});
// 统计菜单
// 【按性别统计人数】菜单项单击事件
mniCountStudentsBySex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsBySexFrame("");
}
});
// 【按班级统计人数】菜单项单击事件
mniCountStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【按系部统计人数】菜单项单击事件
mniCountStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【增加学生记录】菜单项单击事件
mniAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddStudentFrame("");
}
});
// 【按学号删除学生记录】菜单项单击事件
mniDelStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentByIdFrame("");
}
});
// 【按班级删除学生记录】菜单项单击事件
mniDelStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByClassFrame("");
}
});
// 【按系部删除学生记录】菜单项单击事件
mniDelStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByDepartmentFrame("");
}
});
// 【编辑学生记录】菜单项单击事件
mniEditStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new EditStudentFrame("");
}
});
// 【浏览学生记录】菜单项单击事件
mniBrowseStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});
// 【帮助】菜单单击事件
mniHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
}
}
});
// 【关于】菜单单击事件
mniAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()
+ "\n电子邮箱:" + status.getEmail(),
"学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
}
});
// 工具栏按钮单击事件
// 【设置学校信息】按钮
btnSetCollege.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【修改密码】按钮
btnChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});
// 【浏览】按钮
btnBrowseStudent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});
// 【查询】按钮
btnFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});
// 【统计】按钮
btnCountByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
* 退出系统(询问用户是否要退出)
*/
private void exitSystem() {
int choice = JOptionPane.showConfirmDialog(this,
"您是否要退出系统?","学生信息管理系统", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
} else {
// 卸载当前窗口
dispose();
// 重新显示主窗口
Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());
}
}
/**
* 设置状态栏信息
*/
public void setStatusBar() {
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态栏对象
status = statusService.findStatusById(1);
// 设置状态栏标签
lblStatusBar.setText(
status.getCollege() + "学生信息管理系统" + status.getVersion() + " 作者:" + status.getAuthor() + " 地址:"
+ status.getAddress() + " 电话:" + status.getTelephone() + " 邮箱:" + status.getEmail());
}
public static void main(String[] args) {
Application.mainFrame = new MainFrame("");
}
}
***
## ~~创建窗口界面类~~
***
1、创建主界面窗口MainFrame
目前只是创建一个可以显示出来和关闭的主界面窗口,等到将各个功能模块窗口界面都完成了,我们会修改代码,给出主窗口界面的最终版。
MainFrame继承JFrame类
添加构造方法(有一个标题参数的构造方法)
在类的声明部分声明两个变量:状态服务变量与状态变量
创建初始化图形用户界面方法initGUI()
在构造方法里调用初始化图形用户界面方法initGUI()
编写主方法代码,实例化主窗口,设置空标题,保存到Application的变量mainFrame里
如果希望单击关闭按钮后,弹出一个询问框,询问用户是否要退出系统,那么就不用采用上述方法,而应该采用注册窗口监听器的方法,通过编写事件处理方法来弹出询问对话框。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200715154903839.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW45OTk5OTlmYW4=,size_16,color_FFFFFF,t_70)
```java
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.service.StatusService;
import net.cqf.student.bean.Status;
import net.cqf.student.service.impl.StatusServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
/**
* 功能:主界面窗口
* 通过菜单系统调用各功能模块
* 作者:陈秋帆
* 日期:2020年07月13日
*/
public class MainFrame extends JFrame {
private JMenuBar mnbMain; // 主菜单栏
private JMenu mnuSet; // 设置菜单
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
private JMenu mnuOperate; // 操作菜单
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
private JMenu mnuDelStu; // 删除菜单
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
private JMenu mnuFind; // 查询菜单
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
private JMenu mnuCount; // 统计菜单
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
private JMenu mnuHelp; // 帮助菜单
private JMenuItem mniHelp;
private JMenuItem mniAbout;
private JPanel panel; // 面板
private JPanel pnlCenter;
private JPanel pnlSouth;
private JLabel lblStatusBar; // 状态栏标签
private JLabel lblBackground; // 背景标签
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
private JToolBar toolbar; // 工具栏
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
private Status status; // 状态对象
private StatusService statusService; // 状态服务对象
public MainFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化图形用户界面
*/
private void initGUI() {
// 创建主菜单
mnbMain = new JMenuBar();
// 创建【设置】菜单及其菜单项
mnuSet = new JMenu("系统设置[S]");
mnuSet.setMnemonic(KeyEvent.VK_S);
mniSetCollegeInfo = new JMenuItem("学校信息");
mniSetStatusBar = new JMenuItem("状态栏信息");
mniChangePassword = new JMenuItem("修改密码");
mniExit = new JMenuItem("退出系统");
// 创建【操作】菜单及其菜单项
mnuOperate = new JMenu("数据操作[O]");
mnuOperate.setMnemonic(KeyEvent.VK_O);
mniAddStudent = new JMenuItem("增加学生表记录");
mnuDelStu = new JMenu("删除学生表记录");
mniEditStudent = new JMenuItem("编辑学生表记录");
mniBrowseStudent = new JMenuItem("浏览学生表记录");
// 创建【删除学生表记录】的子菜单
mniDelStudentById = new JMenuItem("按学号删除");
mniDelStudentsByClass = new JMenuItem("按班级删除");
mniDelStudentsByDepartment = new JMenuItem("按系部删除");
// 创建【查询】菜单及其菜单项
mnuFind = new JMenu("查询学生[Q]");
mnuFind.setMnemonic(KeyEvent.VK_Q);
mniFindStudentById = new JMenuItem("按学号查询");
mniFindStudentsByName = new JMenuItem("按姓名查询");
mniFindStudentsByClass = new JMenuItem("按班级查询");
mniFindStudentsByDepartment = new JMenuItem("按系部查询");
// 创建【统计】菜单及其菜单项
mnuCount = new JMenu("人数统计[C]");
mnuCount.setMnemonic(KeyEvent.VK_C);
mniCountStudentsBySex = new JMenuItem("按性别统计");
mniCountStudentsByClass = new JMenuItem("按班级统计");
mniCountStudentsByDepartment = new JMenuItem("按系部统计");
// 创建【帮助】菜单及其菜单项
mnuHelp = new JMenu("帮助[H]");
mnuHelp.setMnemonic(KeyEvent.VK_H);
mniHelp = new JMenuItem("帮助");
mniAbout = new JMenuItem("关于");
// 创建图标对象
imgCollege = new ImageIcon("images/college.png");
imgPassword = new ImageIcon("images/password.png");
imgQuery = new ImageIcon("images/query.png");
imgBrowse = new ImageIcon("images/browse.png");
imgCount = new ImageIcon("images/count.png");
imgExit = new ImageIcon("images/exit.png");
// 创建工具栏
toolbar = new JToolBar();
btnSetCollege = new JButton("设置学校", imgCollege);
btnSetCollege.setToolTipText("设置学校信息");
btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
btnChangePassword = new JButton("修改密码", imgPassword);
btnChangePassword.setToolTipText("修改用户密码");
btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
btnBrowseStudent = new JButton("浏览学生", imgBrowse);
btnBrowseStudent.setToolTipText("浏览学生记录");
btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
btnFindStudentById = new JButton("查询学生", imgQuery);
btnFindStudentById.setToolTipText("按学号查询学生记录");
btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
btnCountByDepartment = new JButton("统计人数", imgCount);
btnCountByDepartment.setToolTipText("按系部统计学生人数");
btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
btnExit = new JButton("退出系统", imgExit);
btnExit.setToolTipText("退出系统");
btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
toolbar.add(btnSetCollege);
toolbar.add(btnChangePassword);
toolbar.add(btnBrowseStudent);
toolbar.add(btnFindStudentById);
toolbar.add(btnCountByDepartment);
toolbar.add(btnExit);
// 创建面板
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));
// 创建背景图片
imgBackground = new ImageIcon("images/background.jpg");
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblStatusBar = new JLabel();
// 设置菜单栏
setJMenuBar(mnbMain);
// 添加【设置】菜单
mnbMain.add(mnuSet);
mnuSet.add(mniSetCollegeInfo);
mnuSet.add(mniSetStatusBar);
mnuSet.add(mniChangePassword);
mnuSet.addSeparator();
mnuSet.add(mniExit);
// 添加【删除学生表记录】菜单
mnuDelStu.add(mniDelStudentById);
mnuDelStu.add(mniDelStudentsByClass);
mnuDelStu.add(mniDelStudentsByDepartment);
// 添加【操作】菜单
mnbMain.add(mnuOperate);
mnuOperate.add(mniAddStudent);
mnuOperate.add(mniEditStudent);
mnuOperate.add(mnuDelStu);
mnuOperate.add(mniBrowseStudent);
// 添加【查询】菜单
mnbMain.add(mnuFind);
mnuFind.add(mniFindStudentById);
mnuFind.add(mniFindStudentsByName);
mnuFind.add(mniFindStudentsByClass);
mnuFind.add(mniFindStudentsByDepartment);
// 添加【统计】菜单
mnbMain.add(mnuCount);
mnuCount.add(mniCountStudentsBySex);
mnuCount.add(mniCountStudentsByClass);
mnuCount.add(mniCountStudentsByDepartment);
// 添加【帮助】菜单
mnbMain.add(mnuHelp);
mnuHelp.add(mniHelp);
mnuHelp.add(mniAbout);
// 添加面板
panel.setLayout(new BorderLayout());
panel.add(toolbar, "North");
panel.add(pnlCenter, "Center");
panel.add(pnlSouth, "South");
pnlCenter.add(lblBackground);
pnlSouth.add(lblStatusBar);
// 非管理员不能设置状态栏
if (!Application.username.equals("admin")) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}
// 设置状态栏信息
setStatusBar();
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态对象
status = statusService.findStatusById(1);
// 设置窗口尺寸
setSize(800, 640);
// 设置窗口可见
setVisible(true);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("学生信息管理系统" + status.getVersion());
// 注册窗口监听器,继承窗口适配器,编写事件处理方法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【设置状态栏信息】菜单项单击事件
mniSetStatusBar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetStatusBarFrame("");
}
});
// 【修改密码】菜单项单击事件
mniChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});
// 【退出系统】菜单项单击事件
mniExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
// 查询菜单
// 【按学号查询】菜单项单击事件
mniFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});
// 【按姓名查询】菜单项单击事件
mniFindStudentsByName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByNameFrame("");
}
});
// 【按班级查询】菜单项单击事件
mniFindStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByClassFrame("");
}
});
// 【按系部查询】菜单项单击事件
mniFindStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByDepartmentFrame("");
}
});
// 统计菜单
// 【按性别统计人数】菜单项单击事件
mniCountStudentsBySex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsBySexFrame("");
}
});
// 【按班级统计人数】菜单项单击事件
mniCountStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【按系部统计人数】菜单项单击事件
mniCountStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【增加学生记录】菜单项单击事件
mniAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddStudentFrame("");
}
});
// 【按学号删除学生记录】菜单项单击事件
mniDelStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentByIdFrame("");
}
});
// 【按班级删除学生记录】菜单项单击事件
mniDelStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByClassFrame("");
}
});
// 【按系部删除学生记录】菜单项单击事件
mniDelStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByDepartmentFrame("");
}
});
// 【编辑学生记录】菜单项单击事件
mniEditStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new EditStudentFrame("");
}
});
// 【浏览学生记录】菜单项单击事件
mniBrowseStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});
// 【帮助】菜单单击事件
mniHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
}
}
});
// 【关于】菜单单击事件
mniAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()
+ "\n电子邮箱:" + status.getEmail(),
"学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
}
});
// 工具栏按钮单击事件
// 【设置学校信息】按钮
btnSetCollege.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【修改密码】按钮
btnChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});
// 【浏览】按钮
btnBrowseStudent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});
// 【查询】按钮
btnFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});
// 【统计】按钮
btnCountByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
* 退出系统(询问用户是否要退出)
*/
private void exitSystem() {
int choice = JOptionPane.showConfirmDialog(this,
"您是否要退出系统?","学生信息管理系统", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
} else {
// 卸载当前窗口
dispose();
// 重新显示主窗口
Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());
}
}
/**
* 设置状态栏信息
*/
public void setStatusBar() {
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态栏对象
status = statusService.findStatusById(1);
// 设置状态栏标签
lblStatusBar.setText(
status.getCollege() + "学生信息管理系统" + status.getVersion() + " 作者:" + status.getAuthor() + " 地址:"
+ status.getAddress() + " 电话:" + status.getTelephone() + " 邮箱:" + status.getEmail());
}
public static void main(String[] args) {
Application.mainFrame = new MainFrame("");
}
}
2、创建用户登录窗口LoginFrame
继承JFrame,创建LoginFrame
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.bean.User;
import net.cqf.student.service.UserService;
import net.cqf.student.service.impl.UserServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
public class RegisterFrame extends JFrame {
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblPhone;
private JTextField txtUsername;
private JTextField txtPhone;
private JPasswordField txtPassword;
private JButton btnSubmit;
private JButton btnCancel;
private JButton btnLogin;
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private String username;
private String password;
private String phone;
private Date registerTime;
public RegisterFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
//创建组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
lblUsername = new JLabel("用户名: ");
lblPassword = new JLabel("密 码: ");
lblPhone = new JLabel("电 话: ");
txtUsername = new JTextField("",15);
txtPassword = new JPasswordField("",15);
txtPhone = new JTextField("",15);
btnSubmit = new JButton("提交[S]");
btnCancel = new JButton("取消[C]");
btnLogin = new JButton("登录[L]");
//将控件添加到四个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(lblPhone);
panel3.add(txtPhone);
panel4.add(btnSubmit);
panel4.add(btnCancel);
panel4.add(btnLogin);
//设置大面板布局,将四个小面板添加到大面板
panel.setLayout(new GridLayout(4, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
//设置窗口大小
setSize(250,200);
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口标题
setTitle("用户注册");
//设置窗口可见
setVisible(true);
//注册窗口监听器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//卸载当前窗口---注册可见
dispose();
//让登录窗口可见
Application.loginFrame.setVisible(true);
}
});
//[提交]按钮单击事件处理
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
register();
}
});
//[提交]按钮按键事件处理
btnSubmit.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
register();
}
}
});
//[取消]按钮单击事件处理
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//显示登陆窗口
Application.loginFrame.setVisible(true);
//卸载当前窗口
dispose();
}
});
//[登陆]按钮单击事件处理
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//显示登陆窗口
Application.loginFrame.setVisible(true);
//卸载当前窗口
dispose();
}
});
//[用户名]文本框按键事件处理
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
//[密码]文本框按键事件处理
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtPhone.requestFocus();
}
}
});
//[电话]文本框按键事件处理
txtPhone.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
btnSubmit.requestFocus();
}
}
});
}
private void register() {
//获取用户名
username = txtUsername.getText().trim();
//获取密码
password = new String(txtPassword.getPassword());
//获取电话
phone = txtPhone.getText().trim();
//定义当前时间为注册时间
registerTime = new Date(System.currentTimeMillis());
//对用户名进行非空校验
if(username.equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!","学生信息管理系统",JOptionPane.WARNING_MESSAGE);
txtUsername.requestFocus();
return;
}
//对密码进行非空校验
if (txtPassword.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!","学生信息管理系统",JOptionPane.WARNING_MESSAGE);
txtPassword.requestFocus();
return;
}
//定义用户服务对象
UserService userService = new UserServiceImpl();
//创建用户
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setTelephone(phone);
user.setRegisterTime(registerTime);
//添加用户
int count = userService.addUser(user);
//判断是否添加成功
if (count > 0) {
setVisible(false);
JOptionPane.showMessageDialog(null, "恭喜!注册成功!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
setVisible(true);
}else {
JOptionPane.showMessageDialog(null, "遗憾!注册失败!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
Application.registerFrame = new RegisterFrame("用户注册");
}
}
3、创建用户注册窗口RegisterFrame
在创建用户登录窗口过程中,已经创建了一个可以显示与关闭的空白注册窗口
package net.cqf.student.gui;
import net.cqf.student.app.Application;
import net.cqf.student.bean.Status;
import net.cqf.student.bean.User;
import net.cqf.student.service.StatusService;
import net.cqf.student.service.UserService;
import net.cqf.student.service.impl.StatusServiceImpl;
import net.cqf.student.service.impl.UserServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginFrame extends JFrame{
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnRegister;
private JPanel panel,panel1,panel2,panel3;
public LoginFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定");
btnCancel = new JButton("取消");
btnRegister = new JButton("注册");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
panel.setLayout(new GridLayout(3,1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnRegister.setMnemonic(KeyEvent.VK_R);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login();
}
});
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtUsername.requestFocus();
}
});
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Application.loginFrame.setVisible(false);
Application.registerFrame = new RegisterFrame("用户注册");
}
});
}
private void login() {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserServiceImpl();
User user = userService.login(username, password);
if (user != null) {
Application.loginFrame.setVisible(false);
StatusService statusService = new StatusServiceImpl();
Status status = statusService.findStatusById(1);
Application.id = user.getId();
Application.username = user.getUsername();
Application.password = user.getPassword();
JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + status.getVersion(),"用户登录",JOptionPane.INFORMATION_MESSAGE);
Application.mainFrame = new MainFrame("");
Application.loginFrame.dispose();
}else {
Application.loginFrame.setVisible(false);
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入","用户登录",JOptionPane.ERROR_MESSAGE);
Application.loginFrame.setVisible(true);
txtUsername.selectAll();
txtPassword.selectAll();
txtUsername.requestFocus();
}
}
public static void main(String[] args) {
Application.loginFrame = new LoginFrame("用户登录");
}
}
4、创建设置学校信息窗口SetCollegeInfoFrame
在net.cqf.student.gui包里创建SetCollegeInfoFrame类
package net.cqf.student.gui;
import net.cqf.student.bean.College;
import net.cqf.student.service.CollegeService;
import net.cqf.student.service.impl.CollegeServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 功能:设置学校信息
* 作者:陈秋帆
* 日期:2020年07月14日
*/
public class SetCollegeInfoFrame extends JFrame {
JPanel panel;
JPanel pnlNorth;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JPanel panel5;
JPanel pnlCenter;
JPanel pnlSouth;
JLabel lblName;
JLabel lblPresident;
JLabel lblStartTime;
JLabel lblTelephone;
JLabel lblEmail;
JLabel lblAddress;
JLabel lblProfile;
JButton btnSave;
JButton btnExit;
JTextField txtName;
JTextField txtPresident;
JTextField txtStartTime;
JTextField txtTelephone;
JTextField txtEMail;
JTextField txtAddress;
JTextArea txtProfile;
JScrollPane scrollPane;
private CollegeService collegeService;
private College college;
private int id = 1;
public SetCollegeInfoFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
// 创建组件(面板与控件)
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
lblName = new JLabel("学校名称:");
lblPresident = new JLabel("校 长:");
lblStartTime = new JLabel("建校时间:");
lblTelephone = new JLabel("联系电话:");
lblEmail = new JLabel("电子邮箱:");
lblAddress = new JLabel("通讯地址:");
lblProfile = new JLabel("学校简介:");
txtName = new JTextField(30);
txtPresident = new JTextField(11);
txtStartTime = new JTextField(12);
txtTelephone = new JTextField(11);
txtEMail = new JTextField(12);
txtAddress = new JTextField(30);
txtProfile = new JTextArea(5, 37);
txtProfile.setLineWrap(true);// 让文本区自动换行
scrollPane = new JScrollPane(txtProfile, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
btnSave = new JButton("保存[S]");
btnSave.setMnemonic(KeyEvent.VK_S);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 设置大面板布局,将北面板、中面板与南面板添加到大面板
panel.setLayout(new BorderLayout());
panel.add(pnlNorth, "North");
panel.add(pnlCenter, "Center");
panel.add(pnlSouth, "South");
// 设置北面板布局,添加五个小面板
pnlNorth.setLayout(new GridLayout(5, 1));
pnlNorth.add(panel1);
pnlNorth.add(panel2);
pnlNorth.add(panel3);
pnlNorth.add(panel4);
pnlNorth.add(panel5);
// 将控件依次添加到五个小面板
panel1.add(lblName);
panel1.add(txtName);
panel2.add(lblPresident);
panel2.add(txtPresident);
panel2.add(lblStartTime);
panel2.add(txtStartTime);
panel3.add(lblTelephone);
panel3.add(txtTelephone);
panel3.add(lblEmail);
panel3.add(txtEMail);
panel4.add(lblAddress);
panel4.add(txtAddress);
panel5.add(lblProfile);
// 将滚动面板添加到中面板
pnlCenter.add(scrollPane);
// 将两个按钮添加到南面板
pnlSouth.add(btnSave);
pnlSouth.add(btnExit);
// 创建学校服务对象
collegeService = new CollegeServiceImpl();
// 获取学校对象
college = collegeService.findCollegeById(id);
// 利用学校对象属性值设置相应控件的内容
txtName.setText(college.getName());
txtPresident.setText(college.getPresident());
txtStartTime.setText(college.getStartTime().toString());
txtTelephone.setText(college.getTelephone());
txtEMail.setText(college.getEmail());
txtAddress.setText(college.getAddress());
txtProfile.setText(college.getProfile());
// 设置窗口大小
setSize(450, 350);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【关闭】按钮单击事件处理
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 关闭窗口
dispose();
}
});
// 【保存】按钮单击事件处理
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
try {
// 修改学校对象属性
college.setName(txtName.getText());
college.setPresident(txtPresident.getText());
college.setStartTime(new Date(
new SimpleDateFormat("yyyy-MM-dd")
.parse(txtStartTime.getText().toString()).getTime()));
college.setTelephone(txtTelephone.getText());
college.setEmail(txtEMail.getText());
college.setAddress(txtAddress.getText());
college.setProfile(txtProfile.getText());
// 更新学校信息
collegeService.updateCollege(college);
// 提示用户更新成功
JOptionPane.showMessageDialog(null, "更新数据成功!");
} catch (ParseException e) {
// 提示用户更新失败
JOptionPane.showMessageDialog(null, "更新数据失败!");
}
}
});
}
public static void main(String[] args) {
new SetCollegeInfoFrame("设置学校信息");
}
}