(9)按学号查询学生窗口FindStudentById
仿照老师的代码打出来之后运行结果如下:
为什么只能打印一次呢?
找到和打印按钮相关的属性设置,原来之前不小心将打印按钮设置成了不可用
更改后的完整代码如下:
package net.qt.student.gui;
import net.qt.student.bean.Student;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
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;
/**
* 按学号查询学生记录
* 作者:谯婷
* 日期:2019.6.24
*/
public class FindStudentByIdFrame extends JFrame {
/**
* 学号标签
*/
private JLabel lblInputId;
/**
* 学号文本框
*/
private JTextField txtId;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNouth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 记录当前行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
* @param title
*/
public FindStudentByIdFrame(String title){
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI(){
//创建组件
panel = (JPanel)getContentPane();
pnlNouth = 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_O );
btnBrowseAll = new JButton( "显示全部记录[A]" );
btnBrowseAll.setMnemonic( KeyEvent.VK_A );
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 );
panel.add( pnlNouth, BorderLayout.NORTH );
pnlNouth.add( lblInputId );
pnlNouth.add( txtId );
pnlNouth.add( btnQuery );
pnlNouth.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 e) {
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 e) {
doQuery();
}
} );
//【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取全部学生记录
students = studentService.findAllStudents();
//填充表格数据
fillTableData();
//打印按钮可用
btnPrint.setEnabled(true);
}
} );
//【学号】文本框按键事件
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 );
}
//填充表格
fillTableData();
}else{
JOptionPane.showMessageDialog( null,"请输入待查学生学号!","警告",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<String> currentRow = new Vector<String>( );
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( "" );
}
}
运行效果如下:
(10)按姓名查询学生窗口FindStudentsByName
完整代码如下:
package net.qt.student.gui;
import net.qt.student.bean.Student;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
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 FindStudentsByNameFrame 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<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentsByNameFrame(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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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.clear();
colHead.add("姓名");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
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 FindStudentsByNameFrame( "" );
}
}
运行结果:
因为前面对数据库进行了太多的删除操作,导致里面的数据少得可怜
所以我们将数据库还原一下,操作步骤如下:
右键选择还原备份从…然后进行对还原文件进行选择
这样数据库里面的内容就全部恢复啦~
哈哈,言归正传,继续码代码
(11)按班级查询学生窗口FindStudentsByClass
因为两个类代码很相似,所以有的地方直接可以拿来复制着用,这个类完成速度很快,
完整代码如下:
package net.qt.student.gui;
import net.qt.student.bean.Student;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
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;
/**
* 功能:按班级查询学生记录
* 作者:谯婷
* 日期:2019.6.24
*/
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<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
* @param title
*/
public FindStudentsByClassFrame(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);
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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<String> currentRow = new Vector<String>();
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( "" );
}
}
运行结果:
(11)按系部查询学生窗口FindStudentsByDepartment
完整代码如下:
package net.qt.student.gui;
import net.qt.student.bean.Student;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
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;
/**
* 功能:按系部查询学生记录
* 作者:谯婷
* 日期:2019.6.24
*/
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<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
* @param title
*/
public FindStudentsByDepartmentFrame(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);
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtDepartment.setText(table.getValueAt(row, 5).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<String> currentRow = new Vector<String>();
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( "");
}
}
运行效果:
(12)按性别统计人数窗口CountStudentsBySex
代码如下:
package net.qt.student.gui;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
/**
* 功能:按照性别统计人数
* 作者:谯婷
* 日期:2019.6.24
*/
public class CountStudentsBySexFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
private CountStudentsBySexFrame(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.findRowsBySex();
//设置表头
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 e) {
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 CountStudentsBySexFrame( "" );
}
}
运行效果:
(13)按班级统计人数窗口CountStudentsByClass
代码如下:
package net.qt.student.gui;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
/**
* 功能:按班级统计学生人数
* 作者:谯婷
* 日期:2019年6月20日
*/
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<String> 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( "" );
}
}
效果如下:
(14)按系部统计人数窗口CountStudentsByDepartment
代码如下:
package net.qt.student.gui;
import net.qt.student.service.StudentService;
import net.qt.student.service.impl.StudentServiceImpl;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
/**
* 功能:按系部统计学生人数
* 作者:谯婷
* 日期:2019年6月20日
*/
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<String> 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 CountStudentsByDepartmentFrame( "");
}
}
效果如下图:
(15)设置状态栏信息窗口SetStatusBarFrame
代码如下:
package net.qt.student.gui;
import net.qt.student.app.Application;
import net.qt.student.bean.Status;
import net.qt.student.service.StatusService;
import net.qt.student.service.impl.StatusServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
* 功能:设置状态栏信息
* 作者:谯婷
* 日期:2019.6.24
*/
public class SetStatusBarFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
/**
* 标签
*/
private JLabel lblCollege;
private JLabel lblVersion;
private JLabel lblAuthor;
private JLabel lblTelephone;
private JLabel lblAddress;
private JLabel lblEmail;
/**
* 文本框
*/
private JTextField txtCollege;
private JTextField txtVersion;
private JTextField txtAuthor;
private JTextField txtTelephone;
private JTextField txtAddress;
private JTextField txtEmail;
/**
* 按钮
*/
private JButton btnSave;
private JButton btnExit;
/**
* 状态服务对象
*/
private StatusService statusService;
/**
* 构造方法
* @param title
*/
public SetStatusBarFrame(String title){
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI(){
//创建组件
panel = (JPanel)getContentPane();
panel1 = new JPanel( );
panel2 = new JPanel( );
panel3 = new JPanel( );
panel4 = new JPanel( );
panel5 = new JPanel( );
lblCollege = new JLabel( "校名:" );
lblVersion = new JLabel( "版本:" );
lblAuthor = new JLabel( "作者:" );
lblTelephone = new JLabel( "电话:" );
lblAddress = new JLabel( "地址:" );
lblEmail = new JLabel( "邮件:" );
txtCollege = new JTextField( 12 );
txtVersion = new JTextField( 12 );
txtAuthor = new JTextField( 12 );
txtTelephone = new JTextField( 12 );
txtAddress = new JTextField( 29 );
txtEmail = new JTextField( 29 );
btnSave = new JButton( "保存[S]" );
btnSave.setMnemonic( KeyEvent.VK_S );
btnExit = new JButton( "退出[X]" );
btnExit.setMnemonic( KeyEvent.VK_X);
//添加组件
panel.setLayout( new GridLayout( 5,1 ) );
panel.add( panel1 );
panel.add( panel2 );
panel.add( panel3 );
panel.add( panel4 );
panel.add( panel5);
panel1.add( lblCollege );
panel1.add( txtCollege);
panel1.add( lblVersion);
panel1.add( txtVersion);
panel2.add( lblAuthor );
panel2.add( txtAuthor );
panel2.add( lblTelephone);
panel2.add( txtTelephone );
panel3.add( lblAddress );
panel3.add( txtAddress );
panel4.add( lblEmail );
panel4.add( txtEmail );
panel5.add( btnSave );
panel5.add( btnExit );
//创建状态服务对象
statusService = new StatusServiceImpl();
//按标识符获取状态对象
Status status = statusService.findStatusById( 1 );
if(status != null){
txtCollege.setText( status.getCollege() );
txtVersion.setText( status.getVersion() );
txtAuthor.setText( status.getAuthor() );
txtTelephone.setText( status.getTelephone() );
txtAddress.setText( status.getAddress() );
txtEmail.setText( status.getEmail() );
}
//设置窗口属性
setResizable( false );
pack();
setLocationRelativeTo( null );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//【关闭】按钮事件处理
btnExit.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
} );
//【保存】按钮事件处理
btnSave.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取状态信息对象
Status status = statusService.findStatusById( 1 );
//修改属性值
status.setCollege( txtCollege.getText() );
status.setVersion( txtVersion.getText() );
status.setAuthor( txtAuthor.getText() );
status.setTelephone( txtTelephone.getText() );
status.setAddress( txtAddress.getText() );
status.setEmail( txtEmail.getText() );
//更新状态记录
int count = statusService.updateStatus( status );
//判断是否更新成功
if(count > 0){
Application.mainFrame.setStatusBar();
Application.mainFrame.setTitle( "学生信息管理系统"+status.getVersion() );
}
}
} );
}
}
此时该程序还不可测试
要等到MainFrame类里写好setStatusBar()方法之后,才能测试该程序。
(16)系统主窗口MainFrame(V1.1)
码完有错,这是???求解!
喔喔~原来是之前在SetStatusBarFrame里面的时候,由于马虎直接写了思路没写代码
更正了之后的效果就是: