📄 formemployee.java
字号:
}
}
for (int i = 0; i < cboDepartment.getItemCount(); i++) {
cboDepartment.setSelectedIndex(i);
if(cboDepartment.getSelectedItem().toString().equals(part))
{
break;
}
}
}
}
});
// 实例一个带有滚动条的容器
tableScrollPane=new JScrollPane(tableEmployee);
return tableScrollPane;
}
private MyDefaultMutableTreeNode getTreeNode() {
treeNode = new MyDefaultMutableTreeNode("DB培训中心");
treeNode.setID(0);
treeNode.setTag("root");
MyDefaultMutableTreeNode tNode;
DepartmentDAO manageDepartment = new DepartmentDAO();
departments = manageDepartment.getAllDepartments();
for(DepartmentDTO dept : departments){
tNode = new MyDefaultMutableTreeNode(dept.getName());
tNode.setID(dept.getDID());
treeNode.add(tNode);
}
return treeNode;
}
/*
* (非 Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
String es = e.getActionCommand();
if (es.equals("add")) {
manageEmployee=new EmployeeDAO();
txtId.setText(String.valueOf(manageEmployee.getLastEmployeeId()+1));
txtName.setText("");
txtPhone.setText("");
cboSex.setSelectedIndex(0);
cboDepartment.setSelectedIndex(0);
txtBirthday.setText("");
txtName.requestFocus(true);
} else if (es.equals("save")) {
//this.saveUser();
if(txtId.getText().equals(""))
{
txtId.setFocusable(true);
return;
}
if (this.isExsitedId(Integer.parseInt(txtId.getText()))) {
//修改
int n = JOptionPane.showConfirmDialog(this, "你真的要修改当前员工信息吗?",
"系统询问", JOptionPane.YES_NO_OPTION);
//-----------------------------------
if (n == JOptionPane.YES_OPTION) {
this.updateEmployee();
} else {
return;
}
//----------------------------------
} else {
this.addEmployee();
}
//treeModel.reload();
} else if (es.equals("delete")) {
try {
removeEmployee();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
} else if (es.equals("exit")) {
this.dispose();
}
}
//获得一个建议ID
private int getNewID() {
int id = 0;
//Iterator it = employees.values().iterator();
Iterator it = employees.iterator();
while (it.hasNext()) {
employee = (EmployeeDTO) it.next();
if (id < employee.getEID()) {
id = employee.getEID();
}
}
return id + 1;
}
// 确定ID在数据库中是存否在
private boolean isExsitedId(int id) {
boolean flag = false;
manageEmployee=new EmployeeDAO();
employees=manageEmployee.getAllEmployees();
// Iterator it = employees.values().iterator();
Iterator it = employees.iterator();
while (it.hasNext()) {
employee = (EmployeeDTO) it.next();
if (id == employee.getEID()) {
flag = true;
}
}
return flag;
}
//删除员工
private void removeEmployee() {
//node=(MyDefaultMutableTreeNode)treeEmployee.getLastSelectedPathComponent();
if(tableEmployee.getSelectedRow()<0)
{
JOptionPane.showMessageDialog(this,"请选择要删除的员工编号!");//,"系统提示",JOptionPane.ERROR_MESSAGE);
return;
}
int id=Integer.parseInt(tableModel.getValueAt(tableEmployee.getSelectedRow(),0).toString());
String name=tableModel.getValueAt(tableEmployee.getSelectedRow(),1).toString();
int n = JOptionPane.showConfirmDialog(FormEmployee.this, "你真的要删除编号为:[ "+id+ "],姓名为:[ "+name+" ]的员工吗?", "系统询问", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
manageEmployee = new EmployeeDAO();
if (manageEmployee.removeEmployeeById(id)) {
txtId.setText("");
txtName.setText("");
cboSex.setSelectedIndex(0);
cboDepartment.setSelectedIndex(0);
txtPhone.setText("");
txtName.requestFocus(true);
txtBirthday.setText("");
tableModel.removeRow(tableEmployee.getSelectedRow());
}
}
}
//添加新部门
private void addEmployee() {
txtId.setText(String.valueOf(manageEmployee.getLastEmployeeId()+1));
manageEmployee=new EmployeeDAO();
employee =new EmployeeDTO();
if(txtId.getText().equals(""))
{
JOptionPane.showMessageDialog(this," 员工编号不能为空,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtId.requestFocus(true);
return;
}
employee.setEID(Integer.parseInt(txtId.getText().trim()));
if(txtName.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"员工姓名不能为空,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtName.requestFocus(true);
return;
}
//检查是否是日期格式
try{
java.sql.Date.valueOf(txtBirthday.getText().trim());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"日期格式可能不对,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtBirthday.requestFocus(true);
return;
}
employee.setName(txtName.getText().trim());
employee.setSex(cboSex.getSelectedItem().toString());
employee.setDepartmentId(getDepartmentIdByName(cboDepartment.getSelectedItem().toString()));
employee.setPhone(txtPhone.getText().trim());
employee.setBirthday(Date.valueOf(txtBirthday.getText()));
employee.setPhone(txtPhone.getText().trim());
if(manageEmployee.addEmployee(employee))
{
txtId.setText(String.valueOf(getNewID()));
txtName.setText("");
cboSex.setSelectedIndex(0);
cboDepartment.setSelectedIndex(0);
txtBirthday.setText("");
txtPhone.setText("");
txtName.requestFocus(true);
//提法添加成功
}
else
{
JOptionPane.showMessageDialog(this,"信息添加失败,请核实数据后再进行保存!","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}
//到部门集合中根据部门名称找出相关编号
private int getDepartmentIdByName(String name)
{
int id=0;
for(DepartmentDTO dept : departments)
{
if(name.equals(dept.getName()))
{
id=dept.getDID();
break;
}
}
return id;
}
//修改部门信息
private void updateEmployee() {
manageEmployee=new EmployeeDAO();
employee =new EmployeeDTO();
if(txtId.getText().equals(""))
{
JOptionPane.showMessageDialog(this," 员工编号不能为空,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtId.requestFocus(true);
return;
}
employee.setEID(Integer.parseInt(txtId.getText().trim()));
if(txtName.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"员工姓名不能为空,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtName.requestFocus(true);
return;
}
//检查是否是日期格式
try{
java.sql.Date.valueOf(txtBirthday.getText().trim());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"日期格式可能不对,请输入!","系统提示",JOptionPane.INFORMATION_MESSAGE);
txtBirthday.requestFocus(true);
return;
}
employee.setName(txtName.getText().trim());
employee.setSex(cboSex.getSelectedItem().toString());
employee.setDepartmentId(getDepartmentIdByName(cboDepartment.getSelectedItem().toString()));
employee.setPhone(txtPhone.getText().trim());
employee.setBirthday(Date.valueOf(txtBirthday.getText()));
employee.setPhone(txtPhone.getText().trim());
if(manageEmployee.updateEmployee(employee))
{
txtId.setText(String.valueOf(getNewID()));
txtName.setText("");
cboSex.setSelectedIndex(0);
cboDepartment.setSelectedIndex(0);
txtBirthday.setText("");
txtPhone.setText("");
txtName.requestFocus(true);
//提法添加成功
}
else
{
JOptionPane.showMessageDialog(this,"信息修改失败,请核实数据后再进行修改!","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}
//树结点单击事件
public void valueChanged(TreeSelectionEvent e) {
try {
node = (MyDefaultMutableTreeNode) treeEmployee.getLastSelectedPathComponent();
this.setDataTable(node.getID());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -