Java工作量統計系統

Grand.發表於2020-12-31

前言

基於java GUI圖形使用者介面設計。該系統劃分為四個包:File包主要包含讀寫檔案的類,Frame包主要包含一些視窗的顯示,Main包主要包含主類(啟動系統),Users包主要包含計算這五個值和使用者資訊的類。

一、主方法

該方法用來啟動程式。

public static void main(String[] args) {  
	new LoginFrame(); //建立登入視窗物件
}

二、實體類

定義實體類:

public class User {
private String id;			//員工工號
private String password;	//員工密碼
private String name;		//員工姓名
private String sex;			//員工性別
private double y;			//員工工作量
public User() {}			//無參構造
//五參構造
public User(String id, String password, String name, String sex, double y) {
	super();
	this.id = id;
	this.password = password;
	this.name = name;
	this.sex = sex;
	this.y = y;
}
//setter、getter方法
public String getID() {
	return id;
}
public void setID(String id) {
	this.id = id;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public double getY() {
	return this.y;
}
public void setY(double y) {
	this.y = y;
}
public String toString() {
	return "工號:"+this.id+"  "+"密碼:"+this.password+"  "+"姓名:"+this.name+"  "
+"性別:"+this.sex+"  "+"標準工作量:"+this.y;
}

}

三、視窗類

1.登入視窗

登入功能設計:該功能分為兩個方面。先是判斷一下輸入的資料問題(工號為空、密碼為空、工號密碼都為空、工號密碼超出指定位數、工號超出指定位數、密碼超出指定位數),會給出相應的警告提示和錯誤提示。然後是驗證工號和密碼,也是利用ArrayList集合來儲存檔案裡的物件,用for迴圈遍歷,根據傳入的工號和密碼進行比較,若一致即為登入成功,否則提示“工號或密碼錯誤!”。

public class LoginFrame extends JFrame{  
private static final long serialVersionUID = 1L;
//定義元件   
JButton button1,button2,button3;   //登入,退出,重置按鈕
JPanel panel1,panel2; //宣告皮膚 
JLabel label1,label2,label3; //工號,密碼,圖片 標籤 
JTextField textField;  //輸入賬號的文字框
JPasswordField passwordField;  //輸入密碼的密碼框  
Font font =new Font("楷體", Font.PLAIN, 20);//定義字型

//建構函式
public LoginFrame() { 
	setButton();  //設定按鈕
	setButtonColor();   //設定按鈕顏色
    setImage();   //設定圖片
    setLabel();  //設定標籤
    setField();  //設定輸入框
    setPanel();  //設定皮膚
    addListener();  //新增監聽
    setFrame();  //設定視窗布局
}

//設定按鈕
public void setButton(){
    button1=new JButton("註冊");  
    button1.setFont(font);
    
    button2=new JButton("登入"); 
    button2.setFont(font);
    
    button3=new JButton("重置");
    button3.setFont(font);
}

//設定按鈕顏色
public void setButtonColor() {
    Color buttonColor1=new Color(255 ,48 ,48);
    button1.setBackground(buttonColor1);
    
    Color buttonColor2=new Color(65 ,105 ,180);
    button2.setBackground(buttonColor2);
    
    Color buttonColor3=new Color(0 ,255 ,127);
    button3.setBackground(buttonColor3);
}
//設定圖片
public void setImage() {
	panel1=new JPanel();   

    ImageIcon imageIcon = new ImageIcon(LoginFrame.class.getResource("林氏集團.jpg")) ;//背景圖案  
    imageIcon.setImage(imageIcon.getImage().getScaledInstance(imageIcon.getIconWidth()+45,
    		imageIcon.getIconHeight()+45,Image.SCALE_DEFAULT)); 
    
    //建立標籤,並將圖片加在標籤裡
    JLabel lalbackground=new JLabel();
    lalbackground.setIcon(imageIcon);
    panel1.add(lalbackground);  
}

//設定標籤佈局
public void setLabel() {
    label1=new JLabel("工  號:");  
    label1.setBounds(20, 50, 60, 50);
    
    label1.setFont(font);
    label2=new JLabel("密  碼:");  
    
    label2.setBounds(20, 50, 60, 50);
    label2.setFont(font);
}

//設定輸入框
public void setField() {
    textField=new JTextField(20);  //設定工號文字框長度
    passwordField=new JPasswordField(20);  //設定密碼框長度
}

//設定皮膚
public void setPanel() {
    panel2=new JPanel();      
	//皮膚元件佈局
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER,35,40));//流式佈局
    panel2.add(label1);  
    panel2.add(textField);  

    panel2.add(label2);  
    panel2.add(passwordField);  
    
    panel2.add(button1);       
    panel2.add(button2);  
    panel2.add(button3);

    //加入到視窗中  
    this.add(panel1); 
    this.add(panel2);   
}

//按鈕監聽
public void addListener() {
	 //註冊按鈕事件監聽
    button1.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
			new RegistrationFrame();
		}
	});
    
    //登入按鈕事件監聽
    button2.addActionListener(new Verify());
    
 	//重置按鈕事件監聽
    button3.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			clearBox();
		}
	});
}

//設定視窗布局
public void setFrame() {
	 this.setLayout(new GridLayout(2,1));  //選擇GridLayout佈局管理器        
 	this.setTitle("工作量統計系統");        //框架標題       
 	this.setSize(400,475);   //大小為:400*475
 	this.setLocationRelativeTo(null);//視窗居中           
 	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //設定當關閉視窗時,保證JVM也退出 
 	this.setVisible(true);     //設定框架可見 
 	this.setResizable(false);   //不可伸縮 
}

 //定義內部類,實現驗證工號密碼
 class Verify implements ActionListener{
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
			if(!isEmpty()) {
				try {
					if(verifyMessage(textField.getText().trim(),passwordField.getText().trim())) {
						JOptionPane.showMessageDialog(null, "登入成功!");
						dispose();
						//多執行緒操作
						new Thread(new Runnable() {
							
							@Override
							public void run() {
								try {
									Thread.sleep(1000);
									new MainFrame(textField.getText().trim());
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}).start();   
					}else{
						JOptionPane.showMessageDialog(null, "工號或密碼錯誤!");
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

	}
}

//工號密碼驗證
public boolean verifyMessage(String id,String password) throws Exception{
	BufferedReader br=new BufferedReader(new FileReader("員工資訊.txt"));
	List<User>users=new ArrayList<User>();
	String str=new String();
	while((str=br.readLine())!=null) {
		User user=new User();
		String [] temp=str.split("  ");
		user.setID(temp[0].split(":")[1]);
		user.setPassword(temp[1].split(":")[1]);
		user.setName(temp[2].split(":")[1]);
		user.setSex(temp[3].split(":")[1]);
		user.setY(Double.parseDouble(temp[4].split(":")[1]));
		users.add(user);
	}
	br.close();
	for(User user:users) {
		if(user.getID().equals(id) && user.getPassword().equals(password)) {
			return true;
		}
	}
	return false;
}

//工號密碼判斷是否為空
public boolean isEmpty() {
	if(textField.getText().trim().length()==0&&new String(passwordField.getPassword()).trim().length()==0) {
		//trim():用於刪除頭尾的空白符
		JOptionPane.showMessageDialog(null,"工號密碼不允許為空","Error",JOptionPane.ERROR_MESSAGE);
		return true;
	}else if(new String(passwordField.getPassword()).trim().length()==0) {
		JOptionPane.showMessageDialog(null, "密碼不允許為空","Error",JOptionPane.ERROR_MESSAGE);
		return true;
	}else if(textField.getText().trim().length()==0) {
		JOptionPane.showMessageDialog(null,"工號不允許為空","Error",JOptionPane.ERROR_MESSAGE);
		return true;
	}else if(textField.getText().trim().length() > 20&&new String(passwordField.getPassword()).trim().length() > 20){
        JOptionPane.showMessageDialog(null, "工號密碼超出指定位數","Warning",JOptionPane.WARNING_MESSAGE);
        return true;
    }else if(new String(passwordField.getPassword()).trim().length() > 20) {
    	JOptionPane.showMessageDialog(null, "密碼超出指定位數","Warning",JOptionPane.WARNING_MESSAGE);
    	return true;
    }else if(textField.getText().trim().length() > 20) {
    	JOptionPane.showMessageDialog(null, "工號超出指定位數","Warning",JOptionPane.WARNING_MESSAGE);
    	return true;
    }
	return false;
}

//清空文字框和密碼框  
public void clearBox()  {  
    textField.setText("");  
    passwordField.setText("");  
}

//在控制檯輸出登入的時間
public void outPut() {
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	System.out.println("工號為:"+textField.getText()+" 在"+sdf.format(new Date())+"登入");
}

}

2.註冊視窗

註冊功能設計:功能主要分成兩部分。第一部分:判斷使用者輸入的資訊是否符合規則,如果所需填的資訊有一項為空、確認密碼跟所填密碼不符、沒有在選擇框上打勾表示同意《使用者協議》,這三種情況都不給於註冊。第二部分:判斷是否已經註冊過了,開啟員工資訊的文字檔案,並傳入所要註冊的工號,利用ArrayList集合進行物件儲存,利用for迴圈遍歷,根據傳入的工號和檔案裡每一個物件的工號進行比較,如果一樣就返回true,不一樣則返回false。如果使用者已經註冊了就提示使用者已註冊並清空所填資訊。如果沒有註冊過,用HashSet集合(原因:HashSet的內容不能相同,所以選用HashSet集合來儲存)來儲存使用者資訊,然後呼叫User類的toString()方法來按行寫入使用者資訊的文字檔案。

public class RegistrationFrame extends JFrame{

private static final long serialVersionUID = 1L;
//定義標籤
JLabel registeredLabel,idLabel,nameLabel,sexLabel,yLabel,passwordLabel,confirmLabel;
//定義按鈕
JButton registeredButton,backButton;
//定義選擇框
JCheckBox selectBox;
//定義視窗
JFrame registrationFrame=new JFrame("工作量統計系統註冊");
//定義輸入框
JTextField idTextField,nameTextField,sexTextField,yTextField,passwordTextField,confirmTextField;
//定義皮膚
JPanel topPanel,middlePanel,westPanel,eastPanel,buttomPanel,buttomLeftPanel,buttomRightPanel,
buttomUnderPanel,buttomOnPanel;
//定義字型
Font font = new Font("楷體", Font.PLAIN, 32);
Font font2 =new Font("楷體", Font.PLAIN,24);
Font font3 =new Font("楷體", Font.PLAIN,16);
//建立Set集合物件
Set<User>users=null;

//構造方法
public RegistrationFrame() {
	setTopPanel();  //設定頂部皮膚
	setMiddlePanel();  //設定中部皮膚
	setSide();  //設定兩邊皮膚
	setButtomPanel();  //設定底部皮膚
	setFrame();  //設定窗體佈局
	addListener();  //按鈕事件監聽
}

//設定頂部皮膚
public void setTopPanel() {
	registeredLabel=new JLabel("注         冊");
	registeredLabel.setFont(font);
	topPanel=new JPanel();
	topPanel.add(registeredLabel);
}

//設定中間皮膚
public void setMiddlePanel() {
	middlePanel=new JPanel();
	middlePanel.setLayout(new GridLayout(6,1));
	
	idLabel=new JLabel("工號:");
	idLabel.setFont(font2);
	middlePanel.add(idLabel);
	
	idTextField=new JTextField(10);
	idTextField.setFont(font2);
	middlePanel.add(idTextField);
	
	nameLabel=new JLabel("姓名:");
	nameLabel.setFont(font2);
	middlePanel.add(nameLabel);
	
	nameTextField=new JTextField(10);
	nameTextField.setFont(font2);
	middlePanel.add(nameTextField);
	
	sexLabel=new JLabel("性別:");
	sexLabel.setFont(font2);
	middlePanel.add(sexLabel);
	
	sexTextField=new JTextField(10);
	sexTextField.setFont(font2);
	middlePanel.add(sexTextField);
	
	yLabel=new JLabel("標準工作量:");
	yLabel.setFont(font2);
	middlePanel.add(yLabel);
	
	yTextField=new JTextField(10);
	yTextField.setFont(font2);
	middlePanel.add(yTextField);
	
	passwordLabel=new JLabel("設定密碼:");
	passwordLabel.setFont(font2);
	middlePanel.add(passwordLabel);
	
	passwordTextField=new JTextField(10);
	passwordTextField.setFont(font2);
	middlePanel.add(passwordTextField);
	
	confirmLabel=new JLabel("確認密碼:");
	confirmLabel.setFont(font2);
	middlePanel.add(confirmLabel);
	
	confirmTextField=new JTextField(10);
	confirmTextField.setFont(font2);
	middlePanel.add(confirmTextField);
}

//設定兩邊皮膚
public void setSide() {
	westPanel=new JPanel();
	eastPanel=new JPanel();
	westPanel.setPreferredSize(new Dimension(313, 0));
	eastPanel.setPreferredSize(new Dimension(313, 0));
}

//設定底部皮膚
public void setButtomPanel() {
	buttomPanel=new JPanel();
	buttomUnderPanel=new JPanel();
	buttomOnPanel=new JPanel();
	buttomLeftPanel=new JPanel();
	buttomRightPanel=new JPanel();
	
	buttomRightPanel.setPreferredSize(new Dimension(555, 0));
	selectBox=new JCheckBox("我已閱讀並同意《使用者協議》");
	selectBox.setFont(font3);
	
	buttomOnPanel.add(selectBox);
	buttomOnPanel.add(buttomRightPanel);
	
	registeredButton=new JButton("註冊");
	registeredButton.setFont(font);
	backButton=new JButton("返回");
	backButton.setFont(font);
	
	buttomUnderPanel.add(registeredButton);
	buttomUnderPanel.add(backButton);
	buttomUnderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 200, 3));
	
	buttomPanel.add(buttomOnPanel);
	buttomPanel.add(buttomUnderPanel);
	buttomPanel.setLayout(new GridLayout(2,1));
}

//設定視窗布局
public void setFrame() {
    Container con=getContentPane();//建立容器物件con,取得容器皮膚
    con.setLayout(new BorderLayout());//設定佈局為邊框佈局,邊框佈局分東南西北中5個方位來新增控制元件。
    con.add(topPanel, BorderLayout.NORTH);//頂部皮膚在北邊   
    con.add(middlePanel, BorderLayout.CENTER);//中間皮膚在中間
    con.add(westPanel, BorderLayout.WEST);//西邊皮膚在西間
    con.add(eastPanel, BorderLayout.EAST);//東邊皮膚在東間
    con.add(buttomPanel,BorderLayout.SOUTH); //底部皮膚在南邊
    setDefaultCloseOperation(EXIT_ON_CLOSE);//設定關閉操作
    setTitle("工作量統計系統");
    setSize(961,525);//視窗大小為961*525
    setLocationRelativeTo(null);//視窗居中 
    setResizable(false);   //不可伸縮 
    setVisible(true);//視窗視覺化
}

//按鈕事件監聽
public void addListener() {
	 //註冊按鈕事件監聽
    registeredButton.addActionListener(new Registered());
    
    //返回按鈕事件監聽
    backButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
			new LoginFrame();
		}
	});
}

//私有內部類,用於註冊
private class Registered implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(idTextField.getText().trim().equals("")||
				nameTextField.getText().trim().equals("")||
				sexTextField.getText().trim().equals("")||
				yTextField.getText().trim().equals("")||
				passwordTextField.getText().trim().equals("")||
				confirmTextField.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(null, "資訊未填完整,無法為您註冊!","Warning",JOptionPane.WARNING_MESSAGE);
		}else if(!passwordTextField.getText().trim().equals(confirmTextField.getText().trim())) {
			JOptionPane.showMessageDialog(null, "密碼確認錯誤,請重新確認密碼!","Warning",JOptionPane.WARNING_MESSAGE);
		}else if(!selectBox.isSelected()) {
			JOptionPane.showMessageDialog(null, "未閱讀並同意《使用者協議》,無法為您註冊!","Warning",JOptionPane.WARNING_MESSAGE);
		} else
			try {
				if(isVerificate(idTextField.getText().trim())) {
					JOptionPane.showMessageDialog(null, "該工號已註冊過,無需再註冊!","Error",JOptionPane.ERROR_MESSAGE);
					clear();
				}else {
					users=new HashSet<User>();
					add(idTextField.getText().trim(), passwordTextField.getText().trim(), 
							nameTextField.getText().trim(), sexTextField.getText().trim(), 
							Double.parseDouble(yTextField.getText().trim()));
					writeFile();
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
}

//判斷是否已註冊
public boolean isVerificate(String id) throws Exception {
	BufferedReader br=new BufferedReader(new FileReader("員工資訊.txt"));
	List<User>users=new ArrayList<User>();
	String str=new String();
	while((str=br.readLine())!=null) {
		User user=new User();
		String [] temp=str.split("  "); 
		user.setID(temp[0].split(":")[1]);
		user.setPassword(temp[1].split(":")[1]);
		user.setName(temp[2].split(":")[1]);
		user.setSex(temp[3].split(":")[1]);
		user.setY(Double.parseDouble(temp[4].split(":")[1]));
		users.add(user);
	}
	br.close();
	for(User user:users) {
		if(user.getID().equals(id)) {
			return true;
		}
	}
	return false;
}

//增加物件
public void add(String id, String password, String name, String sex, double y) {
	users.add(new User(id, password,name,sex,y));
}

//寫入檔案
public void writeFile() {
	try {
		BufferedWriter bw = new BufferedWriter(new FileWriter("員工資訊.txt",true));
		for(User user : users) {
			bw.write(user.toString());
			bw.write("\r\n");
		}
		bw.close();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		JOptionPane.showMessageDialog(null, "註冊成功。");
		clear();
	}
}

//清空操作
public void clear() {
	idTextField.setText("");
	nameTextField.setText("");
	sexTextField.setText("");
	yTextField.setText("");
	passwordTextField.setText("");
	confirmTextField.setText("");
	selectBox.setSelected(false);
}
//實現Set集合中的hashCode()方法
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((users == null) ? 0 : users.hashCode());
	return result;
}
	
//實現Set集合中的equals()方法
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	RegistrationFrame other = (RegistrationFrame) obj;
	if (users == null) {
		if (other.users != null)
			return false;
	} else if (!users.equals(other.users))
		return false;
	return true;
}
}

3.功能視窗

(1)計算功能設計:輸入相應的資料計算工作量並匯出到excel表。
(2)更改主題模式設計:將相應的元件更改主題模式(白天模式和夜間模式)下的顏色。

public class MainFrame extends JFrame{

private static final long serialVersionUID = 1L;

//建立類物件
Technology technology=new Technology();
Travel travel=new Travel();
Guidance guidance=new Guidance();
Contract contract=new Contract();
User user=new User();
ReadWorkload read=new ReadWorkload();
WriteFile write=new WriteFile();
//定義標籤
JLabel titleLabel,timeLabel,productLabel,technologyLabel,backgroundLabel,locationLabel,
customersLabel,daysLabel,numLabel,countLabel,y1Label,y2Label,y3Label,y4Label,yLabel;
//定義視窗
JFrame mainFrame=new JFrame("工作量統計系統");
//定義輸入框
JTextField customersTextField,daysTextField,numTextField,counTextField;
//定義按鈕
JButton calButton,backButton,tipButton,replaceButton;
//定義皮膚
JPanel onPanel1,onPanel2,underPanel1,underPanel2,topPanel,bottomPanel,leftPanel,
middlePanel,rightPanel;
//定義字型
Font font  =new Font("楷體", Font.PLAIN, 26);
Font font2 =new Font("楷體", Font.PLAIN,32);
Font font3 =new Font("楷體", Font.PLAIN,20);
//定義選擇框
JComboBox<String> productType = null; // 產品型別
JComboBox<String> technologyType=null; //技術型別
JComboBox<String> travelLocation=null; //出差地點
JComboBox<String> themeMode=null;//主題模式

//構造方法
public MainFrame(String id) {
	setTopPanel();  //設定頂部皮膚
	setMiddlePanel();  //設定中部皮膚
	setButtomPanel();  //設定底部皮膚
	setSide();  //設定兩邊皮膚
	setFrame();  //設定視窗布局
	
    //建立多執行緒物件       
    DisplayTime t = new DisplayTime();
    new Thread(t).start();//啟動執行緒
    
    addListener(); //按鈕事件監聽
   
    
    //檢視按鈕事件監聽
    tipButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String getY=yLabel.getText();
			String[] str=new String[2];
			str=getY.split(":");
			String temp=str[1];
			double y=Double.valueOf(temp.toString());
			try {
				double value=read.readWorkLoad(id);
				if(e.getSource() == tipButton && y>= value) {
					yLabel.setForeground(Color.green);
					yLabel.setText("Y:"+y);
				}else if(e.getSource() == tipButton && y < value) {
					yLabel.setForeground(Color.red);
					yLabel.setText("Y:"+y);
				}
				//多執行緒操作
				new Thread(new Runnable() {
				     @Override
				     public void run() {
				      for(int i = 26; i < 48; i++) {
				    	  yLabel.setFont(new Font("楷體", Font.PLAIN, i));
				    	  try {
				    		  Thread.sleep(10);
				    	  } catch (InterruptedException e) {
				    		  e.printStackTrace();
				       		}
				      	}
				      for(int j = 48; j > 26; j--) {
				    	  yLabel.setFont(new Font("楷體", Font.PLAIN,j));
				    	  try {
				    		  Thread.sleep(10);
				          } catch (InterruptedException e) {
				        	  e.printStackTrace();
				          }
				        }
				     }
				}).start();
				if(y < value) {
					double lack=numSwicth(value-y); //四捨五入保留兩位小數
					JOptionPane.showMessageDialog(null, "您達到標準工作量還缺少"+lack+"!","Warning",JOptionPane.WARNING_MESSAGE);
				}else {
					JOptionPane.showMessageDialog(null, "恭喜你,達到了標準工作量!");
				}
			}catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	});
}

//頂部皮膚佈局
public void setTopPanel() {
	titleLabel=new JLabel("當前是員工查詢工作量介面" );
	titleLabel.setFont(font2);
	
	timeLabel=new JLabel();
	timeLabel.setFont(font3);
	
	onPanel1=new JPanel();
	underPanel1=new JPanel();
	topPanel=new JPanel();
	
	onPanel1.add(titleLabel,JLabel.CENTER);
	underPanel1.add(timeLabel,JLabel.CENTER);
	
	topPanel.add(onPanel1);
	topPanel.add(underPanel1);
	
	topPanel.setLayout(new GridLayout(2,1));	
}

//設定中間皮膚
public void setMiddlePanel() {
	middlePanel=new JPanel();
	
	backgroundLabel=new JLabel("主題模式:");
	backgroundLabel.setFont(font);
	middlePanel.add(backgroundLabel);
	
	themeMode = new JComboBox<>(); 
	themeMode.setPreferredSize(new Dimension(100, 40));
	themeMode.addItem("白天模式");
	themeMode.addItem("夜間模式");
	middlePanel.add(themeMode);
	
	productLabel=new JLabel("產品型別:");
	productLabel.setFont(font);
	middlePanel.add(productLabel);
	
	productType = new JComboBox<>();
	productType.setPreferredSize(new Dimension(100, 40));
	productType.addItem("核心產品");
	productType.addItem("其他產品");
	middlePanel.add(productType);
	
	technologyLabel=new JLabel("技術型別:");
	technologyLabel.setFont(font);
	middlePanel.add(technologyLabel);
	
	technologyType = new JComboBox<>(); 
	technologyType.setPreferredSize(new Dimension(100, 40));
	technologyType.addItem("售前技術");
	technologyType.addItem("售後技術");
	middlePanel.add(technologyType);
	
	locationLabel=new JLabel("出差地點:");
	locationLabel.setFont(font);
	middlePanel.add(locationLabel);
	
	travelLocation=new JComboBox<>();
	travelLocation.setPreferredSize(new Dimension(100, 40));
	travelLocation.addItem("花都區內");
	travelLocation.addItem("廣州市內");
	travelLocation.addItem("廣東省內");
	travelLocation.addItem("省外");
	middlePanel.add(travelLocation);

	customersLabel=new JLabel("客戶數:");
	customersLabel.setFont(font);
	middlePanel.add(customersLabel);
	
	customersTextField=new JTextField(10);
	customersTextField.setFont(font);
	middlePanel.add(customersTextField);
	
	daysLabel=new JLabel("出差天數:");
	daysLabel.setFont(font);
	middlePanel.add(daysLabel);
	
	daysTextField=new JTextField(10);
	daysTextField.setFont(font);
	middlePanel.add(daysTextField);
	
	numLabel=new JLabel("新員工人數:");
	numLabel.setFont(font);
	middlePanel.add(numLabel);
	
	numTextField=new JTextField(10);
	numTextField.setFont(font);
	middlePanel.add(numTextField);
	
	countLabel=new JLabel("合作訂單數:");
	countLabel.setFont(font);
	middlePanel.add(countLabel);
	
	counTextField=new JTextField(10);
	counTextField.setFont(font);
	middlePanel.add(counTextField);

	middlePanel.setLayout(new GridLayout(8,1));   //把中間皮膚元件設定成8行1列的網格佈局
}

//設定底部皮膚
public void setButtomPanel() {
	underPanel2=new JPanel();
	underPanel2.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 3));//流式佈局
    
    calButton=new JButton("計算");
    calButton.setFont(font2);
    underPanel2.add(calButton);
    
    backButton=new JButton("返回");
    backButton.setFont(font2);
    underPanel2.add(backButton);
    
    tipButton=new JButton("檢視");
    tipButton.setFont(font2);
    underPanel2.add(tipButton);
    
    replaceButton=new JButton("更換主題");
    replaceButton.setFont(font2);
    underPanel2.add(replaceButton);
	
    
	onPanel2=new JPanel();
	onPanel2.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 4));//流式佈局
	
	y1Label=new JLabel("y1:0.0");
	y1Label.setFont(font);
	
	y2Label=new JLabel("y2:0.0");
	y2Label.setFont(font);
	
	y3Label=new JLabel("y3:0.0");
	y3Label.setFont(font);
	
	y4Label=new JLabel("y4:0.0");
	y4Label.setFont(font);
	
	yLabel=new JLabel("Y:0.0");
	yLabel.setFont(font);
	
	onPanel2.add(y1Label);
	onPanel2.add(y2Label);
	onPanel2.add(y3Label);
	onPanel2.add(y4Label);
	onPanel2.add(yLabel);
	
	//底部皮膚佈局
	bottomPanel=new JPanel();
	bottomPanel.add(onPanel2);
	bottomPanel.add(underPanel2);
	bottomPanel.setLayout(new GridLayout(2,1));
}

//設定兩邊皮膚
public void setSide() {
	leftPanel=new JPanel();
	rightPanel=new JPanel();
	
	//設定兩邊皮膚大小
	leftPanel.setPreferredSize(new Dimension(373, 0));
	rightPanel.setPreferredSize(new Dimension(373, 0));
}

//設定視窗布局
public void setFrame() {
	//視窗布局
    Container con=getContentPane();//建立容器物件con,取得容器皮膚
    con.setLayout(new BorderLayout());//設定佈局為邊框佈局,邊框佈局分東南西北中5個方位來新增控制元件。
    con.add(topPanel, BorderLayout.NORTH);//頂部皮膚在北邊   
    con.add(leftPanel,BorderLayout.WEST);//左邊皮膚在西邊
    con.add(rightPanel,BorderLayout.EAST);//右邊皮膚在東邊
    con.add(middlePanel, BorderLayout.CENTER);//中間皮膚在中間
    con.add(bottomPanel,BorderLayout.SOUTH); //底部皮膚在南邊
    setDefaultCloseOperation(EXIT_ON_CLOSE);//設定關閉操作
    setSize(1154,670);//視窗大小為1154*670
    setTitle("工作量統計系統");
    setLocationRelativeTo(null);//視窗居中 
    setResizable(false);   //不可伸縮 
    setVisible(true);//視窗視覺化
}

//按鈕監聽事件
public void addListener() {
	 //計算按鈕事件監聽
	calButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(customersTextField.getText().trim().equals("")||
					daysTextField.getText().trim().equals("")||
					numTextField.getText().trim().equals("")||
					counTextField.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "您的資訊未填寫完整,無法為您計算工作量!","Warning",JOptionPane.WARNING_MESSAGE);
			}else {
				calButton.addActionListener(new Calculation());
			}
		}
	});
    
    //返回按鈕事件監聽
    backButton.addActionListener(new ActionListener() {
    	
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
			new LoginFrame();
		}
	});
    
  //更換主題按鈕事件監聽
    replaceButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if("夜間模式".equals((String) themeMode.getSelectedItem())) {
				changeNight();
			}
			if("白天模式".equals((String) themeMode.getSelectedItem())) {
				changeDay();
			}
		}
	});
}

//私有內部類,實現計算
private class Calculation implements ActionListener{
	
	@Override
	public void actionPerformed(ActionEvent e) {
		calY1();
		calY2();	
		calY3();
		calY4();	
		calY();
    }
}
		
//私有類,實現時間的動態實時顯示,多執行緒操作
private class DisplayTime extends JFrame implements Runnable{							 
	
	private static final long serialVersionUID = 1L;
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 EEEE HH:mm:ss");//24小時制,精確到秒
	public void run(){
		while(true){
			timeLabel.setText(sdf.format(new Date()));
			try{
				Thread.sleep(1000);	//執行緒休眠一秒
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
}

//更換白天模式
public void changeDay() {
	this.topPanel.setBackground(Color.white);
	this.onPanel1.setBackground(Color.white);
	this.onPanel2.setBackground(Color.white);
	this.underPanel1.setBackground(Color.white);
	this.underPanel2.setBackground(Color.white);
	this.middlePanel.setBackground(Color.white);
	this.leftPanel.setBackground(Color.white);
	this.rightPanel.setBackground(Color.white);
	this.bottomPanel.setBackground(Color.white);
	this.backButton.setBackground(Color.white);
	this.tipButton.setBackground(Color.white);
	this.replaceButton.setBackground(Color.white);
	this.calButton.setBackground(Color.white);
	this.backgroundLabel.setForeground(Color.black);
	this.countLabel.setForeground(Color.black);
	this.customersLabel.setForeground(Color.black);
	this.daysLabel.setForeground(Color.black);
	this.locationLabel.setForeground(Color.black);
	this.numLabel.setForeground(Color.black);
	this.productLabel.setForeground(Color.black);
	this.technologyLabel.setForeground(Color.black);
	this.timeLabel.setForeground(Color.black);
	this.titleLabel.setForeground(Color.black);
	this.yLabel.setForeground(Color.black);
	this.y1Label.setForeground(Color.black);
	this.y2Label.setForeground(Color.black);
	this.y3Label.setForeground(Color.black);
	this.y4Label.setForeground(Color.black);
	this.backButton.setForeground(Color.black);
	this.calButton.setForeground(Color.black);
	this.tipButton.setForeground(Color.black);
	this.replaceButton.setForeground(Color.black);
}

//更換夜間模式
public void changeNight() {
	this.topPanel.setBackground(Color.black);
	this.onPanel1.setBackground(Color.black);
	this.onPanel2.setBackground(Color.black);
	this.underPanel1.setBackground(Color.black);
	this.underPanel2.setBackground(Color.black);
	this.middlePanel.setBackground(Color.black);
	this.leftPanel.setBackground(Color.black);
	this.rightPanel.setBackground(Color.black);
	this.bottomPanel.setBackground(Color.black);
	this.backButton.setBackground(Color.black);
	this.tipButton.setBackground(Color.black);
	this.replaceButton.setBackground(Color.black);
	this.calButton.setBackground(Color.black);
	this.backgroundLabel.setForeground(Color.white);
	this.countLabel.setForeground(Color.white);
	this.customersLabel.setForeground(Color.white);
	this.daysLabel.setForeground(Color.white);
	this.locationLabel.setForeground(Color.white);
	this.numLabel.setForeground(Color.white);
	this.productLabel.setForeground(Color.white);
	this.technologyLabel.setForeground(Color.white);
	this.timeLabel.setForeground(Color.white);
	this.titleLabel.setForeground(Color.white);
	this.yLabel.setForeground(Color.white);
	this.y1Label.setForeground(Color.white);
	this.y2Label.setForeground(Color.white);
	this.y3Label.setForeground(Color.white);
	this.y4Label.setForeground(Color.white);
	this.backButton.setForeground(Color.white);
	this.calButton.setForeground(Color.white);
	this.tipButton.setForeground(Color.white);
	this.replaceButton.setForeground(Color.white);
}

//計算y1
public void calY1() {
	String sale=(String) productType.getSelectedItem();
	String product=(String) technologyType.getSelectedItem();
	String num=customersTextField.getText().trim();
	int number=Integer.parseInt(num);
	double b=technology.getB(sale);
	double k0=technology.getK0(product);
	double k1=technology.getK1(number);
	String y1_=String.valueOf(numSwicth(technology.getY1(b, k0, k1)));
	double y1=Double.parseDouble(y1_);
	try {
		write.writeY1(b, k0, k1, y1);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	y1Label.setText("y1:"+y1);
	
}

//計算y2
public void calY2() {
	String area=(String) travelLocation.getSelectedItem();
	String days=daysTextField.getText().trim();
	int day=Integer.parseInt(days);
	double d1=travel.getD1(day);
	double d2=travel.getD2(area);
	String y2Temp=String.valueOf(numSwicth(travel.getY2(day, d1, d2)));
	double y2=Double.parseDouble(y2Temp);
	try {
		write.writeY2(day, d1, d2, y2);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	y2Label.setText("y2:"+y2);
}

//計算y3
public void calY3() {
	String num=numTextField.getText().trim();
	int number=Integer.parseInt(num);
	double e=guidance.getE(number);
	String y3Temp=String.valueOf(numSwicth(guidance.getY3(number, e)));
	double y3=Double.parseDouble(y3Temp);
	try {
		write.writeY3(number, e, y3);
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	y3Label.setText("y3:"+y3);
}

//計算y4
public void calY4() {
	String countTemp=counTextField.getText().trim();
	int count=Integer.parseInt(countTemp);
	double f=contract.getF(count);
	String y4Tmp=String.valueOf(numSwicth(contract.getY4(count, f)));
	double y4=Double.parseDouble(y4Tmp);
	try {
		write.writeY4(count, f, y4);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	y4Label.setText("y4:"+y4);
}

//計算Y
public void calY() {
	String getY1=y1Label.getText();
	String str1[]=new String[2];
	str1=getY1.split(":");
	double y1=Double.parseDouble(str1[1]);

	String getY2=y2Label.getText();
	String str2[]=new String[2];
	str2=getY2.split(":");
	double y2=Double.parseDouble(str2[1]);

	String getY3=y3Label.getText();
	String str3[]=new String[2];
	str3=getY3.split(":");
	double y3=Double.parseDouble(str3[1]);

	String gety4=y4Label.getText();
	String str4[]=new String[2];
	str4=gety4.split(":");
	double y4=Double.parseDouble(str4[1]);
	String YTemp=String.valueOf(numSwicth(y1+y2+y3+y4));
	double Y=Double.parseDouble(YTemp);
	try {
		write.writeY(Y);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	yLabel.setText("Y:"+Y);
}

//四捨五入
public double numSwicth(double num)
{
	return Math.round(num*100) / 100.0;
}
}

四、檔案類

1、寫入檔案

每查詢一次工作量就寫一次到excel上。

public class WriteFile {
File file = new File("工作量統計資料.xls");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
BufferedWriter output=null;
public void writeY1(double b,double k0,double k1,double y1)throws Exception {
    output = new BufferedWriter(new FileWriter(file,true));
    output.write("時間:"+sdf.format(new Date())+"\r\n");
    output.write("y1\t"+"(1×"+b+"+7)×"+k0+"×"+k1+"="+y1+"\r\n");
    output.close();
}
public void writeY2(int day,double d1,double d2,double y2)throws Exception {
	output = new BufferedWriter(new FileWriter(file,true));
    output.write("y2\t"+"2×"+day+"×"+d1+"×"+d2+"="+y2+"\r\n");
    output.close();
}
public void writeY3(int number,double e,double y3) throws Exception{
	output = new BufferedWriter(new FileWriter(file,true));
    output.write("y3\t"+"3×"+number+"×0.5"+"×"+e+"="+y3+"\r\n");
    output.close();
}
public void writeY4(int countInfo,double f,double y4)throws Exception {
	output = new BufferedWriter(new FileWriter(file,true));
    output.write("y4\t"+"4×"+countInfo+"×"+f+"="+y4+"\r\n");
    output.close();
}
public void writeY(double y) throws Exception{
    output = new BufferedWriter(new FileWriter(file,true));
    output.write("Y\t"+"y1+y2+y3+y4"+"="+y+"\r\n\n\n");
    output.close();
}

}

2、讀出檔案

從檔案中根據員工的工號找到標準工作量。

public class ReadWorkload {
public double readWorkLoad(String id) throws Exception{
	BufferedReader br=new BufferedReader(new FileReader("員工資訊.txt"));
	List<User>users=new ArrayList<User>();
	String str=new String();
	while((str=br.readLine())!=null) {
		User user=new User();
		String [] temp=str.split("  ");
		user.setID(temp[0].split(":")[1]);
		user.setPassword(temp[1].split(":")[1]);
		user.setName(temp[2].split(":")[1]);
		user.setSex(temp[3].split(":")[1]);
		user.setY(Double.parseDouble(temp[4].split(":")[1]));
		users.add(user);
	}
	br.close();
	for(User user:users) {
		if(user.getID().equals(id)) {
			return user.getY();
		}
	}
	return 0.0;
}

}

五、執行結果展示

合理資料展示:
在這裡插入圖片描述

不合理資料展示:
在這裡插入圖片描述

夜間模式:
在這裡插入圖片描述

總結

1.程式的優點:

利用了多執行緒的知識可以動態顯示實時時間,做了簡單的提示動畫。該系統也明確分成了幾個包,每個包對應著相應的用處。利用面對物件的思想去實現功能。可讀性較好。 讀檔案的資料都是利用集合處理。

2.程式的不足:

沒有使用設計模式,程式的維護效能、可擴充性較差。

相關文章