设置学校信息设置状态栏信息package net.lhp.student.gui;
/**
- 功能:按姓名查询学生记录
- 作者:兰海平
- 日期:2019年7月2日
*/
import net.lhp.student.bean.Student;
import net.lhp.student.service.StudentService;
import net.lhp.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.DISPOSE_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("");
}
}