欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java工作量统计系统

程序员文章站 2022-07-10 08:17:26
前言基于java GUI图形用户界面设计。该系统划分为四个包:File包主要包含读写文件的类,Frame包主要包含一些窗口的显示,Main包主要包含主类(启动系统),Users包主要包含计算这五个值和用户信息的类。一、主方法该方法用来启动程序。public static void main(String[] args) { new LoginFrame(); //创建登录窗口对象}二、实体类定义实体类:public class User {private String id;...

前言

基于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;
}

}

五、运行结果展示

合理数据展示:
Java工作量统计系统

不合理数据展示:
Java工作量统计系统

夜间模式:
Java工作量统计系统

总结

1.程序的优点:

利用了多线程的知识可以动态显示实时时间,做了简单的提示动画。该系统也明确分成了几个包,每个包对应着相应的用处。利用面对对象的思想去实现功能。可读性较好。 读文件的数据都是利用集合处理。

2.程序的不足:

没有使用设计模式,程序的维护性能、可拓展性较差。

本文地址:https://blog.csdn.net/weixin_46942836/article/details/112030384

相关标签: java gui