📄 formemployee.java
字号:
/**
* -----------------------------------------------------------------------
* 创建时间:2006年2月9日
* 作 者:孙丰伟
* ------------------------------------------------------------------------
*/
package cn.sunfengwei.employee.view;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import cn.sunfengwei.employee.model.*;
public class FormEmployee extends JInternalFrame implements ActionListener,
TreeSelectionListener {
//工具栏上按钮
private JButton btnAdd, btnDelete, btnSave, btnQuery, btnExit;
//内容面板
private Container contentPane;
//部门树
private JTree treeEmployee;
private DefaultTreeModel treeModel;
private MyDefaultMutableTreeNode treeNode, node;
//员工表格
private DefaultTableModel tableModel;
private JTable tableEmployee;
//树与表的滚动条
private JScrollPane treeScrollPane, tableScrollPane;
//左右结构与上下结构的拆分面板
private JSplitPane leftSplitPane, topSplitPane;
//员工基本信息组件
private JTextField txtId, txtName, txtPhone,txtBirthday;
private JComboBox cboSex, cboDepartment;
private JSpinner spnBirthday;
//员工对象
private EmployeeDTO employee;
//部门对象
private DepartmentDTO department;
// 员工操作类
private EmployeeDAO manageEmployee;
//员工与部门信使
//private HashMap employees;
private Vector employees;
private Vector<DepartmentDTO> departments;
public FormEmployee(String title) {
super(title, false, //resizable
true, //closable
true, //maximizable
true);//iconifiable
contentPane = this.getContentPane();
treeScrollPane = new JScrollPane(this.setTree());
treeEmployee.addTreeSelectionListener(this);
topSplitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.setEmployeePanel(),this.setTable());
leftSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
this.treeScrollPane, topSplitPane);
//
// 在拆分窗口
//
leftSplitPane.setOneTouchExpandable(true);
leftSplitPane.setDividerLocation(150);
this.contentPane.add(leftSplitPane);
this.setBounds(10, 10, 800, 630);
this.setOpaque(true);
this.contentPane.add(this.createToolBar(), BorderLayout.NORTH);
}
private JToolBar createToolBar() {
JToolBar toolBar = new JToolBar();
btnAdd = new JButton(new ImageIcon("images\\add.gif"));
btnExit = new JButton(new ImageIcon("images\\exit.gif"));
btnSave = new JButton(new ImageIcon("images\\save1.gif"));
btnDelete = new JButton(new ImageIcon("images\\del.gif"));
btnQuery=new JButton(new ImageIcon("images\\04.gif"));
btnAdd.setActionCommand("add");
btnSave.setActionCommand("save");
btnDelete.setActionCommand("delete");
btnQuery.setActionCommand("find");
btnExit.setActionCommand("exit");
btnAdd.addActionListener(this);
btnSave.addActionListener(this);
btnDelete.addActionListener(this);
btnQuery.addActionListener(this);
btnExit.addActionListener(this);
toolBar.add(btnAdd);
toolBar.add(btnSave);
toolBar.add(btnDelete);
toolBar.add(btnQuery);
toolBar.addSeparator();
toolBar.add(btnExit);
return toolBar;
}
private JTree setTree() {
treeModel = new DefaultTreeModel(getTreeNode());
treeEmployee = new JTree(treeModel);
return treeEmployee;
}
//获取部门信息
private JComboBox setDepartment()
{
JComboBox cboDepart=new JComboBox();
DepartmentDAO manageDepartment=new DepartmentDAO();
departments=manageDepartment.getAllDepartments();
for(DepartmentDTO dept : departments)
{
cboDepart.addItem(dept);
}
return cboDepart;
}
//设置员工组件
private JPanel setEmployeePanel() {
JPanel panel = new JPanel();
// 使用Box容器,设置登录窗体中的组件
txtId = new JTextField(20); //20个字符
txtName = new JTextField(20);
txtPhone = new JTextField(20);
txtBirthday=new JTextField(20);
cboSex = new JComboBox();
cboSex.addItem("男");
cboSex.addItem("女");
cboDepartment =this.setDepartment();
spnBirthday = new JSpinner();
//员工编号与姓名
Box boxIdName = Box.createHorizontalBox();
boxIdName.add(Box.createHorizontalStrut(8));
boxIdName.add(new JLabel("员工ID:"));
boxIdName.add(Box.createHorizontalStrut(8));
boxIdName.add(txtId);
boxIdName.add(Box.createHorizontalStrut(8));
boxIdName.add(new JLabel("姓 名:"));
boxIdName.add(Box.createHorizontalStrut(8));
boxIdName.add(txtName);
//性别与生日
Box boxSexBirthday = Box.createHorizontalBox();
boxSexBirthday.add(Box.createHorizontalStrut(8));
boxSexBirthday.add(new JLabel("性 别:"));
boxSexBirthday.add(Box.createHorizontalStrut(8));
boxSexBirthday.add(cboSex);
boxSexBirthday.add(Box.createHorizontalStrut(8));
boxSexBirthday.add(new JLabel("生 日:"));
boxSexBirthday.add(Box.createHorizontalStrut(8));
//boxSexBirthday.add(spnBirthday);
boxSexBirthday.add(txtBirthday);
// 部门与电话
Box boxDepartPhone = Box.createHorizontalBox();
boxDepartPhone.add(Box.createHorizontalStrut(8));
boxDepartPhone.add(new JLabel("电 话:"));
boxDepartPhone.add(Box.createHorizontalStrut(8));
boxDepartPhone.add(txtPhone);
boxDepartPhone.add(new JLabel("部 门:"));
boxDepartPhone.add(Box.createHorizontalStrut(8));
boxDepartPhone.add(cboDepartment);
boxDepartPhone.add(Box.createHorizontalStrut(8));
Box box = Box.createVerticalBox();
box.add(boxIdName);
box.add(Box.createVerticalStrut(9));
box.add(boxSexBirthday);
box.add(Box.createVerticalStrut(9));
box.add(boxDepartPhone);
//box.add(Box.createVerticalStrut(9));
/*-----------------------------------------------*/
FlowLayout flow = new FlowLayout();
flow.setAlignment(FlowLayout.LEFT);
panel.setLayout(flow);
panel.add(box);
contentPane.validate();
return panel;
}
//设置表格数据
private void setDataTable(int departmentId)
{
// 添加之前先删除所有的数据,注意,如果要删除所有的应该倒删,否则也现问题
for (int i = tableModel.getRowCount()-1; i >=0; i--) {
tableModel.removeRow(i);
}
manageEmployee=new EmployeeDAO();
if(departmentId==0)
{
employees=manageEmployee.getAllEmployees();
}
else
{
employees=manageEmployee.getEmployeesByDepartment(departmentId);
}
//Iterator it=employees.values().iterator();
Iterator it=employees.iterator();
while(it.hasNext())
{
employee=(EmployeeDTO)it.next();
Object arrEmp[]={new Integer(employee.getEID()),
employee.getName(),
employee.getSex(),
employee.getBirthday(),
employee.getDepartmentName(),
employee.getPhone()};
tableModel.addRow(arrEmp);
}
}
//设计表格
private JScrollPane setTable()
{
JPanel panel=new JPanel();
// 使用Box容器,设置登录窗体中的组件
tableModel=new DefaultTableModel();
tableModel.addColumn("编号");
tableModel.addColumn("姓名");
tableModel.addColumn("性别");
tableModel.addColumn("生日");
tableModel.addColumn("部门");
tableModel.addColumn("电话");
// 实例化一表格对象,内容为前面的表格模板
tableEmployee=new JTable(tableModel);
// 下列代码设置列宽
TableColumn column = null;
for (int i = 0; i < 4; i++) {
column = tableEmployee.getColumnModel().getColumn(i);
if (i == 1) {
column.setPreferredWidth(120); //sport column is bigger
} else {
column.setPreferredWidth(50);
}
}
tableEmployee.setGridColor(Color.BLUE);
//一次只能选择一行
tableEmployee.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
final ListSelectionModel LSM=tableEmployee.getSelectionModel();
//当击表格时,将表格中的数据添加上面组件上
LSM.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if(!lsm.isSelectionEmpty())
{
int selectIndex=lsm.getMinSelectionIndex();
txtId.setText(tableModel.getValueAt(selectIndex,0).toString());
txtName.setText(tableModel.getValueAt(selectIndex,1).toString());
String sex=tableModel.getValueAt(selectIndex,2).toString(); //女
txtBirthday.setText(tableModel.getValueAt(selectIndex,3).toString());
String part=tableModel.getValueAt(selectIndex,4).toString(); //考试中心
txtPhone.setText(tableModel.getValueAt(selectIndex,5).toString());
//System.out.println(duty);
for (int i = 0; i < cboSex.getItemCount(); i++) {
cboSex.setSelectedIndex(i);
if(cboSex.getSelectedItem().toString().equals(sex))
{
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -