簡體   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