Java+GUI+MySQL 学生管理系统
学生管理系统
一、整体列表总览
- 登录文件——
LoginFrame
package net.zwh.p03.t09.gui;
import net.zwh.p03.t09.bean.User;
import net.zwh.p03.t09.service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
/**
* 功能:用户登录窗口
* 作者:曾文浩
* 日期:2022年06月09日
*/
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 btnSign;
private JPanel panel, panel1, panel2, panel3, panel4;
/**
* 有参构造方法
*
* @param title
*/
public LoginFrame(String title) {
super(title);
initGUI(); // 调用初始化图形用户界面方法
eventsHandling(); // 调用事件处理方法
}
/**
* 事件处理
*/
private void eventsHandling() {
// 给【确定】按钮注册监听器
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名和密码
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
// 创建用户服务对象
UserService userService = new UserService();
// 调用服务对象的登录方法
User user = userService.login(username, password);
// 判断用户登录是否成功
if (user != null) {
// 弹出消息提示窗口
try {
new Main();
JOptionPane.showMessageDialog(null, "恭喜【"+ username + "】登录成功~");
} catch (IOException ioException) {
System.err.println("失败~");
}
dispose();
} else {
// 弹出消息提示窗口
JOptionPane.showMessageDialog(null, "【"+ username + "】登录失败~\n用户名或密码错误~");
// 情况两个文本框
txtUsername.setText("");
txtPassword.setText("");
// 让姓名文本框获取焦点
txtUsername.requestFocus();
}
}
});
//给取消按钮注册监听器
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnSign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Sign();
}
});
}
/**
* 初始化图形用户界面方法
*/
private void initGUI() {
// 实例化组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnSign = new JButton("注册[I]");
// 将控件添加到三个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel4.add(btnSign);
// 设置主面板为三行一列的网格布局
panel.setLayout(new GridLayout(4, 1));
// 将三个小面板依次添加到主面板
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
// 设置按钮热键字母
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnSign.setMnemonic(KeyEvent.VK_I);
// 设置密码框回显字符
txtPassword.setEchoChar('*');
// 设置窗口大小
setSize(700, 500);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
- 主文件——
Main
package net.zwh.p03.t09.gui;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class Main extends JFrame implements ActionListener{
public static void main(String[] args) throws IOException {
new Main();
final int WIDTH=800;//设置窗口的宽
final int HEIGHT=500;//设置窗口的高
}
//面板控件
private JLabel queryLab = null;
private JTextField queryTxt = null;
private JButton queryBtn = null;
private JButton allBtn = null;
private JTable resultTb = null;
private JScrollPane jsp = null;
private JButton addBtn = null;
private JButton deleteBtn = null;
private JButton updateBtn = null;
private JPanel top = null;
private JPanel bottom = null;
private Model sm = null;
//构造函数
public Main() throws IOException {
super("学生管理系统");
//查询栏
queryLab = new JLabel("请输入姓名:");
queryTxt = new JTextField(10);
queryBtn = new JButton("查询");
allBtn = new JButton("全部");
//添加查询栏监听
queryBtn.addActionListener(this);
queryBtn.setActionCommand("query");
allBtn.addActionListener(this);
allBtn.setActionCommand("all");
//增删改栏
addBtn = new JButton("添加");
deleteBtn = new JButton("删除");
updateBtn = new JButton("修改");
//添加增删改栏监听
addBtn.addActionListener(this);
addBtn.setActionCommand("add");
deleteBtn.addActionListener(this);
deleteBtn.setActionCommand("delete");
updateBtn.addActionListener(this);
updateBtn.setActionCommand("update");
//创建窗口整体布局
//顶层查询栏
top = new JPanel();
top.add(queryLab);
top.add(queryTxt);
top.add(queryBtn);
top.add(allBtn);
//底层增删改栏
bottom = new JPanel();
bottom.add(addBtn);
bottom.add(deleteBtn);
bottom.add(updateBtn);
//中间层显示栏
sm = new Model();
String sql = "select * from stu";
sm.queryStu(sql, null);
resultTb = new JTable(sm);
jsp = new JScrollPane(resultTb);
//构建整体布局
this.add(top,BorderLayout.NORTH);
this.add(jsp,BorderLayout.CENTER);
this.add(bottom,BorderLayout.SOUTH);
//设置窗口属性
this.setBounds(500,250,500,300);
// this.setIconImage(ImageIO.read(new File("D:\\yuandaima\\shishuo\\src\\studentManage\\img\\登录.png")));
this.setVisible(true);
this.setResizable(false);
}
//监听
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getActionCommand().equals("query")) {
//查询
//获取输入学生的姓名
String name = queryTxt.getText().trim();
if(name.length() != 0) {
//姓名输入有效时,执行查询
//定义参数
String sql = "select * from stu where stuName=?";
String []paras = {name};
//更新模型
jtableUpdate(sql, paras);
} else {
//姓名为空时,设置提醒
JOptionPane.showMessageDialog(this, "姓名输入不能为空");
}
} else if(e.getActionCommand().equals("add")) {
//添加
new Add(this, "添加学生信息", true);
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("all")) {
//全部显示
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("delete")) {
//删除
//获取选择行号
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
//获取学生ID号
String stuId = (String)sm.getValueAt(rowNum, 0);
//删除学生
String sql = "delete from stu where stuId=?";
String []paras = {stuId};
Model tmp = new Model();
tmp.cudStu(sql, paras);
//更新模型
sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("update")) {
//修改
//获取选择行号
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
new Set(this, "修改学生信息", true, sm, rowNum);
String sql = "select * from stu";
jtableUpdate(sql, null);
}
}
//更新JTable内数据
public void jtableUpdate(String sql, String[] paras)
{
//创建模型
sm = new Model();
sm.queryStu(sql, paras);
//更新显示
resultTb.setModel(sm);
}
}
二、效果预览
-
登录界面
-
登录成功
-
添加学生信息
-
查询
三、所有代码下载链接
链接: ☞代码所在 提取码: ntie