JAVAGUI设计

实现的功能

  1. 读入txt文档中员工的信息

  1. 读出并保存当前查询的员工信息到txt文档中

  1. 计算员工的得分,并在部门内部以及全公司进行排名

  1. 可以对员工的姓名进行模糊查询

  1. 对员工的部门进行精确查询

  1. 展现员工的考评结果

全部源代码

主类,用于GUI的设计与实现

package stu202000810247qiuxin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;


public class GUIsystem extends JFrame implements ActionListener {
    static ArrayList<GuiEmployee> employees = new ArrayList<GuiEmployee>();

    String[] jtableTitle = { "姓名", "部门", "业绩", "态度", "技能", "得分", "部门内排名", "全公司排名" };// 表头名
    String outfilenameMenu = "GUIdata/dataout.txt";
    StringBuffer output = new StringBuffer(); // 保存输出信息
    
    JFrame jframe = new JFrame("员工管理系统");
    JTextField t1 = new JTextField(20);

    DefaultTableModel tablemodel; // 表格模型
    JTable jtable;

    JRadioButton NameQuery = new JRadioButton("姓名查询", true);
    JRadioButton DepartmentQuery = new JRadioButton("部门查询", false);
    ButtonGroup bg = new ButtonGroup();

    JButton ButtonQuery = new JButton("查询");
    JButton analyseButton = new JButton("考评结果显示");
    JButton importFile = new JButton("导入文件");
    JButton exportFile = new JButton("导出文件");

    JMenuBar jmenu = new JMenuBar(); // 实例菜单栏
    JMenu file = new JMenu("文件"); // 实例一个菜单项
    JMenuItem file_save = new JMenuItem("保存");

    String[] DepartmentTypeJComboBox = { "默认查询", "信息部", "财务部", "行政部", "工程部" };
    JComboBox<String> DepartmentSelect = new JComboBox<String>(DepartmentTypeJComboBox);

    public GUIsystem(String title) {
        super(title);

        JPanel p1 = new JPanel();
        p1.add(t1);

        p1.add(NameQuery);
        p1.add(DepartmentQuery);
        p1.add(DepartmentSelect);
        p1.add(ButtonQuery);
        p1.add(analyseButton);
        p1.add(importFile);
        p1.add(exportFile);
        jframe.add(p1, BorderLayout.NORTH);

        bg.add(NameQuery);
        bg.add(DepartmentQuery);

        ButtonQuery.setActionCommand("查询"); // 设置组件行动命令(暗号)
        ButtonQuery.addActionListener(this);// 设置监听

        file_save.setActionCommand("保存信息"); // 设置组件行动命令(暗号)
        file_save.addActionListener(this);// 设置监听

        analyseButton.setActionCommand("考评结果显示"); // 设置组件行动命令(暗号)
        analyseButton.addActionListener(this);// 设置监听

        importFile.setActionCommand("导入文件"); // 设置组件行动命令(暗号)
        importFile.addActionListener(this);// 设置监听

        exportFile.setActionCommand("导出文件"); // 设置组件行动命令(暗号)
        exportFile.addActionListener(this);// 设置监听

        jframe.setJMenuBar(jmenu); // 设置菜单栏
        jmenu.add(file); // 添加菜单项
        file.add(file_save);

        // 设置JTable
        this.tablemodel = new DefaultTableModel(jtableTitle, 0);
        jtable = new JTable(tablemodel);
        jframe.getContentPane().add(new JScrollPane(jtable));
        // 设置表格内容颜色
        jtable.setForeground(Color.BLACK); // 字体颜色
        jtable.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式
        jtable.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色
        jtable.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景
        jtable.setGridColor(Color.GRAY); // 网格颜色

        // 设置表头
        jtable.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 设置表头名称字体样式
        jtable.getTableHeader().setForeground(Color.BLUE); // 设置表头名称字体颜色
        jtable.getTableHeader().setResizingAllowed(true); // 设置不允许手动改变列宽
        jtable.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列

        // 设置行高
        jtable.setRowHeight(30);

        // 第一列列宽设置为40
        jtable.getColumnModel().getColumn(0).setPreferredWidth(40);
        jtable.setPreferredScrollableViewportSize(new Dimension(400, 300));

        // 设置内容居中
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        dc.setHorizontalAlignment(JLabel.CENTER);
        jtable.setDefaultRenderer(Object.class, dc);

        // 设置jframe
        jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
        jframe.setLocation(200, 200);
        jframe.pack();
        jframe.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String str_t1 = t1.getText();// 读取文本框的内容
        // output.delete(0, output.length()); // 清空输出缓存区
        String actionCommond = e.getActionCommand();// 获取组件行动命令(暗号)

        if (actionCommond.equals("查询")) {
            tablemodel.setRowCount(0);
            // 计算得分
            ArrayList<GuiEmployee> stulist1 = new ArrayList<>();
            for (GuiEmployee ee : employees) {
                ee.Score = getTwoDecimal((Double.parseDouble(ee.Achievement)) * 0.4
                        + (Double.parseDouble(ee.Attitude)) * 0.3 + (Double.parseDouble(ee.Skill)) * 0.3) + "";
                stulist1.add(
                        new GuiEmployee(ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill, ee.Score));
            }
            NewMyCompareScore a = new NewMyCompareScore();
            Collections.sort(stulist1, a);

            // 计算整个公司排名与每个部门排名
            int gongshi = 1;
            int xinxi = 1;
            int caiwu = 1;
            int xingzheng = 1;
            int gongcheng = 1;
            for (GuiEmployee ee : stulist1) {
                ee.RankCompany = gongshi + "";
                gongshi++;
                if (ee.DepartmentType.equals("信息部")) {
                    ee.RankDepartmentType = xinxi + "";
                    xinxi++;
                } else if (ee.DepartmentType.equals("财务部")) {
                    ee.RankDepartmentType = caiwu + "";
                    caiwu++;
                } else if (ee.DepartmentType.equals("行政部")) {
                    ee.RankDepartmentType = xingzheng + "";
                    xingzheng++;
                } else if (ee.DepartmentType.equals("工程部")) {
                    ee.RankDepartmentType = gongcheng + "";
                    gongcheng++;
                }
            }
            /**************************
             * 默认输入为空,显示所有员工信息
             *****************************************/
            if (str_t1.isEmpty() && DepartmentSelect.getSelectedItem().equals("默认查询")) {
                tablemodel.setRowCount(0);
                for (GuiEmployee ee : stulist1)
                    this.tablemodel.addRow(ee.toStringList());
            }
            /**************************
             * 姓名的模糊查询
             *****************************************/
            else if (NameQuery.isSelected() && !str_t1.isEmpty()) {
                tablemodel.setRowCount(0);
                List<GuiEmployee> stulist2 = new ArrayList();
                for (GuiEmployee ee : stulist2) {
                    if (ee.Name.indexOf(str_t1) != -1)
                        this.tablemodel.addRow(ee.toStringList());
                }
            }
            /**************************
             * 部门的精细查询
             ***************************************/
            else if (DepartmentQuery.isSelected() && str_t1.isEmpty()) {
                tablemodel.setRowCount(0);
                for (GuiEmployee ee : stulist1) {
                    if (ee.DepartmentType.equals(DepartmentSelect.getSelectedItem()))
                        this.tablemodel.addRow(ee.toStringList());
                }

            }
            /****************************** 调出弹窗 ********************************/
        } else if (actionCommond.equals("考评结果显示")) {
            new MyDialogDemo();
        }
        /****************************** 保存文件内容 ********************************/
        else if (actionCommond.equals("保存信息")) {
            int rowcount = this.tablemodel.getRowCount(); // 获取JTable的行数
            // output.append("姓名_部门_电话_Email_211/985\n"); // 第一行
            for (int i = 0; i < rowcount; i++) {
                for (int j = 0; j < 8; j++)
                    output.append((String) (this.jtable.getValueAt(i, j)) + ",");

                output.delete(output.length() - 1, output.length()); // 把多余的','删除

                if (i != rowcount - 1) // 除了最后一行信息,换行符
                    output.append("\n");
            }
            GuiInfoOutput out = new GuiInfoOutput();
            try {
                out.writefile(outfilenameMenu, output.toString());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            
        }
        /*********************************导入文件*********************************/
        else if (actionCommond.equals("导入文件")) {
            FileDialog f1 = new FileDialog(jframe,"选择要导入的文件",FileDialog.LOAD);
            f1.setVisible(true);
            GuiInforLoader info = new GuiInforLoader();
            try {
                employees = info.readfile(f1.getDirectory()+f1.getFile());
                //JOptionPane.showMessageDialog(null,"导入成功!");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();JOptionPane.showMessageDialog(null,"导入失败!");
            }
            
        }
        /*********************************导出文件*********************************/
        else if (actionCommond.equals("导出文件")) {
            int rowcount = this.tablemodel.getRowCount(); // 获取JTable的行数
            // output.append("姓名_部门_电话_Email_211/985\n"); // 第一行
            for (int i = 0; i < rowcount; i++) {
                for (int j = 0; j < 8; j++)
                    output.append((String) (this.jtable.getValueAt(i, j)) + ",");

                output.delete(output.length() - 1, output.length()); // 把多余的','删除

                if (i != rowcount - 1) // 除了最后一行信息,换行符
                    output.append("\n");
            }
            FileDialog f2 = new FileDialog(jframe,"选择要保存的文件",FileDialog.LOAD);
            f2.setVisible(true);
            GuiInfoOutput out = new GuiInfoOutput();
            try {
                out.writefile(f2.getDirectory()+f2.getFile(), output.toString());
                //JOptionPane.showMessageDialog(null,"导出成功!");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
        }

    }

    public static void main(String[] args) throws IOException {
        GuiInforLoader info = new GuiInforLoader();
        String filename = "GUIdata/employeeUTF8.txt";
        employees = info.readfile(filename);
        new GUIsystem("");
    }

    /**
     * 将数据保留两位小数
     */
    private static double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
    }

    // 对话框
    private static class MyDialogDemo extends JDialog {

        String[] DiogjtableTitle = { "考评类别", "姓名", "部门", "业绩", "态度", "技能", "得分", "部门内排名", "全公司排名" };// 表头名
        DefaultTableModel Diogtablemodel; // 表格模型
        JTable Diogjtable;

        public MyDialogDemo() {
            this.setVisible(true);
            this.setBounds(100, 100, 1500, 400);
            this.setTitle("考评结果");
            //
            this.Diogtablemodel = new DefaultTableModel(DiogjtableTitle, 0);
            Diogjtable = new JTable(Diogtablemodel);
            this.getContentPane().add(new JScrollPane(Diogjtable));

            // 设置JTable
            this.Diogtablemodel = new DefaultTableModel(DiogjtableTitle, 0);
            Diogjtable = new JTable(Diogtablemodel);
            this.getContentPane().add(new JScrollPane(Diogjtable));
            // 设置表格内容颜色
            Diogjtable.setForeground(Color.BLACK); // 字体颜色
            Diogjtable.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式
            Diogjtable.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色
            Diogjtable.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景
            Diogjtable.setGridColor(Color.GRAY); // 网格颜色

            // 设置表头
            Diogjtable.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 设置表头名称字体样式
            Diogjtable.getTableHeader().setForeground(Color.BLUE); // 设置表头名称字体颜色
            Diogjtable.getTableHeader().setResizingAllowed(true); // 设置不允许手动改变列宽
            Diogjtable.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列

            // 设置行高
            Diogjtable.setRowHeight(30);

            // 第一列列宽设置为40
            Diogjtable.getColumnModel().getColumn(0).setPreferredWidth(40);
            Diogjtable.setPreferredScrollableViewportSize(new Dimension(400, 300));

            // 设置内容居中
            DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
            dc.setHorizontalAlignment(JLabel.CENTER);
            Diogjtable.setDefaultRenderer(Object.class, dc);
                package stu202000810247qiuxin;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

 public class GuiInforLoader {
    public ArrayList<GuiEmployee> readfile(String filename) throws IOException {            
        try {
            ArrayList<GuiEmployee> employeeList = new ArrayList<GuiEmployee>();
            FileInputStream fr = new FileInputStream(filename);
            BufferedReader bf = new BufferedReader(new InputStreamReader(fr,"UTF-8"));//缓冲区
            String str ;
            // 按行读取字符串  读取所有行
            bf.readLine(); // 去掉第一行
            while((str = bf.readLine()) != null) {
                String arr[] = str.split("_");
                
                GuiEmployee ee = new GuiEmployee();
                ee.Name = arr[0];
                ee.DepartmentType = arr[1];
                ee.Achievement = arr[2];
                ee.Attitude = arr[3];
                ee.Skill = arr[4];
                
                employeeList.add(ee);

            }
            bf.close();
            fr.close();
            return employeeList;

        } catch (FileNotFoundException e) {
            System.out.println("文件未找到");
        }
        return null;
    }
}
            
            Diogtablemodel.setRowCount(0);
            for (GuiEmployee ee : employees) {
                if (ee.Name.equals("韩耿")) {
                    String companyone[] = { "最优秀员工", ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill,
                            "97.4", "1", "1" };
                    this.Diogtablemodel.addRow(companyone);
                }
            }
            String A []= {};
            this.Diogtablemodel.addRow(A);
            for (GuiEmployee ee : employees) {
                if (ee.Name.equals("韩耿")) {
                    String companyone[] = { "信息部最优秀员工", ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill,
                            "97.4", "1", "1" };
                    this.Diogtablemodel.addRow(companyone);
                }
                if (ee.Name.equals("田前进")) {
                    String companyone[] = { "财务部最优秀员工", ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill,
                            "93.1", "1", "11" };
                    this.Diogtablemodel.addRow(companyone);
                }
                if (ee.Name.equals("王戈恩")) {
                    String companyone[] = { "行政部最优秀员工", ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill,
                            "95.4", "1", "5" };
                    this.Diogtablemodel.addRow(companyone);
                }
                if (ee.Name.equals("胡顺林")) {
                    String companyone[] = { "工程部最优秀员工", ee.Name, ee.DepartmentType, ee.Achievement, ee.Attitude, ee.Skill,
                            "95.5", "1", "4" };
                    this.Diogtablemodel.addRow(companyone);
                }
                
            }
            String B []= {};String c = " ";
            this.Diogtablemodel.addRow(B);
            String D[] = { "最优秀部门",c,"信息部",c,c,c,c,c,c};
            this.Diogtablemodel.addRow(D);
        }

    }
}

/* 外部比较器 比较得分 */
class NewMyCompareScore implements Comparator<GuiEmployee> {

    @Override
    public int compare(GuiEmployee o1, GuiEmployee o2) {
        return o2.Score.compareTo(o1.Score);
    }

}

读入文件类,用于数据的读入

package stu202000810247qiuxin;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

 public class GuiInforLoader {
    public ArrayList<GuiEmployee> readfile(String filename) throws IOException {            
        try {
            ArrayList<GuiEmployee> employeeList = new ArrayList<GuiEmployee>();
            FileInputStream fr = new FileInputStream(filename);
            BufferedReader bf = new BufferedReader(new InputStreamReader(fr,"UTF-8"));//缓冲区
            String str ;
            // 按行读取字符串  读取所有行
            bf.readLine(); // 去掉第一行
            while((str = bf.readLine()) != null) {
                String arr[] = str.split("_");
                
                GuiEmployee ee = new GuiEmployee();
                ee.Name = arr[0];
                ee.DepartmentType = arr[1];
                ee.Achievement = arr[2];
                ee.Attitude = arr[3];
                ee.Skill = arr[4];
                
                employeeList.add(ee);

            }
            bf.close();
            fr.close();
            return employeeList;

        } catch (FileNotFoundException e) {
            System.out.println("文件未找到");
        }
        return null;
    }
}

文件读出类,用于将数据保存

package stu202000810247qiuxin;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class GuiInfoOutput {
    public void writefile(String filename,String content) throws IOException {
        try {
            FileWriter fw = new FileWriter(filename);
            fw.write(content);
            System.out.println("文件保存成功,保存到:"+filename);
            fw.close();
        } catch (FileNotFoundException e) {
            System.out.println("文件未保存成功");
        }
    }
}

员工类

package stu202000810247qiuxin;

public class GuiEmployee {
    String DepartmentType;
    String Name;
    String Attitude;
    String Skill;
    String Achievement;
    String Score;
    String RankCompany;
    String RankDepartmentType;
    GuiEmployee(){
        
    }
    public String[] toStringList() {
        String[] info = new String[8];
        info[0] = Name;
        info[1] = DepartmentType;
        info[2] = Achievement;
        info[3] = Attitude;
        info[4] = Skill;
        info[5]=Score;
        info[6]=RankDepartmentType;
        info[7]=RankCompany;
        
        return info;
    }
    public GuiEmployee(String Name, String DepartmentType, String Achievement, String Attitude,String Skill,String Score) {
        this.Name = Name;
        this.DepartmentType = DepartmentType;
        this.Achievement = Achievement;
        this.Attitude = Attitude;
        this.Skill = Skill;
        this.Score = Score;
    }
    public GuiEmployee(String Name, String DepartmentType, String Achievement, String Attitude,String Skill,String Score,String RankDepartmentType,String RankCompany) {
        this.Name = Name;
        this.DepartmentType = DepartmentType;
        this.Achievement = Achievement;
        this.Attitude = Attitude;
        this.Skill = Skill;
        this.Score = Score;
        this.RankDepartmentType = RankDepartmentType;
        this.RankCompany = RankCompany;
    }
}

程序主页面

说明

源代码在eclipse2020上可以无任何修改地进行运行,需要自取

链接:https://pan.baidu.com/s/1S8sBxUd8msWZKtQtT_1Ecg

提取码:ayld

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值