北邮JAVA高级语言程序设计(选修课)第六次图形化作业
要求描述:
设计图形界面,实现员工信息的录入和查询。
该界面上有菜单如下
EmployeeInfoInput(一级菜单)
–CommisionEmployee(二级菜单)
–BasePlusCommisionEmployee
Search(一级菜单)
—AverageEarningSearch(查询全部员工的平均工资)
点击EmployeeInfoInput菜单下的某个菜单,弹出对话框,可以录入该类员工的详细信息,保存到内存中。
点击Search菜单下的AverageEarningSearch子菜单,显示全部员工的平均工资。
注:只考虑CommisionEmployee和BasePlusCommisionEmployee两类员工的信息输入。
源代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
public class Enter
{
//我提前声明了一些按钮、标签以及输入框,让他们能被不同的内部类调用和修改
factory f = new factory();
public JButton btn1 = new JButton("EmployeeInfoInput");//一级菜单
public JButton btn2 = new JButton("Search");//一级菜单
public JButton btn3 = new JButton("退出系统");//一级菜单
public JButton btn4 = new JButton("CommisionEmployee类员工");//二级菜单
public JButton btn5 = new JButton("BasePlusCommisionEmployee类员工");//二级菜单
public JButton btn6 = new JButton("返回");//二级菜单
public JButton btn7 = new JButton("完成");//三级菜单CommisionEmployee类员工
public JButton btn8 = new JButton("返回");//二级菜单
public JButton btn9 = new JButton("完成");//三级菜单BasePlusCommisionEmployee类员工
public JButton btn10 = new JButton("AverageEarningSearch");//二级菜单
public JButton btn11 = new JButton("返回");//二级菜单
public JButton btn12 = new JButton("ShowInfo");//二级菜单
public JButton btn13 = new JButton("返回");//三级菜单ShowInfo
JTextField text2 = new JTextField(25);//三级菜单CommisionEmployee类员工
JTextField text1 = new JTextField(25);//三级菜单BasePlusCommisionEmployee类员工
String ave2;
JLabel label = new JLabel();
//编写Enter()的构造方法
public Enter()
{
//先来实现主界面的设计
JFrame window1 = new JFrame("员工信息的录入和查询");
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口的同时结束整个程序
window1.setSize(480, 600);
window1.setLocation(720, 240);
window1.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
window1.setVisible(true);
window1.add(btn1);
window1.add(btn2);
window1.add(btn3);
//然后对主界面的三个按钮进行监听
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new CommisionEmployee();
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AverageEarningSearch();
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window1.dispose();
}
});
}
public class CommisionEmployee
{
public CommisionEmployee()
{
//其实创建不同的页面形式的大同小异,可以再声明一个内部类专门处理页面的创建
//这样的代码思路更清晰,看起来也更好看
//但我已经写完了...所以我懒得改了,直接Ctrl c + Ctrl v
JFrame window2 = new JFrame("CommisionEmployee");
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window2.setSize(480, 600);
window2.setLocation(720, 240);
window2.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
window2.setVisible(true);
window2.add(btn4);
window2.add(btn5);
window2.add(btn6);
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new InputCommisionEmployee();
}
});
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new InputBasePlusCommisionEmployee();
}
});
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window2.dispose();
}
});
}
}
public class InputCommisionEmployee
{
public InputCommisionEmployee()
{
JFrame window3 = new JFrame("InputCommisionEmployee");
window3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window3.setSize(480, 600);
window3.setLocation(720, 240);
window3.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
window3.setVisible(true);
window3.add(new JLabel("请输入员工信息形式如下:"));
window3.add(new JLabel("gzo zihan 2018211031 7000 0.1"));
window3.add(text2);
window3.add(btn7);
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//这里省略了Employee类,直接处理好数据存入Hashmap中
String line = text2.getText();
String[] str = line.split(" ");
double a = Double.parseDouble(str[3]);
double b = Double.parseDouble(str[4]);
String c = Double.toString(a*b);
f.addEmployee(str[2],c);
window3.dispose();
}
});
}
}
public class Search
{
public Search()
{
JFrame window4 = new JFrame("CommisionEmployee");
window4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window4.setSize(480, 600);
window4.setLocation(720, 240);
window4.setVisible(true);
window4.add(new JLabel("全部员工的平均工资为:"));
ave2 = Double.toString(f.Average());
label.setText(ave2);
//这里的label标签要声明在Enter类的全局变量中,不然是不会动态显示的
//我最一开始不知道,浪费了好多时间QwQ
window4.add(label);
window4.add(btn8);
window4.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window4.dispose();
}
});
}
}
public class InputBasePlusCommisionEmployee
{
public InputBasePlusCommisionEmployee()
{
JFrame window5 = new JFrame("InputCommisionEmployee");
window5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window5.setSize(480, 600);
window5.setLocation(720, 240);
window5.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
window5.setVisible(true);
window5.add(new JLabel("请输入员工信息形式如下:"));
window5.add(new JLabel("gzo zihan 2018211031 7000 0.1 5000"));
window5.add(text1);
window5.add(btn9);
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String line = text1.getText();
String[] str = line.split(" ");
double a = Double.parseDouble(str[3]);
double b = Double.parseDouble(str[4]);
double c = Double.parseDouble(str[5]);
String d = Double.toString(a*b+c);
f.addEmployee(str[2],d);
window5.dispose();
}
});
}
}
public class AverageEarningSearch
{
public AverageEarningSearch()
{
JFrame window6 = new JFrame("AverageEarningSearch");
window6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window6.setSize(480, 600);
window6.setLocation(720, 240);
window6.setLayout(new FlowLayout(FlowLayout.CENTER,200,100));
window6.setVisible(true);
window6.add(btn10);
window6.add(btn12);
window6.add(btn11);
btn10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Search();
}
});
btn11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window6.dispose();
}
});
//这里我加了一点小功能,显示已经输入的员工的编号和工资
//其实我是想加名字的,但map只储存了编号和工资,就懒得再去处理名字了
//我觉得可以在Enter的构造方法前声明两个数组来分别储存firstName和lastName
//然后在显示的时候加上去
btn12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ShowInfo();
}
});
}
public class ShowInfo
{
public ShowInfo()
{
JFrame window7 = new JFrame("ShowInfo");
window7.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window7.setSize(480, 600);
window7.setLocation(720, 240);
window7.setLayout(new FlowLayout(FlowLayout.CENTER,100,50));
window7.setVisible(true);
String[] Info = {"socialNumber","Earning"};
String[] data1 = f.Number();
String[] data2 = f.Money();
int num = f.one();
String[][] base;
base = new String[10][10];
for(int j = 0;j < num;j++)
{
base[j][0] = data1[j];
base[j][1] = data2[j];
}
//这里用了JTable的构造方法之一,比另一个简便
//base数组储存了表格信息,Info数组储存了表格名称
JTable table = new JTable(base,Info);//这里用了JTable的构造方法之一,比另一个简便
//一定要加这个JScrollPane(添加滚动条),不然表格名称无法显示
//我也不知道为什么非要加....反正加了就能显示
JScrollPane ScrollPane = new JScrollPane(table);
window7.add(ScrollPane );
window7.add(btn13);
btn13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window7.dispose();
}
});
}
}
}
//调用main()函数
public static void main(String[] args) {
new Enter();
}
}
//声明一个factory类来储存员工编码和工资并实现相应的一些操作
class factory
{
HashMap<String, String>employees = new HashMap<>();
public void addEmployee(String num, String money)
{
employees.put(num, money);
}
void printEmployees()
{
for (String num : employees.keySet())
{
System.out.println(Double.parseDouble(employees.get(num)));
}
}
//这里我用了笨办法来获得员工编号和工资并返回储存的数组
public String[] Number()
{
String[] data1;
data1 = new String[10];
int i = 0;
for (String num : employees.keySet())
{
data1[i] = num;
i++;
}
return data1;
}
public String[] Money()
{
String[] sum;
sum = new String[10];
int i = 0;
for (String num : employees.keySet())
{
sum[i] = employees.get(num);
i++;
}
return sum;
}
//计算员工的平均工资并返回
double Average()
{
double[] sum;
//这里我只设计了容量为11的数组来储存员工工资
//也就是说最大只能计算11个人的平均工资,再多数组就会越界报错了
sum = new double[10];
int i = 0;
double total = 0.0;
double AVE = 0.0;
for (String num : employees.keySet())
{
sum[i] = Double.parseDouble(employees.get(num));
i++;
}
for(int j = 0;j < i;j++)
{
total = total + sum[j];
}
AVE = total / Double.valueOf(i);
return AVE;
}
//计算已经输入的员工个数并返回个数值
int one()
{
int i = 0;
for (String num : employees.keySet())
{
i++;
}
return i;
}
}