?? employeedb.java
字號(hào):
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EmployeeDb
{
public static void main(String[] args)
{
EmployeeDbFrame frame = new EmployeeDbFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class EmployeeDbFrame extends JFrame
{
public EmployeeDbFrame()
{
setTitle("Employee Record");
//set the size for the frame
setSize(WIDTH,HEIGHT);
EmployeeDbPanel panel = new EmployeeDbPanel();
Container contain = getContentPane();
//add the panel on frame
contain.add(panel);
}
private static final int WIDTH = 500;//widows' width
private static final int HEIGHT = 620;//widows' height
}
class EmployeeDbPanel extends JPanel
{
private JLabel Title;
private JPanel LayPanel;
private JLabel NamLabel;
private JTextField NamText;
private JLabel IcnoLabel;
private JTextField IcnoText;
private JLabel GenLabel;
private JTextField GenText;
private JLabel DobLabel;
private JTextField DobText;
private JLabel AddrLabel;
private JTextField AddrText;
private JLabel ComLabel;
private JTextField ComText;
private JLabel DeptLabel;
private JTextField DeptText;
private JLabel PostLabel;
private JTextField PostText;
private JLabel SalaryLabel;
private JTextField SalaryText;
private JTextArea ShowArea;
private JTextArea ResuArea;
private JPanel BtnPanel;
private JButton AddBtn;
private JButton DelBtn;
private JButton SeaBtn;
private JButton ChangBtn;
private JButton CouBtn;
private JScrollPane ShowScrollPane;
private JScrollPane ResuScrollPane;
private int num = 0;
private String[][] EmpMess= new String[10][10];
public EmployeeDbPanel()
{
setLayout(new BorderLayout());
Title = new JLabel("Employee Record",JLabel.CENTER);
add(Title,BorderLayout.NORTH);
LayPanel = new JPanel(new GridLayout(10,2));
//new label and text field for show the information
NamLabel = new JLabel("Name");
LayPanel.add(NamLabel);
NamText = new JTextField("",10);
LayPanel.add(NamText);
IcnoLabel = new JLabel("IC No");
LayPanel.add(IcnoLabel);
IcnoText = new JTextField("",10);
LayPanel.add(IcnoText);
GenLabel = new JLabel("Gender");
LayPanel.add(GenLabel);
GenText = new JTextField("",6);
LayPanel.add(GenText);
DobLabel = new JLabel("DOB");
LayPanel.add(DobLabel);
DobText = new JTextField("",10);
LayPanel.add(DobText);
AddrLabel = new JLabel("Address");
LayPanel.add(AddrLabel);
AddrText = new JTextField("",10);
LayPanel.add(AddrText);
ComLabel = new JLabel("Commencing Date");
LayPanel.add(ComLabel);
ComText = new JTextField("",10);
LayPanel.add(ComText);
DeptLabel = new JLabel("Department");
LayPanel.add(DeptLabel);
DeptText = new JTextField("",10);
LayPanel.add(DeptText);
PostLabel = new JLabel("Position");
LayPanel.add(PostLabel);
PostText = new JTextField("",10);
LayPanel.add(PostText);
SalaryLabel = new JLabel("Salary");
LayPanel.add(SalaryLabel);
SalaryText = new JTextField("",10);
LayPanel.add(SalaryText);
//new button for action
ShowArea = new JTextArea();
ShowScrollPane = new JScrollPane(ShowArea);
ShowArea.append("Name IC No Gender DOB Address ComDate Department Position Salary\n");
LayPanel.add(ShowScrollPane);
ResuArea = new JTextArea();
ResuScrollPane = new JScrollPane(ResuArea);
LayPanel.add(ResuScrollPane);
add(LayPanel,BorderLayout.CENTER);
BtnPanel = new JPanel();
AddBtn = new JButton("Add");
BtnPanel.add(AddBtn);
DelBtn = new JButton("Delete");
BtnPanel.add(DelBtn);
SeaBtn = new JButton("Search");
BtnPanel.add(SeaBtn);
ChangBtn = new JButton("Change");
BtnPanel.add(ChangBtn);
CouBtn = new JButton("Average");
BtnPanel.add(CouBtn);
add(BtnPanel,BorderLayout.SOUTH);
AddBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//add message function
AddMessage();
//clear the text field
NamText.setText("");
IcnoText.setText("");
GenText.setText("");
DobText.setText("");
AddrText.setText("");
ComText.setText("");
DeptText.setText("");
PostText.setText("");
SalaryText.setText("");
}
});
//second is searching button action event
SeaBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//clear the ResuArea
ResuArea.setText("");
//Searching message function
SearchMess();
}
});
//third is deleting button action event
DelBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//deleting message funtion
DeletMess();
}
});
//four is changeing button action event
ChangBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
//changing message function
ChangeMess();
}
});
//last is count the average button action event
CouBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//clear the ResuArea
ResuArea.setText("");
//Counting the average and total Salary function
CountAver();
}
});
}
//add the employee message function
public void AddMessage()
{
String StrMess = new String();
//add the employee message
if(!NamText.getText().equals("")&&!IcnoText.getText().equals("")&&!GenText.getText().equals("")&&!DobText.getText().equals("")&&!AddrText.getText().equals("")&&!ComText.getText().equals("")&&!DeptText.getText().equals("")&&!PostText.getText().equals("")&&!SalaryText.getText().equals(""))
{
//add the message
StrMess = NamText.getText()+" "+IcnoText.getText()+" "+GenText.getText()+" "+DobText.getText()+" "+AddrText.getText()+" "+ComText.getText()+" "+DeptText.getText()+" "+PostText.getText()+" "+SalaryText.getText()+"\n";
//add the record in Empmess
EmpMess[num][0] = NamText.getText();
EmpMess[num][1] = IcnoText.getText();
EmpMess[num][2] = GenText.getText();
EmpMess[num][3] = DobText.getText();
EmpMess[num][4] = AddrText.getText();
EmpMess[num][5] = ComText.getText();
EmpMess[num][6] = DeptText.getText();
EmpMess[num][7] = PostText.getText();
EmpMess[num][8] = SalaryText.getText();
num++;
}
//show the message in show area
ShowArea.append(StrMess);
}
//search the employee message function
public void SearchMess()
{
//input searching name in this box
String SeaNam = JOptionPane.showInputDialog("Input you want to search name?");
String StrMess = new String();
//search the searching name
for(int i=0;i<num;i++)
{
if(SeaNam.equalsIgnoreCase(EmpMess[i][0]))
StrMess = EmpMess[i][0]+" "+EmpMess[i][1]+" "+EmpMess[i][2]+" "+EmpMess[i][3]+" "+EmpMess[i][4]+" "+EmpMess[i][5]+" "+EmpMess[i][6]+" "+EmpMess[i][7]+" "+EmpMess[i][8]+"\n";
}
//show the searching name message in Result area
ResuArea.append(StrMess);
}
//delete the employee message function
public void DeletMess()
{
//input deleting name in this box
String DeletNam = JOptionPane.showInputDialog("Input you want to delete name?");
String StrMess = new String();
//search the deleting name
for (int i=0;i<num;i++)
{
if(DeletNam.equalsIgnoreCase(EmpMess[i][0]))
{
for(int k=i;k<num-1;k++)
{
//move last number to prefer
EmpMess[i][0]=EmpMess[i+1][0];
EmpMess[i][1]=EmpMess[i+1][1];
EmpMess[i][2]=EmpMess[i+1][2];
EmpMess[i][3]=EmpMess[i+1][3];
EmpMess[i][4]=EmpMess[i+1][4];
EmpMess[i][5]=EmpMess[i+1][5];
EmpMess[i][6]=EmpMess[i+1][6];
EmpMess[i][7]=EmpMess[i+1][7];
EmpMess[i][8]=EmpMess[i+1][8];
}
//the number delete 1
num--;
}
}
//clear the Show area
ShowArea.setText("");
//update new date
ShowArea.append(" Name IC No Gender DOB Address ComDate Department Position Salary\n");
for(int i=0;i<num;i++)
{
StrMess = EmpMess[i][0]+" "+EmpMess[i][1]+" "+EmpMess[i][2]+" "+EmpMess[i][3]+" "+EmpMess[i][4]+" "+EmpMess[i][5]+" "+EmpMess[i][6]+" "+EmpMess[i][7]+" "+EmpMess[i][8]+"\n";
//show the new message
ShowArea.append(StrMess);
}
}
//change the employee message function
public void ChangeMess()
{
//input the changing name
String ChangNam = JOptionPane.showInputDialog("Input you want to change name?");
for(int i=0;i<num;i++)
{
if(ChangNam.equalsIgnoreCase(EmpMess[i][0]))
{
//input the new record
String NewNam = JOptionPane.showInputDialog("Input the new Name?");
EmpMess[i][0] = NewNam;
String NewIcno = JOptionPane.showInputDialog("Input the new ICNo?");
EmpMess[i][1] = NewIcno;
String NewGen = JOptionPane.showInputDialog("Input the new Gender?");
EmpMess[i][2] = NewGen;
String NewDob = JOptionPane.showInputDialog("Input the new Dob?");
EmpMess[i][3] = NewDob;
String NewAddr = JOptionPane.showInputDialog("Input the new Address?");
EmpMess[i][4] = NewAddr;
String NewCom = JOptionPane.showInputDialog("Input the new Commencing Date?");
EmpMess[i][5] = NewCom;
String NewDept = JOptionPane.showInputDialog("Input the new Department?");
EmpMess[i][6] = NewDept;
String NewPost = JOptionPane.showInputDialog("Input the new Position?");
EmpMess[i][7] = NewPost;
String NewSalary = JOptionPane.showInputDialog("Input the new Salary?");
EmpMess[i][8] = NewSalary;
}
}
String StrMess = new String();
//clear the Show area
ShowArea.setText("");
//update new date
ShowArea.append("Name IC No Gender DOB Address ComDate Department Position Salary\n");
for(int i=0;i<num;i++)
{
StrMess = EmpMess[i][0]+" "+EmpMess[i][1]+" "+EmpMess[i][2]+" "+EmpMess[i][3]+" "+EmpMess[i][4]+" "+EmpMess[i][5]+" "+EmpMess[i][6]+" "+EmpMess[i][7]+" "+EmpMess[i][8]+"\n";
//show the new message
ShowArea.append(StrMess);
}
}
//count the employee message function
public void CountAver()
{
//init the total salary;
int total = 0;
//count the total salary
for(int i=0;i<num;i++)
{
total +=Integer.parseInt(EmpMess[i][8]);
}
//show the total employee
ResuArea.append("Total Employee is :"+num+"\n");
//show the all employee total salary
ResuArea.append("Total salary is :"+total+"\n");
//show the all employee' average salary
ResuArea.append("Average salary is :"+total/num+"\n");
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -