java有一个Jbutton打印来自另一个类的内容
我的addEmployee方法应该检查一次只有1名销售人员、2名设计师和4名制造人员,并检查公司的员工不超过7名,方法将返回一个字符串,指示错误是什么(即“该公司已经有一名销售人员”),或者如果添加了员工,则返回null,但我的if语句不起作用,出于某种原因,它不允许我通过说“canot return a void result”来使用消息对话框,而是强制我使用inputdialog
我的print company方法似乎有问题,但我不知道是什么,因为它只返回null,而不是打印出名称、位置和位置的arraylist
我的问题是如何解决这个问题,这样无论我在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);
}
}
}
共 (0) 个答案