📄 departmentface.java
字号:
package face;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import code.QueryData;
import rule.Rule;
import displayposition.position;
import queue.Queue;
public class DepartmentFace extends JPanel implements ActionListener,position
{
QueryData depCode=new QueryData();
Rule rule=new Rule();
JTextField txtFind=new JTextField(8);
ImageIcon icon=new ImageIcon("Image/Query2.gif");
JButton btnFind=new JButton("查找",icon);
boolean bol;
DefaultTableModel m=new DefaultTableModel();
JTable table=new JTable(m);
Font font=new Font("隶书",Font.BOLD,15);
JScrollPane jsp=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//JLabel addName=new JLabel("部门名称");
JLabel queryName=new JLabel("查找的部门名称");
JTextField txtName =new JTextField(10);
JComboBox txtShangban=new JComboBox(this.ONTIME);
JComboBox txtXiaban=new JComboBox(this.OFFTIME);
JButton btnAdd=new JButton("添加");
JButton btnDel=new JButton("删除");
JButton btnEdit=new JButton("修改");
JButton btnSave=new JButton("保存");
JButton btnCancel=new JButton("取消");
Queue selectednum=new Queue(10);
Queue selectedname=new Queue(10);
String update[];
public DepartmentFace(){
JPanel p1=new JPanel();
p1.add(queryName);
p1.add(txtFind);
p1.add(btnFind);
btnFind.addActionListener(this);
table.setFont(font);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==table)
{
JTable tab=(JTable)e.getSource();
int i=tab.getSelectedRow();
if(i>=0)
{
selectednum.put(m.getValueAt(i,0).toString());
selectedname.put(m.getValueAt(i,1).toString());
}
update=new String[table.getColumnCount()];
for(int j=0;j<update.length;j++)
{
update[j]=table.getValueAt(table.getSelectedRow(),j).toString();
}
}
}
});
JPanel p2=new JPanel();
p2.add(txtName);
txtName.addFocusListener(new FocusAdapter(){
public void focusGained(FocusEvent e)
{
txtName.setForeground(Color.BLUE);
}
});
txtName.setForeground(Color.BLUE);
txtName.setText("在此输入部门名称");
p2.add(txtShangban);
p2.add(txtXiaban);
p2.add(btnAdd);
btnAdd.addActionListener(this);
p2.add(btnDel);
btnDel.addActionListener(this);
p2.add(btnEdit);
btnEdit.addActionListener(this);
p2.add(btnSave);
btnSave.addActionListener(this);
p2.add(btnCancel);
btnCancel.addActionListener(this);
btnSave.setVisible(false);
btnCancel.setVisible(false);
this.setLayout(new BorderLayout());
this.add(p1,BorderLayout.NORTH);
this.add(jsp,BorderLayout.CENTER);
this.add(p2,BorderLayout.SOUTH);
this.setBackground(Color.WHITE);
this.setVisible(true);
this.move((int)(this.MOVEWIDTH-700)/2,(int)(this.MOVEHEIGHT-400)/2);
displayColumnsName();
}
public void displayColumnsName()
{
ArrayList array=depCode.getColumnsName("Department");
Iterator idata=array.iterator();
while(idata.hasNext())
{
m.addColumn(idata.next());
}
}
public void displayDepartment()
{
while(m.getRowCount()>0)
{
m.removeRow(m.getRowCount()-1);
}
ArrayList array=depCode.getQueryData(txtFind.getText());
Iterator idata=array.iterator();
while(idata.hasNext())
{
m.addRow((Object[])idata.next());
}
}
public boolean insertDatainDepartment()
{
String s[]=new String[3];
s[0]=txtName.getText();
s[1]=(String)txtShangban.getSelectedItem();
s[2]=(String)txtXiaban.getSelectedItem();
int i=rule.ExcuteInsertDepartment(s);
txtFind.setText("");
if(i>0)
{
JOptionPane.showMessageDialog(this,"添加了'"+i+"'条记录","增加记录",JOptionPane.INFORMATION_MESSAGE);
displayDepartment();
txtName.setText("在此输入部门名称");
txtName.requestFocus(true);
return true;
}
else
return false;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnFind) displayDepartment();
if (e.getSource()==btnAdd){
if(txtName.getText().equals("")|txtName.getText().equals("在此输入部门名称"))
{
JOptionPane.showMessageDialog(this,"请输入部门名称","信息不足",JOptionPane.INFORMATION_MESSAGE);
txtName.requestFocus(true);
txtName.setText("在此输入部门名称");
return;
}
btnAdd.setVisible(false);
btnDel.setVisible(false);
btnEdit.setVisible(false);
btnSave.setVisible(true);
btnCancel.setVisible(true);
}
if(e.getSource()==btnCancel){
btnAdd.setVisible(true);
btnDel.setVisible(true);
btnEdit.setVisible(true);
btnSave.setVisible(false);
btnCancel.setVisible(false);
}
if(e.getSource()==btnSave)
{
if(insertDatainDepartment())
{
btnAdd.setVisible(true);
btnDel.setVisible(true);
btnEdit.setVisible(true);
btnSave.setVisible(false);
btnCancel.setVisible(false);
}
}
if(e.getSource()==btnDel)
{
if(selectedname.getlength()==0)
{
JOptionPane.showMessageDialog(this,"请选择一个或多个部门(点击所在行)","信息不足",JOptionPane.INFORMATION_MESSAGE);
return;
}
String sname="";
for(int j=0;j<selectedname.getlength();j++)
{
sname=sname+" "+selectedname.get(j);
}
if(JOptionPane.showConfirmDialog(this,"您要删除以下部门:"+sname,"删除记录",JOptionPane.WARNING_MESSAGE)==JOptionPane.YES_OPTION)
{
int sum=0;
for(int k=0;k<selectednum.getlength();k++)
{
int i=rule.ExcuteDeleteDepartment(selectednum.get(k));
sum=sum+i;
}
selectednum.clearAll();
selectedname.clearAll();
if(sum>0)
{
JOptionPane.showMessageDialog(this,"删除了'"+sum+"'条记录","删除记录",JOptionPane.INFORMATION_MESSAGE);
update=null;
displayDepartment();
}
}else
{
selectednum.clearAll();
selectedname.clearAll();
}
}
if(e.getSource()==btnEdit)
{
if(update!=null)
new UpdateDepartment(update,this);
}
}
}
class UpdateDepartment extends JFrame implements ActionListener,position
{
JLabel labID=new JLabel("部门编号:");
JLabel labName=new JLabel("部门名称:");
JLabel labOnTime=new JLabel("上班时间:");
JLabel labOffTime=new JLabel("下班时间:");
JTextField txtDepID=new JTextField(6);
JTextField txtDepName=new JTextField(15);
JComboBox combOntime=new JComboBox(this.ONTIME);
JComboBox combOfftime=new JComboBox(this.OFFTIME);
JButton btnOK=new JButton("确定");
JButton btnCancel =new JButton("取消");
Rule rule=new Rule();
DepartmentFace fa;
String updatedata[];
UpdateDepartment(String updatedata[],DepartmentFace fa)
{
super("部门信息修改");
this.fa=fa;
this.updatedata=updatedata;
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(new JLabel(" "));
p1.add(labID);
p1.add(txtDepID);
txtDepID.setText(updatedata[0]);
txtDepID.setFont(new Font("隶书",Font.BOLD,15));
txtDepID.setEditable(false);
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(new JLabel(" "));
p2.add(labName);
p2.add(txtDepName);
txtDepName.setText(updatedata[1]);
txtDepName.setFont(new Font("隶书",Font.BOLD,15));
JPanel p3=new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.add(new JLabel(" "));
p3.add(labOnTime);
p3.add(combOntime);
combOntime.setFont(new Font("隶书",Font.BOLD,15));
for(int i=0;i<combOntime.getItemCount();i++)
{
if(combOntime.getItemAt(i).toString().equals(updatedata[2]))
combOntime.setSelectedIndex(i);
}
JPanel p4=new JPanel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(new JLabel(" "));
p4.add(labOffTime);
p4.add(combOfftime);
combOfftime.setFont(new Font("隶书",Font.BOLD,15));
for(int i=0;i<combOfftime.getItemCount();i++)
{
if(combOfftime.getItemAt(i).toString().equals(updatedata[3]))
combOfftime.setSelectedIndex(i);
}
JPanel p5=new JPanel();
p5.add(btnOK) ;
btnOK.addActionListener(this);
p5.add(btnCancel);
this.getContentPane().setLayout(new GridLayout(5,1));
this.getContentPane().add(p1);
this.getContentPane().add(p2);
this.getContentPane().add(p3);
this.getContentPane().add(p4);
this.getContentPane().add(p5);
this.setSize(400,300);
this.getContentPane().setBackground(Color.WHITE);
this.move((int)(this.MOVEWIDTH-400)/2,(int)(this.MOVEHEIGHT-300)/2);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnOK)
{
String[] s={txtDepID.getText(),txtDepName.getText(),combOntime.getSelectedItem().toString(),combOfftime.getSelectedItem().toString()};
int i=rule.ExcuteUpdateDepartment(s);
if(i>0)
{
JOptionPane.showMessageDialog(this,"更新了'"+i+"'条记录","更新记录",JOptionPane.INFORMATION_MESSAGE);
fa.displayDepartment();
dispose();
}
}
else
{
dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -