📄 actionlisteneroper.java
字号:
package cn.edu.csu.oo.gui.project.action.studentaction;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import cn.edu.csu.oo.gui.project.dao.common.DbException;
import cn.edu.csu.oo.gui.project.dao.studentdao.StudentDao;
import cn.edu.csu.oo.gui.project.dao.studentdao.impl.StudentDaoImpl;
import cn.edu.csu.oo.gui.project.view.dialog.StudentRegistDialog;
import cn.edu.csu.oo.gui.project.view.panel.StudentModifyPanel;
import cn.edu.csu.oo.gui.project.view.panel.StudentRegistPanel;
import cn.edu.csu.oo.gui.project.vo.StudentVo;
public class ActionListenerOper implements ActionListener {
private StudentRegistDialog studentRegistDialog;
private StudentRegistPanel studentRegistpanel;
private StudentModifyPanel studentmodifypanel;
public ActionListenerOper(StudentRegistDialog studentRegistDialog) {
this.studentRegistDialog = studentRegistDialog;
}
public ActionListenerOper(StudentRegistPanel studentRegistpanel) {
this.studentRegistpanel = studentRegistpanel;
}
public ActionListenerOper(StudentModifyPanel studentmodifypanel) {
this.studentmodifypanel = studentmodifypanel;
}
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
StudentDao dao = new StudentDaoImpl();
if (name.equals("确定")) {
// 对界面输入数据进行检测
if (studentRegistDialog instanceof JDialog) {
if (studentRegistDialog.checkInputText()) {
// 获取界面的提交数据
StudentVo value = studentRegistDialog.getInputTextValue();
try {
if (dao.registStudent(value)) {
JOptionPane.showMessageDialog(null, "数据插入成功", "输入插入提示信息",
JOptionPane.YES_OPTION);
} else {
JOptionPane.showMessageDialog(null, "数据插入失败", "输入插入提示信息",
JOptionPane.YES_OPTION);
}
} catch (DbException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "输入插入提示信息",
JOptionPane.YES_OPTION);
}
}
} else if (studentRegistpanel instanceof JPanel) {
if (studentRegistpanel.checkInputText()) {
// 获取界面的提交数据
StudentVo value = studentRegistpanel.getInputTextValue();
DefaultTableModel model = (DefaultTableModel) studentRegistpanel
.buildJTable().getModel();
try {
if (dao.registStudent(value)) {
JOptionPane.showMessageDialog(null, "数据插入成功", "输入插入提示信息",
JOptionPane.YES_OPTION);
Object[] data = { new Integer(value.getStudentNo()),
value.getStudentName(), value.getStudentSex(),
value.getStudentMajor(),
new Integer(value.getStudentGrade()), value.getStudentMail(),
value.getStudentAddress(), value.getStudentMobile(),
value.getStudentDate() };
model.addRow(data);
} else {
JOptionPane.showMessageDialog(null, "数据插入失败", "输入插入提示信息",
JOptionPane.YES_OPTION);
}
} catch (DbException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "输入插入提示信息",
JOptionPane.YES_OPTION);
}
}
}
} else if (name.equals("刷新")) {
Vector v = dao.getStudentInfo();
Iterator iter = v.iterator();
StudentVo value = null;
DefaultTableModel model = (DefaultTableModel) studentmodifypanel
.buildJTable().getModel();
int row = model.getRowCount();
for (int i = row - 1; i >= 0; i--) {
model.removeRow(i);
}
while (iter.hasNext()) {
value = (StudentVo) iter.next();
Object[] data = { new Integer(value.getStudentNo()),
value.getStudentName(), value.getStudentSex(),
value.getStudentMajor(), new Integer(value.getStudentGrade()),
value.getStudentMail(), value.getStudentAddress(),
value.getStudentMobile(), value.getStudentDate() };
model.addRow(data);
}
} else if (name.equals("修改")) {
if (studentmodifypanel.checkInputText()) {
StudentVo value = studentmodifypanel.getInputTextValue();
// 如果数据修改成功
if (dao.updateStudentInfo(value)) {
JOptionPane.showMessageDialog(null, "数据修改成功", "修改提示信息",
JOptionPane.YES_OPTION);
Object[] data = { new Integer(value.getStudentNo()),
value.getStudentName(), value.getStudentSex(),
value.getStudentMajor(), new Integer(value.getStudentGrade()),
value.getStudentMail(), value.getStudentAddress(),
value.getStudentMobile(), value.getStudentDate() };
JTable table = studentmodifypanel.buildJTable();
// 得到选中的记录的行号
int row = table.getSelectedRow();
// 循环修改jtable中选中的这条记录的各个字段的值
for (int i = 0; i < table.getColumnCount(); i++) {
table.setValueAt(data[i], row, i);
}
}
}
} else if (name.equals("删除")) {
int studentId = Integer.parseInt(studentmodifypanel.tabledata[0]);
if (dao.deleteStudentByStuId(studentId)) {
JOptionPane.showMessageDialog(null, "数据删除成功", "修改提示信息",
JOptionPane.YES_OPTION);
DefaultTableModel model = (DefaultTableModel) studentmodifypanel
.buildJTable().getModel();
int row = studentmodifypanel.buildJTable().getSelectedRow();
model.removeRow(row);
} else {
JOptionPane.showMessageDialog(null, "数据删除失败", "修改提示信息",
JOptionPane.YES_OPTION);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -