今天我完成了按学号查询学生FindStudentByIdFrame,按班级查询学生FindStudentByClassFrame,按系部查询学生FindStudentByDepartmentFrame,按姓名查询学生FindStudentByNameFrame,按学号删除记录DelteStudentByIdFrame,按班级删除学生DeleteStudentsByClassFrame,按系部删除学生DeleteStudentsByDepartmentFrame
1、package net.tr.student.gui;
/**
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;
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 btnBrowesAll;
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;
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);
btnBrowesAll = new JButton("显示全部记录[A]");
btnBrowesAll.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(btnBrowesAll);
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();
}
});
//显示全部记录按钮单击事件
btnBrowesAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//获取全部学生记录
students =studentService.findAllStudents();
//填充表格数据
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);
}
}
public static void main(String[] args) {
new FindStudentByIdFrame("");
}
}
2、package net.tr.student.gui;
/**
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;
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 btnBrowesAll;
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;
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);
btnBrowesAll = new JButton("显示全部记录[A]");
btnBrowesAll.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(btnBrowesAll);
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();
}
});
//显示全部记录按钮单击事件
btnBrowesAll.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() {
@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);
//填充表格
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);
}
}
public static void main(String[] args) {
new FindStudentsByClassFrame("");
}
}
3、package net.tr.student.gui;
/**
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;
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 btnBrowesAll;
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;
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);
btnBrowesAll = new JButton("显示全部记录[A]");
btnBrowesAll.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(btnBrowesAll);
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();
}
});
//显示全部记录按钮单击事件
btnBrowesAll.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() {
@Override
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.findStudentByDepartment(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);
}
}
public static void main(String[] args) {
new FindStudentsByDepartmentFrame("");
}
}
4、package net.tr.student.gui;
/**
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;
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 btnBrowesAll;
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;
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);
btnBrowesAll = new JButton("显示全部记录[A]");
btnBrowesAll.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(btnBrowesAll);
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();
}
});
//显示全部记录按钮单击事件
btnBrowesAll.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.findStudentByName(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 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);
}
}
public static void main(String[] args) {
new FindStudentsByNameFrame("");
}