📄 addteacher.java
字号:
package com.exam.ui.manage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.exam.db.bean.Course;
import com.exam.db.bean.Teacher;
import com.exam.db.dao.CourseDao;
import com.exam.db.dao.TeacherDao;
import com.exam.ui.SuperFrame;
import com.exam.ui.utils.NumberLenghtLimitedDmt;
public class AddTeacher extends SuperFrame {
private static final long serialVersionUID = 1L;
public AddTeacher() {
try {
;
init();
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() {
this.setSize(600, 400);
this.setCenter();
this.setTitle("添加老师信息");
JPanel pnlTotal = new JPanel();
this.getContentPane().add(pnlTotal);
pnlTotal.setLayout(null);
final JTextField txtTeaID = new JTextField();
txtTeaID.setVisible(false);
txtTeaID.setEditable(false);
txtTeaID.setBounds(140, 280, 130, 20);
pnlTotal.add(txtTeaID);
TeacherDao teacherDao = new TeacherDao();
Teacher teacher = teacherDao.selectTeacherLastID();
int teaNo = Integer.parseInt(teacher.getTeaID().substring(1)) + 1;
if (teaNo < 10) {
txtTeaID.setText("T00" + teaNo);
} else if (teaNo < 100) {
txtTeaID.setText("T0" + teaNo);
} else {
txtTeaID.setText("T" + teaNo);
}
JLabel lblTeaName = new JLabel("姓 名");
lblTeaName.setBounds(70, 30, 70, 20);
pnlTotal.add(lblTeaName);
final JTextField txtTeaName = new JTextField();
txtTeaName.setBounds(140, 30, 130, 20);
pnlTotal.add(txtTeaName);
JLabel lblTeaSex = new JLabel("性 别");
lblTeaSex.setBounds(320, 30, 70, 20);
pnlTotal.add(lblTeaSex);
final JComboBox cboTeaSex = new JComboBox();
cboTeaSex.addItem("男");
cboTeaSex.addItem("女");
cboTeaSex.setBounds(390, 30, 130, 20);
pnlTotal.add(cboTeaSex);
JLabel lblTeaCardID = new JLabel("身份证号");
lblTeaCardID.setBounds(70, 70, 70, 20);
pnlTotal.add(lblTeaCardID);
final JTextField txtTeaCardID = new JTextField();
txtTeaCardID.setBounds(140, 70, 130, 20);
pnlTotal.add(txtTeaCardID);
JLabel lblTeaSort = new JLabel("职位类别");
lblTeaSort.setBounds(320, 70, 70, 20);
pnlTotal.add(lblTeaSort);
final JComboBox cboTeaSort = new JComboBox();
cboTeaSort.addItem("");
cboTeaSort.addItem("管理员");
cboTeaSort.addItem("班主任");
cboTeaSort.addItem("科目教员");
cboTeaSort.setBounds(390, 70, 130, 20);
pnlTotal.add(cboTeaSort);
JLabel lblTeaPhone = new JLabel("电话号码");
lblTeaPhone.setBounds(70, 110, 70, 20);
pnlTotal.add(lblTeaPhone);
final JTextField txtTeaPhone = new JTextField();
txtTeaPhone.setDocument(new NumberLenghtLimitedDmt(11));
txtTeaPhone.setBounds(140, 110, 130, 20);
pnlTotal.add(txtTeaPhone);
JLabel lblTeaCouID = new JLabel("任职科目");
lblTeaCouID.setBounds(320, 110, 70, 20);
pnlTotal.add(lblTeaCouID);
final JComboBox cboTeaCouID = new JComboBox();
cboTeaCouID.addItem("");
cboTeaCouID.setBounds(390, 110, 130, 20);
pnlTotal.add(cboTeaCouID);
List<Course> list = new ArrayList<Course>();
CourseDao courseDao = new CourseDao();
list = courseDao.selectAllCourse();
for (int i = 0; i < list.size(); i++) {
cboTeaCouID.addItem(list.get(i).getGrade() + "-"
+ list.get(i).getCouName());
}
JLabel lblTeaBirthday = new JLabel("出生日期");
lblTeaBirthday.setBounds(70, 150, 70, 20);
pnlTotal.add(lblTeaBirthday);
final JTextField txtTeaBirthday = new JTextField();
txtTeaBirthday.setEditable(false);
txtTeaBirthday.setBounds(140, 150, 130, 20);
pnlTotal.add(txtTeaBirthday);
JLabel lblTeaAge = new JLabel("年 龄");
lblTeaAge.setBounds(320, 150, 70, 20);
pnlTotal.add(lblTeaAge);
final JTextField txtTeaAge = new JTextField();
txtTeaAge.setEditable(false);
txtTeaAge.setBounds(390, 150, 130, 20);
pnlTotal.add(txtTeaAge);
JLabel lblTeaNation = new JLabel("民 族");
lblTeaNation.setBounds(70, 190, 70, 20);
pnlTotal.add(lblTeaNation);
final JTextField txtTeaNation = new JTextField("汉族");
txtTeaNation.setBounds(140, 190, 130, 20);
pnlTotal.add(txtTeaNation);
JLabel lblTeaTeaState = new JLabel("当前状态");
lblTeaTeaState.setBounds(320, 190, 70, 20);
pnlTotal.add(lblTeaTeaState);
final JComboBox cboTeaState = new JComboBox();
cboTeaState.addItem("在职");
cboTeaState.setBounds(390, 190, 130, 20);
pnlTotal.add(cboTeaState);
JLabel lblTeaAddr = new JLabel("家庭住址");
lblTeaAddr.setBounds(70, 245, 70, 20);
pnlTotal.add(lblTeaAddr);
final JTextArea txaTeaAddr = new JTextArea();
txaTeaAddr.setLineWrap(true);
final JScrollPane scpTeaAddr = new JScrollPane(txaTeaAddr);
scpTeaAddr.setBounds(140, 230, 130, 60);
pnlTotal.add(scpTeaAddr);
JLabel lblTeaRemark = new JLabel("备 注");
lblTeaRemark.setBounds(320, 245, 70, 20);
pnlTotal.add(lblTeaRemark);
final JTextArea txaTeaRemark = new JTextArea();
txaTeaRemark.setLineWrap(true);
final JScrollPane scpTeaRemark = new JScrollPane(txaTeaRemark);
scpTeaRemark.setBounds(390, 230, 130, 60);
pnlTotal.add(scpTeaRemark);
JButton btnTeaSave = new JButton("保 存");
btnTeaSave.setBounds(180, 320, 100, 25);
pnlTotal.add(btnTeaSave);
JButton btnTeaReset = new JButton("重 置");
btnTeaReset.setBounds(320, 320, 100, 25);
pnlTotal.add(btnTeaReset);
btnTeaSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String teaID = "";
String teaName = "";
String teaSex = "";
int teaAge = 0;
String teaBirthday = "";
String teaCardID = "";
String teaNation = "";
String teaPhone = "";
int teaState = 0;
int teaSort = 0;
String teaAddr = "";
int teaCouID = 0;
String teaRemark = "";
if (txtTeaName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请输入姓名!");
txtTeaName.requestFocus();
return;
}
teaID = txtTeaID.getText();
teaName = txtTeaName.getText();
teaSex = cboTeaSex.getSelectedItem().toString().trim();
if (txtTeaAge.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请输入身份证号");
return;
}
teaAge = Integer.parseInt(txtTeaAge.getText());
teaBirthday = txtTeaBirthday.getText();
teaNation = txtTeaNation.getText();
teaPhone = txtTeaPhone.getText();
teaState = cboTeaState.getSelectedIndex();
teaRemark = txaTeaRemark.getText();
teaCardID = txtTeaCardID.getText();
teaSort = cboTeaSort.getSelectedIndex();
teaCouID = cboTeaCouID.getSelectedIndex();
teaAddr = txaTeaAddr.getText();
teaRemark = txaTeaRemark.getText();
if (teaCardID.equals("")) {
JOptionPane.showMessageDialog(null, "请输入身份证号!");
txtTeaCardID.requestFocus();
return;
}
if (teaNation.equals("")) {
JOptionPane.showMessageDialog(null, "请输入民族!");
txtTeaNation.requestFocus();
return;
}
if(teaSort == 0){
JOptionPane.showMessageDialog(null, "请选择职位!");
return;
}
if(teaSort == 3){
if(teaCouID == 0){
JOptionPane.showMessageDialog(null, "请选择任课科目");
return;
}
}
if (teaAddr.equals("")) {
JOptionPane.showMessageDialog(null, "请输入家庭住址!");
txaTeaAddr.requestFocus();
return;
}
Teacher teacher = new Teacher();
teacher.setTeaID(teaID);
teacher.setTeaName(teaName);
teacher.setTeaSex(teaSex);
teacher.setTeaAge(teaAge);
teacher.setTeaBirthday(teaBirthday);
teacher.setTeaCardID(teaCardID);
teacher.setTeaNation(teaNation);
teacher.setTeaPhone(teaPhone);
teacher.setTeaAddr(teaAddr);
teacher.setTeaState(teaState);
teacher.setTeaSort(teaSort);
teacher.setCouID(teaCouID);
teacher.setRemark(teaRemark);
int result = 0;
TeacherDao teacherdao = new TeacherDao();
if (teaCouID == 0) {
result = teacherdao.insertTeacherNoCou(teacher);
} else {
result = teacherdao.insertTeacher(teacher);
}
if (result == 1) {
JOptionPane.showMessageDialog(null, "保存成功!");
dispose();
} else {
JOptionPane.showMessageDialog(null, "保存失败!\n年龄应大于22岁");
return;
}
}
});
btnTeaReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtTeaID.setText("");
txtTeaName.setText("");
cboTeaSex.setSelectedIndex(0);
txtTeaAge.setText("");
txtTeaBirthday.setText("");
txtTeaCardID.setText("");
txtTeaNation.setText("汉族");
txtTeaPhone.setText("");
cboTeaState.setSelectedIndex(0);
txaTeaAddr.setText("");
cboTeaSort.setSelectedIndex(0);
cboTeaCouID.setSelectedIndex(0);
txaTeaRemark.setText("");
}
});
// 设置电话号码的形态
txtTeaPhone.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
}
public void focusLost(FocusEvent arg0) {
int stuPhonelength = txtTeaPhone.getText().length();
if (stuPhonelength == 0 || stuPhonelength == 8
|| stuPhonelength == 11 || stuPhonelength == 12) {
} else {
JOptionPane.showMessageDialog(null, "电话号码输入错误!");
txtTeaPhone.setText("");
txtTeaPhone.requestFocus();
}
}
});
// 设置身份证号码的形态
txtTeaCardID.addFocusListener(new FocusListener() {
// 得到焦点方法
public void focusGained(FocusEvent e) {
}
// 丢失焦点方法
public void focusLost(FocusEvent arg0) {
String str = txtTeaCardID.getText().trim();
char[] c = str.toCharArray();
if (str.length() == 18) {
for (int i = 0; i < 18; i++) {
if (i == 0) {
if (c[i] > '0' && c[i] <= '9') {
} else {
JOptionPane.showMessageDialog(null,
"身份证号码有误!\n请重新输入!", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
} else if (i == 17) {
if ((c[i] > '0' && c[i] <= '9') || c[i] == 'X'
|| c[i] == 'x') {
} else {
JOptionPane.showMessageDialog(null,
"身份证号码有误!\n请重新输入!", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
} else {// 除0和18位外的其他位数
if (c[i] >= '0' && c[i] <= '9') {
} else {
JOptionPane.showMessageDialog(null,
"身份证号码有误!\n请重新输入!", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
}
}
} else {
JOptionPane.showMessageDialog(null, "身份证号码有误!\n请重新输入!",
"错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 出生日期
String stuYear = str.substring(6, 10);// 年
String stuMonth = str.substring(10, 12);// 月
String stuDay = str.substring(12, 14);// 日
// 当前时间
int year = Calendar.getInstance().get(Calendar.YEAR);// 年
// 身份证时间
int StuCardIDYear = Integer.parseInt(stuYear);// 年
int StuCardIDMouth = Integer.parseInt(stuMonth);// 月
int StuCardIDDay = Integer.parseInt(stuDay);// 日
// 年龄;
if ((StuCardIDMouth > 0 && StuCardIDMouth <= 12)
&& (StuCardIDDay <= 31 && StuCardIDDay > 0)) {
txtTeaBirthday.setText(stuYear + stuMonth + stuDay);
if ((year - StuCardIDYear) >= 18
&& (year - StuCardIDYear) <= 60) {
String age = Integer.toString(year - StuCardIDYear);
txtTeaAge.setText(age);
} else {
txtTeaBirthday.setText("");
txtTeaCardID.setText("");
return;
}
} else {
JOptionPane.showMessageDialog(null, "身份证号码有误!\n请重新输入!");
txtTeaBirthday.setText("");
txtTeaCardID.setText("");
return;
}
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -