本文主要是介绍一次很水的java-GUI作品,职工管理系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
提示:本作品视图代码大部分使用Jformdesigner自动生成,使用idea运行没有任何问题,
特色:使用了序列化与反序列化的方法,使得职工的数据不会因为程序的停止,而造成数据丢失。
注意:视图代码的setTitle(“xxxxxx:xxxxxxx”);这里的xxxx可以修改为你想要的程序标题,登录界面的用户名为root,密码:123
1.视图代码:
(1)AddEmployeeView添加员工的界面代码
/** Created by JFormDesigner on Sat Apr 03 11:45:59 CST 2021*/package view;import Controller.*;
import model.Employee;
import model.Save;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;/*** @author Brainrain*/
public class AddEmployeeView extends JFrame {public AddEmployeeView() {initComponents();}private void CommitButtonActionPerformed(ActionEvent e) { //进行用户交互,添加用户信息// 添加员工信息try {new Write(new Employee(EmployName.getText().toString(),EmployBirthday.getText().toString(),EmploySalary.getText().toString(),EmployEmail.getText().toString()));JOptionPane.showMessageDialog(this,"成功添加!!!");} catch (EmailErrorException exception) {JOptionPane.showMessageDialog(this,"您输入的邮箱格式有误,请重新输入,例如:1234567@qq.com");} catch (BirthdayErrorException birthdayErrorException) {JOptionPane.showMessageDialog(this,"您输入的出生日期格式有误,请重新输入,例如:2001-01-01");} catch (NumFromatException numFromatException) {JOptionPane.showMessageDialog(this,"您输入的数字有误,请重新输入,");} catch (EmpNullException empNullException) {JOptionPane.showMessageDialog(this,"请不要输入空信息");} catch (NameException nameException) {JOptionPane.showMessageDialog(this,"您输入的名字为空!!");}}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentslabel1 = new JLabel();label2 = new JLabel();label3 = new JLabel();label5 = new JLabel();label6 = new JLabel();EmployBirthday = new JTextField();EmploySalary = new JTextField();EmployEmail = new JTextField();EmployName = new JTextField();CommitButton = new JButton();//======== this ========setTitle("xxxxxx:xxxxxxx");Container contentPane = getContentPane();//---- label1 ----label1.setText("\u57fa\u672c\u5de5\u8d44:");//---- label2 ----label2.setText("email:");//---- label3 ----label3.setText("\u51fa\u751f\u5e74\u6708:");//---- label5 ----label5.setText("\u59d3\u540d:");//---- CommitButton ----CommitButton.setText("\u63d0\u4ea4");CommitButton.addActionListener(e -> CommitButtonActionPerformed(e));GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(115, 115, 115).addGroup(contentPaneLayout.createParallelGroup().addComponent(label5).addComponent(label3).addComponent(label1).addComponent(label2)).addGap(60, 60, 60).addGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addComponent(CommitButton).addContainerGap(244, Short.MAX_VALUE)).addGroup(contentPaneLayout.createSequentialGroup().addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(EmployBirthday, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE).addComponent(EmployName, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE).addComponent(EmploySalary, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE).addComponent(EmployEmail, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE).addGap(171, 171, 171)))));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addContainerGap(57, Short.MAX_VALUE).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label5).addComponent(label6).addComponent(EmployName, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE).addComponent(EmployBirthday, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1).addComponent(EmploySalary, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2).addComponent(EmployEmail, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)).addGap(68, 68, 68).addComponent(CommitButton).addGap(132, 132, 132)));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JLabel label1;private JLabel label2;private JLabel label3;private JLabel label5;private JLabel label6;private JTextField EmployBirthday;private JTextField EmploySalary;private JTextField EmployEmail;private JTextField EmployName;private JButton CommitButton;// JFormDesigner - End of variables declaration //GEN-END:variables
}
(2)DeleteView(删除用户的界面)
/** Created by JFormDesigner on Sat Apr 03 14:01:29 CST 2021*/package view;import java.awt.*;
import Controller.Delete;
import Controller.EmptyException;
import Controller.NotFoundNoException;
import Controller.NotFoundNoException;import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;/*** @author Brainrain*/
public class DeleteView extends JFrame {public DeleteView() {initComponents();}private void DeleteButtonActionPerformed(ActionEvent e) { //进行用户交互,删除用户信息try {Delete.delete(EmployNo.getText().toString());JOptionPane.showMessageDialog(this,"删除成功");} catch (EmptyException exception) {JOptionPane.showMessageDialog(this,"员工表为空,无法删除");}catch (NotFoundNoException exception){JOptionPane.showMessageDialog(this,"无法查询到对应的员工号,请检查员工号");}}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentslabel2 = new JLabel();label6 = new JLabel();EmployNo = new JTextField();DeleteButton = new JButton();//======== this ========setTitle("xxxxxx:xxxxxxx");setMinimumSize(new Dimension(420, 280));Container contentPane = getContentPane();//---- label2 ----label2.setText("\u5de5\u53f7\uff1a");//---- DeleteButton ----DeleteButton.setText("\u5220\u9664");DeleteButton.addActionListener(e -> DeleteButtonActionPerformed(e));GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(115, 115, 115).addComponent(label2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE).addGroup(contentPaneLayout.createParallelGroup().addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE).addGap(171, 171, 171)).addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addComponent(EmployNo, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE).addGap(76, 76, 76)))).addGroup(contentPaneLayout.createSequentialGroup().addGap(154, 154, 154).addComponent(DeleteButton).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addContainerGap(15, Short.MAX_VALUE).addComponent(label6).addGap(67, 67, 67).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2).addComponent(EmployNo, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(DeleteButton).addGap(98, 98, 98)));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JLabel label2;private JLabel label6;private JTextField EmployNo;private JButton DeleteButton;// JFormDesigner - End of variables declaration //GEN-END:variables
}
(3)LoginView 登录界面
/** Created by JFormDesigner on Sat Apr 03 10:05:04 CST 2021*/package view;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/*** @author Brainrain*/
public class LoginView extends JFrame {public static void main(String[] args) {new LoginView().setVisible(true);}public LoginView() {initComponents();}private void ResetButtonActionPerformed(ActionEvent e) { //重置按钮的交互ResetValue(e);}private void ResetValue(ActionEvent e) { //重置用户输入的内容UserName.setText(" ");Password.setText(" ");}private void LoginButtonActionPerformed(ActionEvent e) {//登录按钮的交互Login(e);}private void Login(ActionEvent e) {String passwd=Password.getText().toString(); //获取用户输入的内容String user = UserName.getText().toString();if(passwd.equals("")||user.equals("")){JOptionPane.showMessageDialog(this,"用户名或密码不能为空");}else if(passwd.equals("123")&&user.equals("root")){JOptionPane.showMessageDialog(this,"登录成功!!!");this.dispose();new MainView().setVisible(true);}else JOptionPane.showMessageDialog(this,"用户名或密码错误");}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentslabel1 = new JLabel();label2 = new JLabel();label3 = new JLabel();UserName = new JTextField();Password = new JTextField();LoginButton = new JButton();ResetButton = new JButton();//======== this ========这里的内容应该是标题,但是程序一运行就会自动转换成编码setTitle("xxxxxx:xxxxxxx");setFont(new Font(Font.DIALOG, Font.BOLD, 12));Container contentPane = getContentPane();//---- label1 ----程序一运行汉字就会自动转换成编码label1.setText("\u804c\u5de5\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\u767b\u5f55\u754c\u9762");label1.setHorizontalAlignment(SwingConstants.CENTER);label1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));//---- label2 ----label2.setText("UserName:");//---- label3 ----label3.setText("Password:");//---- LoginButton ----LoginButton.setText("Login");LoginButton.addActionListener(e -> LoginButtonActionPerformed(e));//---- ResetButton ----ResetButton.setText("Reset");ResetButton.addActionListener(e -> ResetButtonActionPerformed(e));GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(141, 141, 141).addGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addComponent(label1, GroupLayout.PREFERRED_SIZE, 232, GroupLayout.PREFERRED_SIZE).addGap(0, 19, Short.MAX_VALUE)).addGroup(contentPaneLayout.createSequentialGroup().addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false).addComponent(label2, GroupLayout.Alignment.LEADING).addComponent(label3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(LoginButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(18, 18, 18).addGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addComponent(UserName, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)).addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addGap(0, 77, Short.MAX_VALUE).addComponent(ResetButton)).addComponent(Password)))).addContainerGap(116, Short.MAX_VALUE)));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(24, 24, 24).addComponent(label1, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2).addComponent(UserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(23, 23, 23).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label3).addComponent(Password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE).addGroup(contentPaneLayout.createParallelGroup().addComponent(ResetButton).addComponent(LoginButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(23, 23, 23)));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JLabel label1;private JLabel label2;private JLabel label3;private JTextField UserName;private JTextField Password;private JButton LoginButton;private JButton ResetButton;// JFormDesigner - End of variables declaration //GEN-END:variables
}
(3)MainView主窗口
/** Created by JFormDesigner on Sat Apr 03 11:12:05 CST 2021*/package view;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;/*** @author Brainrain*/
public class MainView extends JFrame {public MainView() {initComponents();}private void AddEmployeeButtonActionPerformed(ActionEvent e) {new AddEmployeeView().setVisible(true); //与添加员工按钮,进行交互}private void ShowEmployeeButtonActionPerformed(ActionEvent e) { //与展示员工信息按钮进行交互new ShowEmployeeView().setVisible(true);}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentsAddEmployeeButton = new JButton();label1 = new JLabel();ShowEmployeeButton = new JButton();//======== this ========setTitle("xxxxxx:xxxxxxx");Container contentPane = getContentPane();//---- AddEmployeeButton ----AddEmployeeButton.setText("\u6dfb\u52a0\u804c\u5de5\u4fe1\u606f");AddEmployeeButton.addActionListener(e -> AddEmployeeButtonActionPerformed(e));//---- label1 ----label1.setText("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");label1.setHorizontalAlignment(SwingConstants.CENTER);label1.setFont(new Font("\u534e\u6587\u65b0\u9b4f", Font.PLAIN, 24));//---- ShowEmployeeButton ----ShowEmployeeButton.setText("\u6d4f\u89c8\u804c\u5de5\u4fe1\u606f");ShowEmployeeButton.addActionListener(e -> ShowEmployeeButtonActionPerformed(e));GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(55, 55, 55).addComponent(AddEmployeeButton, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE).addComponent(ShowEmployeeButton, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE).addGap(76, 76, 76)).addGroup(contentPaneLayout.createSequentialGroup().addGap(155, 155, 155).addComponent(label1, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE).addContainerGap(172, Short.MAX_VALUE)));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(78, 78, 78).addComponent(label1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).addGap(34, 34, 34).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(AddEmployeeButton).addComponent(ShowEmployeeButton)).addContainerGap(176, Short.MAX_VALUE)));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JButton AddEmployeeButton;private JLabel label1;private JButton ShowEmployeeButton;// JFormDesigner - End of variables declaration //GEN-END:variables
}
(4)ShowEmployeeView职工信息展示窗口
/** Created by JFormDesigner on Sat Apr 03 12:03:23 CST 2021*/package view;import Controller.Count;
import Controller.EmptyException;
import Controller.ShowInfo;
import model.Employee;import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;/*** @author Brainrain*/
public class ShowEmployeeView extends JFrame {public ShowEmployeeView() {initComponents();}private void ConfigActionPerformed(ActionEvent e) { //与修改按钮进行交互new UpdateView().setVisible(true);}private void DeleteActionPerformed(ActionEvent e) {new DeleteView().setVisible(true);}//与删除按钮进行交互private void ShowActionPerformed(ActionEvent e) { //与显示全体员工按钮进行交互new ShowView().setVisible(true);}private void CountActionPerformed(ActionEvent e) { //与统计人数按钮进行交互try {int num = Controller.Count.count();JOptionPane.showMessageDialog(this,"目前人数为"+num+"!!");} catch (EmptyException emptyException) {JOptionPane.showMessageDialog(this,"目前人数为0!!");}}private void AvgSalActionPerformed(ActionEvent e) { //与求平均值按钮进行交互try {double avg = Controller.AvgSal.avgsal();JOptionPane.showMessageDialog(this,"基本工资的平均工资为:"+avg);} catch (EmptyException emptyException) {JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算平均工资");}}private void HighestSalEmpActionPerformed(ActionEvent e) { //与最高工资员工按钮进行交互try {Employee emp = Controller.HighestSal.high(); //得到最高工资员工的个人信息String string = "最高工资的员工信息如下\n工号:"+emp.getNo()+"\n姓名:"+emp.getName()+"\n出生年月:"+emp.getBirthDay()+"\n基本工资:"+emp.getSal()+"\nEmail:"+emp.getEmail();JOptionPane.showMessageDialog(this,string);} catch (EmptyException emptyException) {JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算平均工资");}}private void LowestSalEmpActionPerformed(ActionEvent e) { //与最低资员工按钮进行交互try {Employee emp = Controller.LowestSal.low();//得到最低工资员工的个人信息String string = "最低工资的员工信息如下\n工号:"+emp.getNo()+"\n姓名:"+emp.getName()+"\n出生年月:"+emp.getBirthDay()+"\n基本工资:"+emp.getSal()+"\nEmail:"+emp.getEmail();JOptionPane.showMessageDialog(this,string);} catch (EmptyException emptyException) {JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算平均工资");}}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentsmenuBar1 = new JMenuBar();menu3 = new JMenu();Count = new JMenuItem();AvgSal = new JMenuItem();HighestSalEmp = new JMenuItem();LowestSalEmp = new JMenuItem();Config = new JButton();Delete = new JButton();Show = new JButton();//======== this ========setTitle("xxxxxx:xxxxxxx");Container contentPane = getContentPane();//======== menuBar1 ========{//======== menu3 ========{menu3.setText("\u7edf\u8ba1");//---- Count ----Count.setText("\u7edf\u8ba1\u804c\u5de5\u4eba\u6570");Count.addActionListener(e -> CountActionPerformed(e));menu3.add(Count);//---- AvgSal ----AvgSal.setText("\u57fa\u672c\u5de5\u8d44\u5e73\u5747\u503c");AvgSal.addActionListener(e -> AvgSalActionPerformed(e));menu3.add(AvgSal);//---- HighestSalEmp ----HighestSalEmp.setText("\u6700\u9ad8\u5de5\u8d44\u804c\u5de5");HighestSalEmp.addActionListener(e -> HighestSalEmpActionPerformed(e));menu3.add(HighestSalEmp);//---- LowestSalEmp ----LowestSalEmp.setText("\u6700\u4f4e\u5de5\u8d44\u804c\u5de5");LowestSalEmp.addActionListener(e -> LowestSalEmpActionPerformed(e));menu3.add(LowestSalEmp);}menuBar1.add(menu3);//---- Config ----Config.setText("\u4fee\u6539");Config.addActionListener(e -> ConfigActionPerformed(e));menuBar1.add(Config);//---- Delete ----Delete.setText("\u5220\u9664");Delete.addActionListener(e -> DeleteActionPerformed(e));menuBar1.add(Delete);//---- Show ----Show.setText("\u663e\u793a\u5168\u4f53\u5458\u5de5\u4fe1\u606f");Show.addActionListener(e -> ShowActionPerformed(e));menuBar1.add(Show);}setJMenuBar(menuBar1);GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGap(0, 508, Short.MAX_VALUE));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGap(0, 332, Short.MAX_VALUE));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JMenuBar menuBar1;private JMenu menu3;private JMenuItem Count;private JMenuItem AvgSal;private JMenuItem HighestSalEmp;private JMenuItem LowestSalEmp;private JButton Config;private JButton Delete;private JButton Show;// JFormDesigner - End of variables declaration //GEN-END:variables
}
(5)ShowView将职工信息制成表格输出:
/** Created by JFormDesigner on Sun Apr 04 09:57:23 CST 2021*/package view;import Controller.EmptyException;
import Controller.ShowInfo;
import model.Employee;import java.awt.*;
import java.beans.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;/*** @author Brainrain*/
public class ShowView extends JFrame {public ShowView() {initComponents();SetTable();}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentsMessage = new JScrollPane();table1 = new JTable();//======== this ========setTitle("xxxxxx:xxxxxxx");Container contentPane = getContentPane();//======== Message ========{//---- table1 ----table1.setModel(new DefaultTableModel(new Object[][] {{null, null, "", null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},{null, null, null, null, null},},new String[] {"\u5de5\u53f7", "\u59d3\u540d", "\u51fa\u751f\u65e5\u671f", "\u57fa\u672c\u5de5\u8d44", "Email"}));Message.setViewportView(table1);}GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addContainerGap().addComponent(Message, GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE).addContainerGap()));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addContainerGap().addComponent(Message, GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE).addContainerGap()));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponentsSetTable();}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JScrollPane Message;private JTable table1;// JFormDesigner - End of variables declaration //GEN-END:variablesprivate void SetTable() //这里在显示职工信息的时候,用了Jtable,因此在这个地方设置jtable的内容{DefaultTableModel dft = (DefaultTableModel) table1.getModel();dft.setRowCount(0);try {ArrayList<Employee>arrayList = ShowInfo.showinfo();for(Employee emp : arrayList){Vector<Object> a = new Vector<>();a.add(emp.getNo());a.add(emp.getName());a.add(emp.getBirthDay());a.add(emp.getSal());a.add(emp.getEmail());dft.addRow(a);}} catch (EmptyException emptyException) {// JOptionPane.showMessageDialog(this,"该员工表为空");}}
}
(6)UpdateView更新职工信息窗口:
/** Created by JFormDesigner on Sat Apr 03 13:51:35 CST 2021*/package view;import Controller.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;/*** @author Brainrain*/
public class UpdateView extends JFrame {public UpdateView() {initComponents();}private void UpdateButtonActionPerformed(ActionEvent e) { //与更新员工按钮进行交互try {Update.update(EmployName.getText().toString(),EmployBirthday.getText().toString(),EmploySalary.getText().toString(),EmployEmail.getText().toString(),EmpNO.getText().toString());JOptionPane.showMessageDialog(this,"修改成功");} catch (EmptyException emptyException) {JOptionPane.showMessageDialog(this,"该员工表为空,请插入后再修改");}catch (EmailErrorException exception) {JOptionPane.showMessageDialog(this,"您输入的邮箱格式有误,请重新输入,例如:1234567@qq.com");} catch (BirthdayErrorException birthdayErrorException) {JOptionPane.showMessageDialog(this,"您输入的出生日期格式有误,请重新输入,例如:2001-01-01");} catch (NumFromatException numFromatException) {JOptionPane.showMessageDialog(this,"您输入的数字有误,请重新输入,");} catch (NotFoundNoException notFoundNoException) {JOptionPane.showMessageDialog(this,"查无此人,请核对工号");}}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponentslabel1 = new JLabel();label2 = new JLabel();label3 = new JLabel();label5 = new JLabel();label6 = new JLabel();EmployBirthday = new JTextField();EmploySalary = new JTextField();EmployEmail = new JTextField();EmployName = new JTextField();UpdateButton = new JButton();label4 = new JLabel();EmpNO = new JTextField();//======== this ========setTitle("xxxxxx:xxxxxxx");Container contentPane = getContentPane();//---- label1 ----label1.setText("\u57fa\u672c\u5de5\u8d44:");//---- label2 ----label2.setText("email:");//---- label3 ----label3.setText("\u51fa\u751f\u5e74\u6708:");//---- label5 ----label5.setText("\u59d3\u540d:");//---- UpdateButton ----UpdateButton.setText("\u66f4\u65b0");UpdateButton.addActionListener(e -> UpdateButtonActionPerformed(e));//---- label4 ----label4.setText("\u5de5\u53f7");GroupLayout contentPaneLayout = new GroupLayout(contentPane);contentPane.setLayout(contentPaneLayout);contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addGap(115, 115, 115).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addComponent(label5).addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(label1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(label2).addComponent(label4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(60, 60, 60).addGroup(contentPaneLayout.createParallelGroup().addGroup(contentPaneLayout.createSequentialGroup().addComponent(UpdateButton).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(EmployBirthday).addComponent(EmploySalary, GroupLayout.Alignment.LEADING).addComponent(EmployEmail).addGroup(GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup().addComponent(EmployName, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)).addComponent(EmpNO)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE).addGap(171, 171, 171)))));contentPaneLayout.setVerticalGroup(contentPaneLayout.createParallelGroup().addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup().addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label5).addComponent(label6).addComponent(EmployName, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE).addComponent(EmployBirthday, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1).addComponent(EmploySalary, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2).addComponent(EmployEmail, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)).addGap(1, 1, 1).addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label4).addComponent(EmpNO, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)).addGap(51, 51, 51).addComponent(UpdateButton).addGap(51, 51, 51)));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variablesprivate JLabel label1;private JLabel label2;private JLabel label3;private JLabel label5;private JLabel label6;private JTextField EmployBirthday;private JTextField EmploySalary;private JTextField EmployEmail;private JTextField EmployName;private JButton UpdateButton;private JLabel label4;private JTextField EmpNO;// JFormDesigner - End of variables declaration //GEN-END:variables
}
2.model(职工类,读数据,写数据)
(1)职工类:
package model;import Controller.BirthdayErrorException;
import Controller.EmailErrorException;
import Controller.NameException;
import Controller.NumFromatException;import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;public class Employee implements Serializable {private String No; //员工工号private String Name; //员工姓名private String BirthDay;//员工出生日期private String Sal; //员工工资private String Email; //员工邮箱static int i=0; //员工入职的序号public Employee() {}public Employee(String name, String birthDay,String sal, String email) throws EmailErrorException, BirthdayErrorException, NumFromatException, NameException {if(name.isEmpty()) //检查名字是否为空{throw new NameException();}if(!check_email(email)) //检查邮箱格式{throw new EmailErrorException();}if(!check_birthday(birthDay))//检查出生日期格式{throw new BirthdayErrorException();}if(!check_sal(sal))//检查工资格式{throw new NumFromatException();}No=CreateNo();Name = name;BirthDay = birthDay;Sal = sal;Email=email;}public static boolean check_sal(String sal) { //检查工资格式String format = "^[0-9]*$";return sal.matches(format);}public static boolean check_birthday(String birthDay) {//检查出生日期格式String format="^\\d{4}-\\d{1,2}-\\d{1,2}";return birthDay.matches(format);}public static boolean check_email(String email) {//检查邮箱格式String format = "[a-zA-Z0-9_]{1,}[@][a-z0-9]{2,3}[.][a-z]{2,3}";return email.matches(format);}private String CreateNo() //根据日期,入职顺序创建工号{ ArrayList<Employee> arrayList = ReadData.Read();//读取数据文件if(arrayList!=null && arrayList.size()!=0) /*如果此时数据文件里存在内容,为了确保员工编号不会重复,从数据文件中读取最后一个员工的工号的后两位*/{String temp_no = arrayList.get(arrayList.size()-1).getNo();i = Integer.parseInt(temp_no.substring(temp_no.length()-2,temp_no.length()));}Date date = new Date();//生成当前日期i++; // 计算员工编号最后两位SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");String o=null;if(i<10) //为了确保工号格式,做一下判断如果是个位数,在个位数前加0{o="0"+ Employee.i;}else o=Employee.i+"";String no = simpleDateFormat.format(date)+o;return no;}public String getNo() {return No;}public void setNo(String no) {No = no;}public String getName() {return Name;}public void setName(String name) {Name = name;}public String getBirthDay() {return BirthDay;}public void setBirthDay(String birthDay) {BirthDay = birthDay;}public String getSal() {return Sal;}public void setSal(String sal) {Sal = sal;}public String getEmail() {return Email;}public void setEmail(String email) {Email = email;}@Overridepublic String toString() {return "Employee{" +"No='" + No + '\'' +", Name='" + Name + '\'' +", BirthDay='" + BirthDay + '\'' +", Email='" + Email + '\'' +'}';}
}
// if(arrayList!=null && arrayList.size()!=0)
// {
// for(Employee e : arrayList)
// {
// if(o.equals(e.getNo().substring(6,7)))
// {
// i++;
// if(i<10)
// {
// o="0"+ Employee.i;
// }
// else o=Employee.i+"";
// break;
// }
// }
// }
(2)读取数据:
package model;import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;public class ReadData {public static ArrayList<Employee> Read() //文件反序列化{try {ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("Emps"));Object obj = objectInputStream.readObject();if(obj instanceof ArrayList){ArrayList<Employee> obj1 = (ArrayList<Employee>) obj;return obj1;}} catch (ClassNotFoundException | IOException e) {// e.printStackTrace();}return null;}}
(3)写入数据
package model;import java.io.*;
import java.util.ArrayList;
import java.util.Stack;public class Save {public static ArrayList<Employee>Emps = new ArrayList<>();public Save() {}public Save(Employee e) {refresh(e);}public static void refresh(Employee Emp){ //当文件为空或不存在时,使用该种方式添加Emps.add(Emp);Serializable();}public static void delete_refresh(ArrayList<Employee>Emp) //当文件存在时,使用这种方式添加{Emps=Emp;Serializable();}public static void Serializable() //文件序列化过程{try {ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Emps"));objectOutputStream.writeObject(Emps);objectOutputStream.flush();objectOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
3.处理数据(各类功能)
(1)求平均值:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class AvgSal {public static double avgsal() throws EmptyException {double sum=0;ArrayList<Employee> arrayList = ReadData.Read(); //读取数据文件,反序列化if(arrayList==null || arrayList.size()==0) //如果数据文件为空或不存在抛出异常{throw new EmptyException();}for(Employee e: arrayList) //计算平均值{sum+=Double.parseDouble(e.getSal());}return sum/arrayList.size();}
}
(2)记录员工人数:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class Count {public static int count() throws EmptyException {ArrayList<Employee> arrayList = ReadData.Read(); //获取数据文件,反序列化if(arrayList==null || arrayList.size()==0){throw new EmptyException();}return arrayList.size(); //返回arraylist的大小(人数)}}
(3)删除员工
package Controller;
import model.Employee;
import model.ReadData;
import model.Save;
import java.util.ArrayList;
public class Delete {public static void delete(String No) throws EmptyException, NotFoundNoException {ArrayList<Employee> arrayList = ReadData.Read(); //读取数据文件,反序列化if(arrayList==null || arrayList.size()==0){throw new EmptyException();}int flag=0;//flag的作用是,当删除的员工编号不存在时,及时抛出异常,for (Employee e : arrayList) { //通过遍历数据文件,找到需要删除的员工的编号,进行删除if (e.getNo().equals(No)) {flag=1;arrayList.remove(e);Save.delete_refresh(arrayList);//删除数据后,及时修改本地文件return;}}if(flag==0){throw new NotFoundNoException();}}
}
(4)得到最高工资的员工信息:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class HighestSal {public static Employee high() throws EmptyException {double max=-1;ArrayList<Employee> arrayList = ReadData.Read(); //读取数据文件,反序列化if(arrayList==null || arrayList.size()==0){throw new EmptyException();}Employee re=null;for(Employee e: arrayList) //遍历数据找出最高工资的员工{if(max<Double.parseDouble(e.getSal())){max = Double.parseDouble(e.getSal());re=e;}}return re; //返回该员工的信息}}
(5)得到最低工资的员工信息
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class LowestSal {public static Employee low() throws EmptyException {double min=Double.MAX_VALUE;ArrayList<Employee> arrayList = ReadData.Read();//读取数据文件,反序列化if(arrayList==null || arrayList.size()==0){throw new EmptyException();}Employee re=null;for(Employee e: arrayList)//遍历数据找出最低工资的员工{if(min>Double.parseDouble(e.getSal())){min = Double.parseDouble(e.getSal());re=e;}}return re; //返回员工信息}
}
(6)全体员工信息:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class ShowInfo {public static ArrayList<Employee> showinfo() throws EmptyException {ArrayList<Employee> arrayList = ReadData.Read();//读取数据文件,反序列化if(arrayList==null || arrayList.size()==0){throw new EmptyException();}return arrayList; //将数据返回与图形化界面交互}
}
(7)更新员工信息:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;import static model.Employee.*;public class Update {public Update() {}//更新员工信息public static void update(String name,String birthday,String sal,String email,String No) throws NotFoundNoException, EmptyException, EmailErrorException, BirthdayErrorException, NumFromatException {int flag=0; //flag的作用还是用来标记输入的员工ArrayList<Employee> arrayList = ReadData.Read(); //读取数据文件,反序列化if(arrayList==null || arrayList.size()==0) //数据为空或不存在时抛出异常{throw new EmptyException();}if(!email.isEmpty()&&!check_email(email)) //如果用户输入的邮箱不合法,则抛出异常{throw new EmailErrorException();}if(!birthday.isEmpty()&&!check_birthday(birthday)) //用户输入生日日期格式错误抛出异常{throw new BirthdayErrorException();}if(!sal.isEmpty()&&!check_sal(sal)) //如果用户输入的工资不是数字的话抛出异常{throw new NumFromatException();}Employee old_e = null;for(Employee e:arrayList) //遍历数据{if(e.getNo().equals(No)){old_e=e; //记录旧的数据方便对数据进行更新if(!name.isEmpty()) //当名字非空时,修改名字,下免得if语句的功能都是这样,保证用户可以对单一的数据进行// 修改{e.setName(name);}else if(!birthday.isEmpty()){e.setBirthDay(birthday);}else if(!sal.isEmpty()){e.setBirthDay(birthday);}arrayList.remove(old_e); //移除旧的数据arrayList.add(e); //添加修改后的数据Save.delete_refresh(arrayList);//对于本地文件进行更新操作return;}}if(flag==0){throw new NotFoundNoException();}}
}
(8)添加新员工:
package Controller;import model.Employee;
import model.ReadData;
import model.Save;import java.util.ArrayList;public class Write {Employee employee;public Write() {}public Write(Employee emp) throws EmpNullException {if(emp==null) //如果用户输入的用户信息为空。抛出异常{throw new EmpNullException();}ArrayList<Employee> arrayList = ReadData.Read(); //读取数据if(arrayList==null || arrayList.size()==0) /*如果添加的时候还没有员工数据,这时候直接通过save来添加,无需更新数据*/{new Save(emp);}else{arrayList.add(emp); //添加新数据Save.delete_refresh(arrayList);//将旧的数据更新覆盖}}
}
以下是各类异常:
package Controller;public class BirthdayErrorException extends Exception{public BirthdayErrorException (){}public BirthdayErrorException (String s){super(s);}
}
package Controller;public class EmailErrorException extends Exception{public EmailErrorException(){}public EmailErrorException(String s){super(s);}}
package Controller;public class EmpNullException extends Exception{public EmpNullException (){}public EmpNullException(String s){super(s);}
}
package Controller;public class EmptyException extends Exception {
public EmptyException(){}
public EmptyException(String s){super(s);}
}
package Controller;public class NameException extends Exception{public NameException (){}public NameException(String s){super(s);}
}
package Controller;public class NotFoundNoException extends Exception {public NotFoundNoException(){}public NotFoundNoException(String s){super(s);}}package Controller;public class NumFromatException extends Exception{public NumFromatException(){}public NumFromatException(String s){super(s);}
}
几张截图:
这篇关于一次很水的java-GUI作品,职工管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!