实训目的:综合运用J2SE有关知识开发【学生信息管理系统】。主要涉及程序控制结构、面向对象编程、图形用户界面、Java数据库应用、MySQL数据库这几个方面的内容。通过本项目的开发,了解系统开发的一般流程,初步了解MVC模式与单元测试,巩固所学的Java理论知识,提高Java数据库应用编程的基本技能。
项目说明:本实训案例教学,利用Java SE基本知识是开发【学生信息管理系统】。本系统包括登录注册、系统设置(设置学校信息、设置状态栏信息、修改用户密码)、数据操作(增加学生记录、编辑学生记录、删除学生记录、浏览学生记录)、查询学生(按学号查询、按姓名查询、按班级查询、按系部查询)、人数统计(按性别统计人数、按班级统计人数、按系部统计人数)等功能。
7)浏览学生窗口BrowseStudentsFrame
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import org.junit.jupiter.api.Test;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
public class BrowseStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/**
* 声明控制
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
/**
* 当前行记录
*/
private int currentRow;
/**
* 学生列表
*/
private List student;
/**
* 创建学生服务对象
*/
StudentService studentService;
/**
* 构造方法
*/
public BrowseStudentFrame(String title){
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
//创建组件
panel=(JPanel) getContentPane();
pnlCenter=new JPanel();
pnlSouth=new JPanel();
pnlRow1=new JPanel();
pnlRow2=new JPanel();
pnlRow3=new JPanel();
pnlRow4=new JPanel();
pnlRow5=new JPanel();
pnlRow6=new JPanel();
pnlRow7=new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId=new JLabel("学号:");
lblName=new JLabel("姓名:");
lblSex=new JLabel("性别:");
lblAge =new JLabel("年龄:");
lblDepartment=new JLabel("系部:");
lblClass=new JLabel("班级:");
lblTelephone=new JLabel("电话:");
txtId=new JTextField(40);
txtName=new JTextField(40);
txtSex=new JTextField(40);
txtAge=new JTextField(40);
txtDepartment=new JTextField(40);
txtClass=new JTextField(40);
txtTelephone=new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop=new JButton("第一条[T]");
btnPrevious=new JButton("上一条[P]");
btnNext=new JButton("下一条[N]");
btnBottom=new JButton("最后一条[B]");
btnExit=new JButton("退出[X]");
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
//添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter,BorderLayout.CENTER);
panel.add(pnlSouth,BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7,1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(btnTop);
pnlSouth.add(btnPrevious);
pnlSouth.add(btnNext);
pnlSouth.add(btnBottom);
pnlSouth.add(btnExit);
//设置窗口属性
setSize(800,500);
//设置窗口不可调整大小
setLocationRelativeTo(null);
//设置窗口屏幕居中
setVisible(true);
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建学生服务对象
studentService=new StudentServicelmpl();
//获取全部学生列表
student=studentService.findAllStudent();
//判断是否有学生记录
if (student.size()>0){
//设置当前记录号
currentRow=1;
//设置窗口标题
setTitle("浏览学生记录"+"&&当前记录:"+currentRow);
//填充窗口个文本框数据
fillFrameDate(currentRow);
}else {
JOptionPane.showMessageDialog(null,"表中没有记录",
"浏览学生表记录",JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
//【第一条】按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow=1;
//填充当前记录数
fillFrameDate(currentRow);
}
});
//[上一条】按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(currentRow>1){
//设置当前记录号
currentRow--;
//填充当前记录数据
fillFrameDate(currentRow);
}else {
JOptionPane.showMessageDialog(null, "已到第一条记录!",
"浏览学表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
//[下一条]按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow0) {
setTitle("浏览学生表记录"+"&&当前记录:"+currentRow);
txtId.setText(student.get(currentRow).getId());
txtName.setText(student.get(currentRow).getName());
txtSex.setText(student.get(currentRow).getSex());
txtAge.setText(student.get(currentRow).getAge()+"");
txtDepartment.setText(student.get(currentRow).getDepartment());
txtClass.setText(student.get(currentRow).getClazz());
txtTelephone.setText(student.get(currentRow).getTelephone());
}
}
/**
* 主方法
*/
public static void main(String[] args) {
new BrowseStudentFrame("");
}
}
运行程序,效果如下:
8)添加学生窗口AddStudentFrame
完整的代码如下:
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
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;
import java.util.List;
public class AddStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtID;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnExit;
private JButton btnOK;
private JButton btnCancel;
/**
* 当前记录号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 构造方法
*/
public AddStudentFrame(String title){
super(title);
initGUI();
}
private void initGUI() {
//创造组件
panel =(JPanel) getContentPane();
pnlCenter=new JPanel();
pnlSouth=new JPanel();
pnlRow1=new JPanel();
pnlRow2=new JPanel();
pnlRow3=new JPanel();
pnlRow4=new JPanel();
pnlRow5=new JPanel();
pnlRow6=new JPanel();
pnlRow7=new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId=new JLabel("学号:");
lblName=new JLabel("姓名:");
lblSex=new JLabel("性别:");
lblAge=new JLabel("年龄:");
lblDepartment=new JLabel("系部:");
lblClass=new JLabel("班级:");
lblTelephone=new JLabel("电话:");
txtID=new JTextField(20);
txtName=new JTextField(20);
txtSex=new JTextField(20);
txtAge=new JTextField(20);
txtDepartment=new JTextField(20);
txtClass=new JTextField(20);
txtTelephone=new JTextField(20);
btnOK=new JButton("确定[O]");
btnCancel=new JButton("取消[C]");
btnExit=new JButton("退出[X]");
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnExit.setMnemonic(KeyEvent.VK_X);
//添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter,BorderLayout.CENTER);
panel.add(pnlSouth,BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7,1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtID);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(btnOK);
pnlSouth.add(btnCancel);
pnlSouth.add(btnExit);
//设置窗口属性
setSize(800,600);
//设置窗口不可调整大小
setResizable(false);
//设置窗口不可调整大小
pack();
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//【确定】按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//学号非空校验
if(txtID.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"学号不能为空!",
"增加学生记录",JOptionPane.WARNING_MESSAGE);
txtID.requestFocus();
return;
}
//年龄数字校验
if(!isNumber(txtAge.getText().trim())){
JOptionPane.showMessageDialog(null,"注意:年龄全由数字构成!",
"增加学生记录",JOptionPane.ERROR_MESSAGE);
txtAge.setText("");
txtAge.requestFocus();
return;
}
//创建学生实体
Student student=new Student();
//设置学生实体属性
student.setId(txtID.getText().trim());
student.setName(txtName.getText().trim());
student.setSex(txtSex.getText().trim());
student.setAge(Integer.parseInt(txtAge.getText()));
student.setDepartment(txtDepartment.getText().trim());
student.setClazz(txtName.getText().trim());
student.setTelephone(txtName.getText().trim());
//创建学生服务对象
StudentService studentService=new StudentServicelmpl();
//添加学生记录
int count=studentService.addStudent(student);
//判断是否添加成功
if(count >0){
JOptionPane.showMessageDialog(null,"添加记录成功!",
"增加学生记录",JOptionPane.INFORMATION_MESSAGE);
txtID.setText("");
txtName.setText("");
txtSex.setText("");
txtSex.setText("");
txtAge.setText("");
txtDepartment.setText("");
txtClass.setText("");
txtTelephone.setText("");
txtID.requestFocus();
}else {
JOptionPane.showMessageDialog(null,"添加记录失败",
"增加学生记录",JOptionPane.ERROR_MESSAGE);
}
}
});
//【取消】按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txtID.setText("");
txtName.setText("");
txtSex.setText("");
txtDepartment.setText("");
txtClass.setText("");
txtTelephone.setText("");
txtClass.setText("");
txtTelephone.setText("");
txtID.requestFocus();
}
});
//【退出】按钮单击事件处理
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//【学号】文本框按键事件处理
txtID.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
if(!txtID.getText().trim().equals("")){
txtName.requestFocus();
}
}
}
});
//【姓名】文本框按键事件处理
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
txtSex.requestFocus();
}
}
});
//【性别】文本框按键事件处理
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
txtAge.requestFocus();
}
}
});
//【年龄】文本框按键事件处理
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
txtDepartment.requestFocus();
}
}
});
//【系部】文本框按键事件处理
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
txtClass.requestFocus();
}
}
});
//【班级】文本框按键事件处理
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
txtTelephone.requestFocus();
}
}
});
}
/**
* 判断一个字符串是否全是数字
*/
boolean isNumber(String str){
for (int i=0;i'9'){
return false;
}
}
return true;
}
/**
* 主方法
*/
public static void main(String[] args) {
new AddStudentFrame("");
}
}
运行程序,测试结果如下:
9)编辑学生窗口EditStudentFrame
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EditStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
private JPanel pnlSouth1;
private JPanel pnlSouth2;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
private JButton btnEdit;
private JButton btnOK;
private JButton btnCancel;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
StudentService studentService;
/**
* 构造方法
*/
public EditStudentFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
//创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new GridLayout(2, 1));
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlSouth1 = new JPanel();
pnlSouth2 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge = new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass = new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(40);
txtName = new JTextField(40);
txtAge = new JTextField(40);
txtSex = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop = new JButton("第一条[T]");
btnPrevious = new JButton("上一条[P]");
btnNext = new JButton("下一条[N]");
btnBottom = new JButton("最后一条[B]");
btnExit = new JButton("退出[X]");
btnEdit = new JButton("编辑[E]");
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消C]");
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
btnExit.setMnemonic(KeyEvent.VK_E);
btnExit.setMnemonic(KeyEvent.VK_O);
btnExit.setMnemonic(KeyEvent.VK_C);
//添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlSouth, BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7, 1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
;
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(pnlSouth1);
pnlSouth.add(pnlSouth2);
pnlSouth1.add(btnTop);
pnlSouth1.add(btnPrevious);
pnlSouth1.add(btnNext);
pnlSouth1.add(btnBottom);
pnlSouth1.add(btnExit);
pnlSouth2.add(btnEdit);
pnlSouth2.add(btnOK);
pnlSouth2.add(btnCancel);
//设置窗口属性
setSize(900, 600);
//设置窗口为不可调整大小
setLocationRelativeTo(null);
//设置窗口屏幕居中
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建学生服务对象
studentService = new StudentServicelmpl();
//获取全部学生列表
students = studentService.findAllStudent();
//判断是否有学生记录
if (students.size() > 0) {
//设置当前记录号
currentRow = 1;
//设置窗口标题
setTitle("浏览学生表记录" + "&&当前记录:" + currentRow);
//填充窗口各文本框数据
fillFrameDate(currentRow);
} else {
JOptionPane.showMessageDialog(null, "表中没有记录!",
"浏览学生表记录", JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
//[第一条】按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = 1;
//填充当前记录数
fillFrameDate(currentRow);
}
});
//[上一条】按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow > 1) {
//设置当前记录号
currentRow--;
//填充当前记录数据
fillFrameDate(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到第一条记录!",
"浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
//【下一条】按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() - 1) {
//设置当前记录号
currentRow++;
//填充当前记录数据
fillFrameDate(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到第一条记录!",
"浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
//【最后一条】按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = students.size() - 1;
//填充当前记录数
fillFrameDate(currentRow);
}
});
//【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//【编辑】按钮单击事件
btnEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置窗口标题
setTitle("编辑学生表记录" + "&&当前记录:" + currentRow);
txtName.setEditable(true);
txtSex.setEditable(true);
txtAge.setEditable(true);
txtDepartment.setEditable(true);
txtClass.setEditable(true);
txtTelephone.setEditable(true);
btnOK.setEnabled(true);
btnCancel.setEnabled(true);
}
});
//【确定】按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取当前学生实体
Student student = students.get(currentRow);
if (isNumber(txtAge.getText())) {
if (isLegalTelephone(txtTelephone.getText())) {
//修改学生实体属性
student.setName(txtName.getText());
student.setSex(txtSex.getText());
student.setAge(Integer.parseInt(txtAge.getText()));
student.setDepartment(txtDepartment.getText());
student.setClazz(txtClass.getText());
student.setTelephone(txtTelephone.getText());
//更新学生信息
int count = studentService.updateStudent(student);
//判断是否更新成功
if (count > 0) {
JOptionPane.showMessageDialog(null, "更新记录成功!",
"编辑学生信息成功", JOptionPane.INFORMATION_MESSAGE);
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnExit.setEnabled(true);
txtName.setEnabled(false);
txtSex.setEnabled(false);
txtAge.setEnabled(false);
txtDepartment.setEnabled(false);
txtClass.setEnabled(false);
txtTelephone.setEnabled(false);
//重新获取全部学生列表
students = studentService.findAllStudent();
//设置窗口标题
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
} else {
JOptionPane.showMessageDialog(null, "更新记录失败!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "非法手机号!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtTelephone.selectAll();
txtTelephone.requestFocus();
}
} else {
JOptionPane.showMessageDialog(null, "年龄必须是数字!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtAge.selectAll();
txtAge.requestFocus();
}
}
});
//【取消】按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnExit.setEnabled(true);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
//恢复文本修改前的值
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
});
// 【学号】文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtName.requestFocus();
}
}
});
// 【姓名】文本框按键事件
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtSex.requestFocus();
}
}
});
// 【性别】文本框按键事件
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtAge.requestFocus();
}
}
});
// 【年龄】文本框按键事件
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtDepartment.requestFocus();
}
}
});
// 【系部】文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtClass.requestFocus();
}
}
});
// 【班级】文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtTelephone.requestFocus();
}
}
});
}
/**
* 将当前记录数据填充窗口各文本框
*
* @param currentRow
*/
private void fillFrameDate(int currentRow) {
if (currentRow > 0) {
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
txtId.setText(students.get(currentRow).getId());
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
}
// 判断一个字符串是否全是数字
private boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
return false;
}
}
return true;
}
/**
* 判断是否合法手机号
*
* @param telephone
* @return
*/
private boolean isLegalTelephone(String telephone) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(telephone);
return m.matches();
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new EditStudentFrame("");
}
}
运行程序,测试效果如下:
10)按学号删除学生窗口DeleteStudentByIdFrame
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
public class DeleteStudentByIdFrame extends JFrame {
/**
* 学号标签
/
private JLabel lblInputId;
/*
* 学号文本框
/
private JTextField txtId;
/*
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentByIdFrame(String title){
super(title);
initGUI();
}
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputId = new JLabel("输入学号:");
txtId = new JTextField(10);
txtId.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputId);
pnlNorth.add(txtId);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServicelmpl();
// 获取全部学生列表
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 设置窗口大小
setSize(600, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按学号删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// 【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删学生学号
String id = txtId.getText().trim();
// 按学号删除学生
int count = studentService.deleteStudentById(id);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudent();
// 清空待删学生学号文本框
txtId.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取全部学生记录
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 【学号】文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtId.setText(table.getValueAt(row, 0).toString());
}
});
}
private void doQuery() {
// 获取查询学号
String id = txtId.getText().trim();
if (!id.equals("")) {
students.clear();
Student student = studentService.findStudentById(id);
if (student != null) {
// 将查询到的学生添加到列表
students.add(student);
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtId.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new DeleteStudentByIdFrame("");
}
}
运行程序,测试结果如下:
11)按班级删除学生窗口DeleteStudentsByClass
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
public class DeleteStudentsByClassFrame extends JFrame {
/**
* 班级标签
/
private JLabel lblInputClass;
/*
* 班级文本框
/
private JTextField txtClass;
/*
* 面板
/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/*
* 按钮
/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/*
* 记录行集
/
private Vector rows;
/*
* 表格列标题
/
private Vector colHead;
/*
* 表格
/
private JTable table;
/*
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentsByClassFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputClass = new JLabel("输入班级:");
txtClass = new JTextField(10);
txtClass.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputClass);
pnlNorth.add(txtClass);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServicelmpl();
// 获取全部学生列表
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 设置窗口大小
setSize(600, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按班级删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//退出
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删班级
String clazz = txtClass.getText().trim();
// 按班级删除学生
int count = studentService.deleteStudentByClass(clazz);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudent();
// 清空待删班级文本框
txtClass.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取全部学生记录
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 【班级】文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtClass.setText(table.getValueAt(row, 5).toString());
}
});
}
private void doQuery() {
// 获取查询班级
String clazz = txtClass.getText().trim();
if (!clazz.equals("")) {
students = studentService.findStudentByClass(clazz);
if (students.size() > 0) {
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查班级!", "警告", JOptionPane.WARNING_MESSAGE);
txtClass.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtClass.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new DeleteStudentsByClassFrame("");
}
12)按系部删除学生窗口DeleteStudentsByDepartment
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
public class DeleteStudentsByDepartmentFrame extends JFrame {
/**
* 系部标签
/
private JLabel lblInputDepartment;
/*
* 系部文本框
/
private JTextField txtDepartment;
/*
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentsByDepartmentFrame(String title){
super(title);
initGUI();
}
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputDepartment = new JLabel("输入系部:");
txtDepartment = new JTextField(10);
txtDepartment.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputDepartment);
pnlNorth.add(txtDepartment);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServicelmpl();
// 获取全部学生列表
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 设置窗口大小
setSize(600, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按系部删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// 【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删系部
String department = txtDepartment.getText().trim();
// 按系部删除学生
int count = studentService.deleteStudentByDepartment(department);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudent();
// 清空待删班级文本框
txtDepartment.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取全部学生记录
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 【系部】文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtDepartment.setText(table.getValueAt(row, 4).toString());
}
});
}
private void doQuery() {
// 获取查询系部
String department = txtDepartment.getText().trim();
if (!department.equals("")) {
students = studentService.findStudentByDepartment(department);
if (students.size() > 0) {
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查系部!", "警告", JOptionPane.WARNING_MESSAGE);
txtDepartment.requestFocus();
}
}
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtDepartment.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new DeleteStudentsByDepartmentFrame("");
}
}
运行结果,如下:
13)按学号查询学生窗口FindStudentById
package net.zzm.student.gui;
import net.zzm.student.bean.Student;
import net.zzm.student.service.StudentService;
import net.zzm.student.service.impl.StudentServicelmpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.List;
public class FindStudentByIdFrame extends JFrame {
/**
* 学号标签
/
private JLabel lblInputId;
/*
* 学号文本框
/
private JTextField txtId;
/*
* 面板
/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/*
* 按钮
/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/*
* 记录行集
/
private Vector rows;
/*
* 表格列标题
/
private Vector colHead;
/*
* 表格
/
private JTable table;
/*
* 滚动面板
/
private JScrollPane scroller;
/*
* 当前记录行号
/
private int currentRow;
/*
* 学生列表
/
private List students;
/*
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentByIdFrame(String title) {
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputId = new JLabel("输入学号:");
txtId = new JTextField(10);
txtId.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputId);
pnlNorth.add(txtId);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServicelmpl();
// 获取全部学生列表
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 设置窗口大小
setSize(600, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按学号查询学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudent();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnPrint.setEnabled(false);
}
});
// 【学号】文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtId.setText(table.getValueAt(row, 0).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询学号
String id = txtId.getText().trim();
if (!id.equals("")) {
students.clear();
Student student = studentService.findStudentById(id);
if (student != null) {
// 将查询到的学生添加到列表
students.add(student);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);
txtId.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtId.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new FindStudentByIdFrame("");
}