Java实训总结2019.6.25
作者:互联网
按系部删除学生窗口DeleteStudentsByDepartment
package net.Caoxinru.student.gui;
import net.Caoxinru.student.bean.Student;
import net.Caoxinru.student.service.StudentService;
import net.Caoxinru.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.Vector;
import java.util.List;
/**
-
功能:按系部删除学生记录
-
作者:曹鑫如
-
日期:2019年6月25日
/
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);
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); 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 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(); } }); // 【删除】按钮单击事件 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.deleteStudentsByDepartment(department); if (count > 0) { JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE); // 重新获取全部学生列表 students = studentService.findAllStudents(); // 清空待删班级文本框 txtDepartment.setText(""); // 填充数据 fillTableData(); // 删除按钮不可用 btnDelete.setEnabled(false); } else { JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE); } } } } }); // 【查询】按钮单击事件 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(); // 删除按钮不可用 btnDelete.setEnabled(false); } }); // 【系部】文本框按键事件 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, 4).toString()); } });
}
/**
- 查询方法
*/
private void doQuery() {
// 获取查询系部
String department = txtDepartment.getText().trim();
if (!department.equals("")) {
students = studentService.findStudentsByDepartment(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("");
}
}
按学号查询学生窗口FindStudentById
package net.Caoxinru.student.gui;
- 系部标签
import net.Caoxinru.student.bean.Student;
import net.Caoxinru.student.service.StudentService;
import net.Caoxinru.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;
/**
-
功能:按学号查询学生记录
-
作者:曹鑫如
-
日期:2019年6月25日
/
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 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();
// 删除按钮不可用
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("");
}
}
按姓名查询学生窗口FindStudentsByName
package net.Caoxinru.student.gui;
- 学号标签
import net.Caoxinru.student.bean.Student;
import net.Caoxinru.student.service.StudentService;
import net.Caoxinru.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;
/**
-
功能:按照名字查找学生信息
-
作者:曹鑫如
-
日期:2019年6月25日
/
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 colHead;
/* - 表格
/
private JTable table;
/* - 滚动面板
*/
private JScrollPane scroller;
/**
- 当前记录行号
/
private int currentRow;
/* - 学生列表
/
private List 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 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 FindStudentsByNameFrame("");
}
}
按班级查询学生窗口FindStudentsByClass
package net.Caoxinru.student.gui;
- 姓名标签
import net.Caoxinru.student.bean.Student;
import net.Caoxinru.student.service.StudentService;
import net.Caoxinru.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;
/**
-
功能:按班级查询学生记录
-
作者:曹鑫如
-
日期:2019年6月25日
/
public class FindStudentByClassFrame 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 FindStudentByClassFrame(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() == 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 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 FindStudentByClassFrame("");
}
}
按系部查询学生窗口FindStudentsByDepartment
- 班级标签
package net.Caoxinru.student.gui;
import net.Caoxinru.student.bean.Student;
import net.Caoxinru.student.service.StudentService;
import net.Caoxinru.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;
/**
-
功能:按系部查询学生记录
-
作者:曹鑫如
-
日期:2019年6月25日
/
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() == 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 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("");
}
- 系部标签
}
修改系统主窗口MainFrame
package net.Caoxinru.student.gui;
import net.Caoxinru.student.app.Application;
import net.Caoxinru.student.bean.Status;
import net.Caoxinru.student.service.StatusService;
import net.Caoxinru.student.service.impl.StatusServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.io.IOException;
/**
-
功能:主界面窗口
-
通过菜单系统
-
调用各个功能模块
-
作者:曹鑫如
-
日期:2019年6月20日
/
public class MainFrame extends JFrame {
/*- 菜单部分
/
private JMenuBar mnbMain;
/* - 设置菜单
/
private JMenu mnuSet;
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
/* - 操作菜单
/
private JMenu mnuOperate;
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
/* - 删除菜单
/
private JMenu mnuDelStu;
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
/* - 查询菜单
/
private JMenu mnuFind;
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
/* - 统计菜单
/
private JMenu mnuCount;
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
/* - 帮助菜单
/
private JMenu mnuHelp;
private JMenuItem mniHelp;
private JMenuItem mniAbout;
/* - 面板
/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlSouth;
/* - 状态栏标签
/
private JLabel lblStatusBar;
/* - 背景标签
*/
private JLabel lblBackground;
/**
- 图标对象
/
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
/* - 工具栏
/
private JToolBar toolbar;
/* - 按钮
/
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
/* - 状态对象
/
private Status status;
/* - 状态服务对象
*/
private StatusService statusService;
/**
- 构造方法
- @param title
*/
public MainFrame(String title) {
super(title);
initGUI();
}
/**
-
初始化图形用户界面
*/
private void initGUI() {
// 创建主菜单
mnbMain = new JMenuBar();// 创建【设置】菜单及其菜单项
mnuSet = new JMenu(“系统设置[S]”);
mnuSet.setMnemonic(KeyEvent.VK_S);
mniSetCollegeInfo = new JMenuItem(“学校信息”);
mniSetStatusBar = new JMenuItem(“状态栏信息”);
mniChangePassword = new JMenuItem(“修改密码”);
mniExit = new JMenuItem(“退出系统”);// 创建【操作】菜单及其菜单项
mnuOperate = new JMenu(“数据操作[O]”);
mnuOperate.setMnemonic(KeyEvent.VK_O);
mniAddStudent = new JMenuItem(“增加学生表记录”);
mnuDelStu = new JMenu(“删除学生表记录”);
mniEditStudent = new JMenuItem(“编辑学生表记录”);
mniBrowseStudent = new JMenuItem(“浏览学生表记录”);// 创建【删除学生表记录】的子菜单
mniDelStudentById = new JMenuItem(“按学号删除”);
mniDelStudentsByClass = new JMenuItem(“按班级删除”);
mniDelStudentsByDepartment = new JMenuItem(“按系部删除”);// 创建【查询】菜单及其菜单项
mnuFind = new JMenu(“查询学生[Q]”);
mnuFind.setMnemonic(KeyEvent.VK_Q);
mniFindStudentById = new JMenuItem(“按学号查询”);
mniFindStudentsByName = new JMenuItem(“按姓名查询”);
mniFindStudentsByClass = new JMenuItem(“按班级查询”);
mniFindStudentsByDepartment = new JMenuItem(“按系部查询”);// 创建【统计】菜单及其菜单项
mnuCount = new JMenu(“人数统计[C]”);
mnuCount.setMnemonic(KeyEvent.VK_C);
mniCountStudentsBySex = new JMenuItem(“按性别统计”);
mniCountStudentsByClass = new JMenuItem(“按班级统计”);
mniCountStudentsByDepartment = new JMenuItem(“按系部统计”);// 创建【帮助】菜单及其菜单项
mnuHelp = new JMenu(“帮助[H]”);
mnuHelp.setMnemonic(KeyEvent.VK_H);
mniHelp = new JMenuItem(“帮助”);
mniAbout = new JMenuItem(“关于”);// 创建图标对象
imgCollege = new ImageIcon(“images/college.png”);
imgPassword = new ImageIcon(“images/password.png”);
imgQuery = new ImageIcon(“images/query.png”);
imgBrowse = new ImageIcon(“images/browse.png”);
imgCount = new ImageIcon(“images/count.png”);
imgExit = new ImageIcon(“images/exit.png”);// 创建工具栏
toolbar = new JToolBar();
btnSetCollege = new JButton(“设置学校”, imgCollege);
btnSetCollege.setToolTipText(“设置学校信息”);
btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
btnChangePassword = new JButton(“修改密码”, imgPassword);
btnChangePassword.setToolTipText(“修改用户密码”);
btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
btnBrowseStudent = new JButton(“浏览学生”, imgBrowse);
btnBrowseStudent.setToolTipText(“浏览学生记录”);
btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
btnFindStudentById = new JButton(“查询学生”, imgQuery);
btnFindStudentById.setToolTipText(“按学号查询学生记录”);
btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
btnCountByDepartment = new JButton(“统计人数”, imgCount);
btnCountByDepartment.setToolTipText(“按系部统计学生人数”);
btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
btnExit = new JButton(“退出系统”, imgExit);
btnExit.setToolTipText(“退出系统”);
btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
toolbar.add(btnSetCollege);
toolbar.add(btnChangePassword);
toolbar.add(btnBrowseStudent);
toolbar.add(btnFindStudentById);
toolbar.add(btnCountByDepartment);
toolbar.add(btnExit);// 创建面板
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));// 创建背景图片
imgBackground = new ImageIcon(“images/background.jpg”);
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblStatusBar = new JLabel();// 设置菜单栏
setJMenuBar(mnbMain);
// 添加【设置】菜单
mnbMain.add(mnuSet);
mnuSet.add(mniSetCollegeInfo);
mnuSet.add(mniSetStatusBar);
mnuSet.add(mniChangePassword);
mnuSet.addSeparator();
mnuSet.add(mniExit);// 添加【删除学生表记录】菜单
mnuDelStu.add(mniDelStudentById);
mnuDelStu.add(mniDelStudentsByClass);
mnuDelStu.add(mniDelStudentsByDepartment);// 添加【操作】菜单
mnbMain.add(mnuOperate);
mnuOperate.add(mniAddStudent);
mnuOperate.add(mniEditStudent);
mnuOperate.add(mnuDelStu);
mnuOperate.add(mniBrowseStudent);// 添加【查询】菜单
mnbMain.add(mnuFind);
mnuFind.add(mniFindStudentById);
mnuFind.add(mniFindStudentsByName);
mnuFind.add(mniFindStudentsByClass);
mnuFind.add(mniFindStudentsByDepartment);// 添加【统计】菜单
mnbMain.add(mnuCount);
mnuCount.add(mniCountStudentsBySex);
mnuCount.add(mniCountStudentsByClass);
mnuCount.add(mniCountStudentsByDepartment);// 添加【帮助】菜单
mnbMain.add(mnuHelp);
mnuHelp.add(mniHelp);
mnuHelp.add(mniAbout);// 添加面板
panel.setLayout(new BorderLayout());
panel.add(toolbar, “North”);
panel.add(pnlCenter, “Center”);
panel.add(pnlSouth, “South”);
pnlCenter.add(lblBackground);
pnlSouth.add(lblStatusBar);// 非管理员不能设置状态栏
if (!Application.username.equals(“admin”)) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}// 设置状态栏信息
setStatusBar();// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态对象
status = statusService.findStatusByID(1);// 设置窗口尺寸
setSize(800, 640);
// 设置窗口可见
setVisible(true);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“学生信息管理系统” + status.getVersion());// 注册窗口监听器,继承窗口适配器,编写事件处理方法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});// 【设置状态栏信息】菜单项单击事件
mniSetStatusBar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetStatusBarFrame("");
}
});// 【修改密码】菜单项单击事件
mniChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});// 【退出系统】菜单项单击事件
mniExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});// 查询菜单
// 【按学号查询】菜单项单击事件
mniFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});// 【按姓名查询】菜单项单击事件
mniFindStudentsByName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByNameFrame("");
}
});// 【按班级查询】菜单项单击事件
mniFindStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByClassFrame("");
}
});// 【按系部查询】菜单项单击事件
mniFindStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByDepartmentFrame("");
}
});// 统计菜单
// 【按性别统计人数】菜单项单击事件
mniCountStudentsBySex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsBySexFrame("");
}
});// 【按班级统计人数】菜单项单击事件
mniCountStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByClassFrame("");
}
});// 【按系部统计人数】菜单项单击事件
mniCountStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});// 【增加学生记录】菜单项单击事件
mniAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddStudentFrame("");
}
});// 【按学号删除学生记录】菜单项单击事件
mniDelStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentByIdFrame("");
}
});// 【按班级删除学生记录】菜单项单击事件
mniDelStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByClassFrame("");
}
});// 【按系部删除学生记录】菜单项单击事件
mniDelStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByDepartmentFrame("");
}
});// 【编辑学生记录】菜单项单击事件
mniEditStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new EditStudentFrame("");
}
});// 【浏览学生记录】菜单项单击事件
mniBrowseStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});// 【帮助】菜单单击事件
mniHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec(“cmd /c start help/帮助文档.chm”);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), “学生信息管理系统”, JOptionPane.ERROR_MESSAGE);
}
}
});// 【关于】菜单单击事件
mniAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
“开发人员:” + status.getCollege() + “_” + status.getAuthor() + “\n联系电话:” + status.getTelephone()
+ “\n电子邮箱:” + status.getEmail(),
“学生信息管理系统” + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
}
});// 工具栏按钮单击事件
// 【设置学校信息】按钮
btnSetCollege.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});// 【修改密码】按钮
btnChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("");
}
});// 【浏览】按钮
btnBrowseStudent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("");
}
});// 【查询】按钮
btnFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("");
}
});// 【统计】按钮
btnCountByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
- 退出系统(询问用户是否要退出)
*/
private void exitSystem() {
int choice = JOptionPane.showConfirmDialog(this,
“您是否要退出系统?”, “学生信息管理系统”, JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
} else {
// 卸载当前窗口
dispose();
// 重新显示主窗口
Application.mainFrame = new MainFrame(“学生信息管理系统” + status.getVersion());
}
}
/**
- 设置状态栏信息
*/
public void setStatusBar() {
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态栏对象
status = statusService.findStatusByID(1);
// 设置状态栏标签
lblStatusBar.setText(
status.getCollege() + “学生信息管理系统” + status.getVersion() + " 作者:" + status.getAuthor() + " 地址:"
+ status.getAddress() + " 电话:" + status.getTelephone() + " 邮箱:" + status.getEmail());
}
/**
- 主方法
- @param args
*/
public static void main(String[] args) {
Application.mainFrame = new MainFrame("");
}
}
启动登录窗口,成功登录后,可以在主界面里通过菜单系统调用各个功能模块。
- 菜单部分
标签:25,Java,2019.6,private,public,add,student,new,void 来源: https://blog.csdn.net/Ccaoxinru/article/details/93617491