繁体   English   中英

具有另一个类的Jbutton打印内容

[英]have a Jbutton print content from another class

我的addEmployee方法应该一次检查只有1个销售人员,2个设计师和4个制造人员,并且将检查公司中不超过7名员工,并且该方法将返回一个String指示错误是什么(即“该公司中已经有一个销售人员”),如果添加了员工,则为null,但是我的if语句不起作用,并且由于某种原因,它不允许我通过说“不返回结果无效”,但强迫我使用输入对话框。

我的印刷公司方法似乎有问题,但我不知道是什么,因为它仅返回null而不是打印出名称和位置的数组列表。

我的问题是如何解决此问题,以便我在gui面板中输入的任何内容都显示在joptionpane中,直到现在我得到的joption窗格为空。

这是代码:

public String addEmployee(String fName, String lName, String pos) {
    String message = null;

//  if (pos.equals("DESIGN")&&pos.equals("SALES")&&pos.equals("MANUFACTURING")&& numDesign==0 &&numSales==0&&numManufacturing==0)
    if (pos.equals("DESIGN")&&pos.equals("SALES")&&pos.equals("MANUFACTURING")&& numDesign==0 &&numSales==0&&numManufacturing==0)
    {
         return JOptionPane.showInputDialog(null,"woow ",JOptionPane.ERROR_MESSAGE);
    }
    if (pos.equals("DESIGN")&& numDesign==1&&numSales!=2&&numManufacturing!=4)
    {
        p2=Position.DESIGN;
        addEmp = new Employees(fName, lName, pos);
        list.add(addEmp);
        numDesign++;
//  message="There are already"+ (numDesign++) +" design persons\nEmployee not added";

    }//else  return message;

    else if (pos.equals("SALES")&&numDesign<1&&numSales<2&&numManufacturing<4)
    {
        //String error;
        p2=Position.SALES;
        addEmp = new Employees(fName, lName,pos);
        list.add(addEmp);
        numSales++;

    }//else return JOptionPane.showInputDialog(null," There is already a sales person\nEmployee not added");
    else if (pos.equals("MANUFACTURING")&& numDesign<1&&numSales<2&&numManufacturing<4)
    {
        p2=Position.MANUFACTURING;
        addEmp = new Employees(fName, lName, pos);
        list.add(addEmp);
        numManufacturing++;
        //p2.MANUFACTURING++;
    }//else return JOptionPane.showInputDialog(null," There are already four manufacturing persons \nEmployee not added ","Empolyees", JOptionPane.ERROR_MESSAGE);

     if (numSales<1)
     {
         return JOptionPane.showInputDialog(null," There is already a sales person\nEmployee not added");
     }
     if (numDesign<2)
     {
         return JOptionPane.showInputDialog(null, "There are already"+ (numDesign++) +" design persons\nEmployee not added");
     }
    if (numberOfCompanies<3)
    {
        return JOptionPane.showInputDialog(null,"There are already two companies Company not added","Empolyees", JOptionPane.ERROR_MESSAGE);
    }
    if (numManufacturing<4)
    {
         return JOptionPane.showInputDialog(null," There are already four manufacturing persons \nEmployee not added ","Empolyees", JOptionPane.ERROR_MESSAGE);
    }
    if (numEmployees<7)
    {
        return  JOptionPane.showInputDialog(null,"There are already 7 employees\nEmployee not added","Empolyees", JOptionPane.ERROR_MESSAGE);//check.toString();//look
         // System.out.println(check.printCompany());
    }

    return null;
    // TODO Auto-generated method stub

}

/*public Company(String str) {
    // TODO Auto-generated constructor stub
    //
}*/





public String printCompany( ) {
    // TODO Auto-generated method stub
    String str = null;
    for (int index=0; index<list.size();index++)
    {
        str+=list.get(index).getFName()+" "+list.get(index).getLName()+" "+"Position:"+list.get(index).getPosition();
        System.out.println(str);
    }
    return str;

}
public String toString()
{
    int index=0;

    String str =list.get(index).getFName()+" "+list.get(index).getLName()+" "+"Position:"+list.get(index).getPosition();
    System.out.println(str);;
    return JOptionPane.showInputDialog(null,str);
}

这是我试图从printcompany方法打印arraylist的gui类:

private class ButtonListener implements ActionListener

{

    @Override
    public void actionPerformed(ActionEvent e) {    
        // TODO Auto-generated method stub


        String lnameInput;


        String fNameInput;
        lnameInput= lNameText.getText();


        fNameInput=fNameText.getText();


        if(e.getSource() == addEmpButton){

        if(design.isSelected())
        {
            check.addEmployee(fNameInput, lnameInput, "Design");

        }

        if(sales.isSelected())
        {
            check.addEmployee(fNameInput, lnameInput, "Sales");
        }

        if(manufacturing.isSelected())
        {
            check.addEmployee(fNameInput, lnameInput, "Manufacturing");
        }

        }

        if(e.getSource() == clearButton)
                {
            lNameText.setText("");
            fNameText.setText("");
                }
         if (e.getSource() == printEmpsButton) {
                     JOptionPane.showMessageDialog(null,check.printCompany());//look
                  System.out.println(check.printCompany());
              }

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

        if(e.getSource() == newCompButton)
                {
        //  check = null;
            check = new Company(companyN);
            message=JOptionPane.showInputDialog(null, "What is the name of this Company?","Company Name", JOptionPane.PLAIN_MESSAGE);
                }







    }


}

您的指令不是在Company类中显示JOptionPanes,而是让addEmployee(...)方法返回String。 了解此类存在的原因不是与用户进行通信,而是与其他类(即您的GUI)进行通信,而是应该与其他类进行通信。 我会:

  • 首先,将所有JOptionPanes排除在Company类之外。 他们不属于那里。
  • 而是让该方法仅返回字符串。 您的GUI将使用这些字符串与用户进行交互。
  • 首先添加一个新员工前,请检查员工位置计数。 如果达到最大值,则返回您的字符串。
  • 否则,如果一切都很好,请添加您的新员工并增加您的人数。
  • 现在,您的GUI代码应该捕获addEmployee(...)方法返回的所有字符串。 我将其放入一个String变量,例如errorMessage 即, String errorMessage = check.addEmployee(...); (您的代码中当然没有...)。
  • 如果返回的String为null ,即, if (errorMessage == null)那么一切都很好,但是如果不是,那么您可以在该类中使用errorMessage String通知用户某些问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM