实训目的:让学生综合运用J2SE有关知识开发【学生信息管理系统】。主要涉及程序控制结构、面向对象编程、图形用户界面、Java数据库应用、MySQL数据库这几个方面的内容。通过本项目的开发,让学生了解系统开发的一般流程,初步了解MVC模式与单元测试,巩固所学的Java理论知识,提高Java数据库应用编程的基本技能。
项目说明:本实训案例教学,利用Java SE基本知识是开发【学生信息管理系统】。本系统包括登录注册、系统设置(设置学校信息、设置状态栏信息、修改用户密码)、数据操作(增加学生记录、编辑学生记录、删除学生记录、浏览学生记录)、查询学生(按学号查询、按姓名查询、按班级查询、按系部查询)、人数统计(按性别统计人数、按班级统计人数、按系部统计人数)等功能。
创建FindStudentByIdFrame类,通过ID查询学生记录
编写主方法
声明变量
创建服务对象及构造方法
添加组件
创建标题边框对象和学生服务对象,并获取全部学生列表,填充表格数据
设置窗口属性
编写查询方法,填充表格方法
运行结果如下:
创建FindStudentByNameFrame类,按照姓名查找学生
代码如下:
package net.qf.student.gui;
/**
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.qf.student.bean.Student;
import net.qf.student.service.StudentService;
import net.qf.student.service.impl.StudentServiceImpl;
public class FindStudentByNameFrame extends JFrame {
/**
* 姓名标签
/
private JLabel lblInputName;
/*
* 姓名文本框
*/
private JTextField txtName;
/**
* 面板
*/
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 FindStudentByNameFrame(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();
lblInputName = new JLabel("输入姓名:");
txtName = new JTextField(10);
txtName.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(lblInputName);
pnlNorth.add(txtName);
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 StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
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.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtName.setText(table.getValueAt(row, 1).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询姓名
String name = txtName.getText().trim();
if (!name.equals("")) {
// 按姓名查询获取学生列表
students = studentService.findStudentsByName(name);
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生姓名!", "警告", JOptionPane.WARNING_MESSAGE);
txtName.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
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);
txtName.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new FindStudentByNameFrame("");
}
}
运行结果如下:
创建FindStudentsByClassFrame类,按照姓名查找学生
代码如下:
package net.qf.student.gui;
/**
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.qf.student.bean.Student;
import net.qf.student.service.StudentService;
import net.qf.student.service.impl.StudentServiceImpl;
public class FindStudentsByClassFrame 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 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 FindStudentsByClassFrame(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();
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);
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(lblInputClass);
pnlNorth.add(txtClass);
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 StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
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.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
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.findStudentsByClass(clazz);
// 填充表格
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 FindStudentsByClassFrame("");
}
}
运行结果如下:
创建FindStudentsByDepartmentFrame类,按照姓名查找学生
代码如下:
package net.qf.student.gui;
/**
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.qf.student.bean.Student;
import net.qf.student.service.StudentService;
import net.qf.student.service.impl.StudentServiceImpl;
public class FindStudentsByDepartmentFrame 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 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 FindStudentsByDepartmentFrame(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();
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);
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(lblInputDepartment);
pnlNorth.add(txtDepartment);
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 StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
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.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
/* (non-Javadoc)
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
/* (non-Javadoc)
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
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.findStudentsByDepartment(department);
// 填充表格
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 FindStudentsByDepartmentFrame("");
}
}
运行结果如下:
创建CountStudentBySexFrame类,按性别统计学生人数
编写主方法
声明变量
创建学生服务对象 ,构造方法,及初始化界面
创建对象
添加组件
创建学生服务对象,获取按性别统计结果记录行数
并设置表头
创建表格,设置窗口属性
退出按钮单击事件
打印按钮单击事件
运行结果如下
创建CountStudentByClassFrame类,按照班级统计人数
代码如下:
package net.qf.student.gui;
/**
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import net.qf.student.service.StudentService;
import net.qf.student.service.impl.StudentServiceImpl;
public class CountStudentsByClassFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
public CountStudentsByClassFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_P);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
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 StudentServiceImpl();
// 获取按性别统计结果记录行集
rows = studentService.findRowsByClass();
// 设置表头
colHead.add("班级");
colHead.add("人数");
// 创建表格(参数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);
}
// 设置窗口大小
setSize(300, 200);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按班级统计学生人数");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
}
/**
* 主方法
* @param args
*/
public static void main(String[] args) {
new CountStudentsByClassFrame("");
}
}
运行结果如下:
创建CountStudentByDepartmentFrame类,按照班级统计人数
代码如下:
package net.qf.student.gui;
/**
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import net.qf.student.service.StudentService;
import net.qf.student.service.impl.StudentServiceImpl;
public class CountStudentsByDepartmentFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
public CountStudentsByDepartmentFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_P);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
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 StudentServiceImpl();
// 获取按性别统计结果记录行集
rows = studentService.findRowsByDepartment();
// 设置表头
colHead.add("系部");
colHead.add("人数");
// 创建表格(参数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);
}
//设置窗口大小
setSize(300,200);
//设置窗口不可调整大小
setResizable(false);
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口标题
setTitle("按班级统计学生人数");
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
}
/**
* 主方法
* @param args
*/
public static void main(String[] args) {
new CountStudentsByClassFrame("");
}
}
运行结果如下:
今天主要编写了通过ID,NAME,CLASS,DEPARTMENT来查询学生的信息,然后按照班级,系部,性别来统计学生的人数,方法和昨天的删除添加的结构框架是差不多的,只是根据不同的需求添加了一下细节方面的东西,但记住大框架,其他的都会比较好解决的。