Java 我如何将不同类中的代码关联起来,以使方法和操作事件彼此协同工作

Java 我如何将不同类中的代码关联起来,以使方法和操作事件彼此协同工作,java,swing,arraylist,enums,Java,Swing,Arraylist,Enums,首先,这基本上不是一个回复,因为我在这篇文章中问的问题与我之前的文章不同,我不删除这篇文章的唯一原因是因为我在等待一个已经回答的用户回答一个问题,因为他已经帮了很多忙,并且知道情况如何 TLDR;不是转载 我的结论是我有三个问题 1:如何在Employee类中使用company类的变量和arrayList 2:我应该如何使actionListeners正确运行 3:公司类中的方法正确吗?如果没有,我应该如何制作它们 检查上面段落中给出的链接,以获得对我的问题的提示。这里有一个链接到我的教授提供的

首先,这基本上不是一个回复,因为我在这篇文章中问的问题与我之前的文章不同,我不删除这篇文章的唯一原因是因为我在等待一个已经回答的用户回答一个问题,因为他已经帮了很多忙,并且知道情况如何

TLDR;不是转载

我的结论是我有三个问题

1:如何在Employee类中使用company类的变量和arrayList

2:我应该如何使actionListeners正确运行

3:公司类中的方法正确吗?如果没有,我应该如何制作它们

检查上面段落中给出的链接,以获得对我的问题的提示。这里有一个链接到我的教授提供的提示,这样你们就可以看到我想要达到的目的。我通常会从导师那里得到帮助,但他们现在不在。非常非常感谢你的帮助。我必须在今晚之前完成这件事。我知道这对这个网站来说有点太多了,但这是你能很好地了解正在发生的事情的唯一方法。我已经开始了一篇关于这个问题的帖子,并且更进一步了,但是没有人再回答我在那个帖子上的问题,所以我正在做另一篇

下面是名为GUI.java的驱动程序

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class GUI extends JFrame {

private JPanel employeePanel;

private JPanel buttonPanel2;
private JPanel positionPanel;

private JPanel namePanel2;
private JPanel buttonPanel1;
private JPanel upperLine;
private JPanel lowerLine;
private JPanel companyAndPresidentPanel;
private JPanel companyPanel;
private JRadioButton designButton;
private JRadioButton salesButton;
private JRadioButton manuButton;
private JTextField firstField;
private JTextField lastField;
private JLabel firstLabel;
private JLabel lastLabel;
private JLabel cNameLabel;
private JLabel presidentLabel;
private JLabel logo;
private ButtonGroup bGroup;


private static final long serialVersionUID = 1L;//adding default serial ID so class is "used"

/*
 * Position p;
    p=p.SALES;
    */

Company c;
Employee e;
private JButton addButton;
private JButton clearButton;
private JButton printButton;
private JButton newButton;
private JButton exitButton;

String companyName;

public GUI(){


    companyName=JOptionPane.showInputDialog("What is the name of this company", companyName);
    setTitle("Company Employees");
    setSize(425,450);
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main();
    subPanels();
    add(companyAndPresidentPanel, BorderLayout.NORTH);
    add(employeePanel,BorderLayout.CENTER);
    add(buttonPanel1, BorderLayout.SOUTH);

    setVisible(true);   


    //pack();

}
public void subPanels(){

    positionPanel = new JPanel();
    buttonPanel1 = new JPanel();
    buttonPanel2 = new JPanel();
    employeePanel = new JPanel();
    namePanel2= new JPanel();
    upperLine = new JPanel();
    lowerLine = new JPanel();
    employeePanel.setSize(new Dimension(100, 100));
    designButton = new JRadioButton("Design");
    salesButton = new JRadioButton("Sales");
    manuButton = new JRadioButton("Manufacturing");

     addButton = new JButton("Add Employee");
     clearButton = new JButton("Clear Button");
     printButton = new JButton("Print Company Employees");
     newButton = new JButton("New Company");
     exitButton = new JButton("Exit");



    firstField = new JTextField(10);
    lastField = new JTextField(10);

    firstLabel = new JLabel("First Name:");
    lastLabel = new JLabel("Last Name:");


    bGroup = new ButtonGroup();
    bGroup.add(designButton);
    bGroup.add(salesButton);
    bGroup.add(manuButton);


    positionPanel.setLayout(new FlowLayout());
    positionPanel.add(designButton);
    positionPanel.add(salesButton);
    positionPanel.add(manuButton);
    positionPanel.setBorder(BorderFactory.createTitledBorder("Position"));


    upperLine.add(printButton);
    lowerLine.add(newButton);
    lowerLine.add(exitButton);
    buttonPanel1.add(upperLine, BorderLayout.NORTH);
    buttonPanel1.add(lowerLine, BorderLayout.SOUTH);

    buttonPanel2.add(addButton);
    buttonPanel2.add(clearButton);



    namePanel2.setLayout(new GridLayout(2,2));
    namePanel2.add(firstLabel);
    namePanel2.add(firstField);
    namePanel2.add(lastLabel);
    namePanel2.add(lastField);


    employeePanel.setBorder(BorderFactory.createTitledBorder("Add Employee"));
    employeePanel.add(namePanel2, BorderLayout.NORTH);
    employeePanel.add(positionPanel, BorderLayout.CENTER);
    employeePanel.add(buttonPanel2, BorderLayout.SOUTH);
    employeePanel.setBorder(BorderFactory.createTitledBorder("Add Employee"));


    printButton.addActionListener(new aListener());
    addButton.addActionListener(new aListener());
    clearButton.addActionListener(new aListener());
    newButton.addActionListener(new aListener());
    exitButton.addActionListener(new aListener());

}
/*
 * if data manager.add employee.equals("too many design")
 * joption pane too many
 */
public void main(){



    companyAndPresidentPanel = new JPanel();
    companyPanel = new JPanel();
    presidentLabel = new JLabel("President:Amin Oskoui");
    cNameLabel = new JLabel("");
    logo = new JLabel("");
    cNameLabel.setText(companyName);

    ImageIcon myImage = new ImageIcon("src/company.png");
    logo.setIcon(myImage);

    companyPanel.add(logo);
    companyPanel.add(cNameLabel);

    companyAndPresidentPanel.setLayout(new GridLayout(2,1));
    companyAndPresidentPanel.add(companyPanel);
    companyAndPresidentPanel.add(presidentLabel);





}
private class aListener implements ActionListener {
    String fName;
    String lName;
    String position;
    Position position2;
    int nEmployees, nCompanies, nDesign, nSales, nManu;
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        if(e.getSource()==addButton){
            fName = firstField.getText();
            lName = lastField.getText();
            if(e.getSource() instanceof JRadioButton){
                if(manuButton.isSelected()){
                    position=(manuButton.getText());
                    position2.valueOf(position);
                }   
                else if(designButton.isSelected()){
                    position=(designButton.getText());
                    position2.valueOf(position);
                }
                else if(salesButton.isSelected()){
                    position=(salesButton.getText());
                    position2.valueOf(position);
                }
                c=new Company(nEmployees, nCompanies, nDesign, nSales, nManu);
                c.addEmployee(fName, lName, position2);
                c.printCompany();

            }
        }
        else if(e.getSource()==clearButton){
            firstField.setText("");
            lastField.setText("");
            bGroup.clearSelection();

        }
        else if (e.getSource()==printButton){
            JOptionPane.showMessageDialog(null,"The employee list");//I don't have this working for it to show the list so I dunno what to do here.
        }
        else if (e.getSource()== newButton){

        }
        else if (e.getSource()==exitButton){
            System.exit(0);

        }

    }

}
public static void main(String[] args)//main class
{
      new GUI();
}

}
下面是名为Company.java的数据管理器

import javax.swing.*;

import java.util.*;
public class Company {
private static final long serialVersionUID = 1L;//ensuring that the class corresponds with a serialized object

Position p;
Employee a;

private String companyName;//name of company
private String employeeName;
private String position;

final int maxCompanies = 2, maxEmployees = 7, maxSales = 1, maxDesign = 2, maxManufacturing = 4;

private static int numberOfCompanies;//the number of companies
private int numEmployees;//the number of employees
public int numDesign;//the number of employees in design
private int numManufacturing;// the number of employees in manufacturing
private int numSales;//the number of employees in sales

private ArrayList<Employee> employeeList;

public Company(Position position){
    this.p = position;

}
public Company(String cn){
    numEmployees = 0;
    numSales = 0;
    numDesign = 0;
    numManufacturing = 0;
    employeeList = new ArrayList<Employee>();
}
public Company(int ec, int nc, int nd, int ns,int nm) {
    numEmployees = ec;
    numberOfCompanies = nc;
    numDesign = nd;
    numSales = ns;
    numManufacturing = nm;
}
public String addEmployee(String fName, String lName, Position p) {
       String errorMessage;
       errorMessage = "It is one of the errors";
    switch (p) {
        case SALES:
            //if there's less than 1 salesman, add them to the list
            if (numSales < 1) {
                Employee employee = new Employee(fName, lName, p);
                employeeList.add(employee);
            }
            else {
                JOptionPane.showMessageDialog(null, "There is already a Sales representative.");

                }


        case DESIGN:
            if (numDesign < 2) {
                Employee employee2 = new Employee(fName, lName, p);
                employeeList.add(employee2);
            }
            else {
                JOptionPane.showMessageDialog(null, "There are already two design employees.");            
                }


        case MANUFACTURING:
            if (numManufacturing < 4){
                Employee employee2 = new Employee(fName, lName, p);
                employeeList.add(employee2);
            }
            else {
                JOptionPane.showMessageDialog(null, "There are already four manufacturers.");              
                }
        default:


    }
    return errorMessage;
    }
public static int getNumCompanies(){//return the number of companies 
    return numberOfCompanies;
}
public int getNumEmployees(){//get the number of employees
    return numEmployees;
}
public String printCompany(){//print the company with all of the positions
    String companyPrint;

    return companyName;
}
@Override
public String toString() {//converts everything to a string
    return "Company [position="  + ", companyName=" + companyName
            + ", employees=" + employeeList + ", numEmployees=" + numEmployees
            + ", numDesign=" + numDesign + ", numManufacturing="
            + numManufacturing + ", numSales=" + numSales + "]";
}

}
这是枚举类

//in Position.java
public enum Position {
DESIGN("Design"),
MANUFACTURING("Manufacturing"),
SALES("Sales");

private final String positionName;


private Position(String positionName) {
    this.positionName= positionName;
}


@Override
public String toString() {
    return positionName;
}
}

每个帖子你应该问一个问题,我将关注你的员工类:

  • 如前所述,构造函数已损坏
  • 您需要使用传递到构造函数中的参数并设置类字段。例如:

  • 您的Employee构造函数在其内部创建自身的另一个实例——您有,
    Employee Employee=new Employee(fName,lName,p2)
    在构造函数内部,它不应该存在(不确定您认为它有什么用途),并且会无限地导致递归,直到堆栈内存耗尽为止
  • Employee甚至不应该编译,因为构造函数中有一个方法
  • 员工不应具有addEmployee方法。这种类型的方法只对包含员工集合的类有用,而这是您的另一个类。它在员工类中的作用是什么?没有
  • 再次该类需要getter变量
  • 我将从Employee类中删除JOptionPane和任何用户交互。此类用于保存描述单个员工的数据,而不是用于与用户交互。该函数将由其他类执行
  • 如果这是我的代码,我会给Employee一个main方法,只是为了测试类。或者,您可以创建一个稍后删除的小类,比如TestEmployee,它拥有main方法并为您执行此操作
  • 解决这个问题和任何编码问题的关键是分而治之,一次一个地单独解决每个小问题,但你以前也听说过这一点
我建议你为其他每节课问其他问题,并集中精力于你的问题和课程一次一节课。您的问题中不应包含tldr代码或文本


编辑1
为了其他人的利益,这里有你其他问题的链接。我建议回答此问题的任何人阅读问题和评论,了解之前讨论的内容:

  • 删除的问题:

编辑2
为了其他人的进一步利益,以下是他的任务要求的开始:

CS103 36674计算机科学I项目5 2014年春季JUSTH

截止日期:2014年4月13日星期日晚上11:59

本程序测试的概念:

Static variable and methods
Multiple classes (Data Element/Data Structure/Data Manager/GUI driver)
Class relationships – Dependency and Aggregation
标签上的图像

Enumerated types
Layout Managers
Borders (bordered radio group)
Arraylists
你们决定成立两家公司来生产和销售两种不同的产品。你将成为每家公司的总裁。每家公司将雇佣两人设计产品,四人制造产品,一人销售产品。您的任务是编写一个GUI驱动的程序,输入您雇用的人员的姓名及其在公司的职位。您将输入第一家公司的员工姓名,然后输入第二家公司的员工姓名。你将展示一个标志,以你作为总裁的名字代表你的公司。此时,您的公司不会超过2家

操作

  • 首先要求用户输入第一个公司名称。将显示一个带有公司名称的徽标,其中包括您作为总裁的姓名
  • 用户将输入雇用人员的姓名,然后指出该人员将填补哪个职位。然后,用户选择添加员工按钮
  • 如果所选职位已填补(1个销售、2个设计或4个制造),将显示错误消息
  • 当用户选择清除按钮时,名字和姓氏字段将被清除
  • 当用户选择“打印员工”按钮时,将显示员工姓名及其职位
  • 当用户选择“新建公司”按钮时,将询问第二家公司的名称,并显示徽标、公司名称和您作为总裁的姓名。如果已经记录了两个公司,则会显示一条错误消息
  • 当用户关闭框架或按下退出按钮时,应用程序退出
规格

  • 枚举类-位置
  • 创建名为Position的枚举类型。有效值为销售、设计、制造。根据设计要求添加任何其他方法或实例变量
  • 数据元素–员工类Static variable and methods Multiple classes (Data Element/Data Structure/Data Manager/GUI driver) Class relationships – Dependency and Aggregation
    Enumerated types
    Layout Managers
    Borders (bordered radio group)
    Arraylists
    
    public class Foo {
       private int bar;
       private String baz;
    
       public Foo(int bar, String baz) {
          this.bar = bar; // set class fields with parameters
          this.baz = baz;
       }
    
       // getter field so other classes can get necessary info
       public int getBar() {
          return bar;
       }
    
       public String getBaz() {
          return baz;
       }
    
       @Override
       public String toString() {
          return "Foo [bar=" + bar + ", baz=" + baz + "]";
       }
    
       // main method solely for testing this Foo class
       public static void main(String[] args) {
          Foo foo = new Foo(3, "baz test");
          System.out.println(foo);
       }
    }