目前我有这个班级是基本的薪资报告:
PayrollReport(Person person, double salary){
this.person = person;
this.salary = salary;
}
public String toString(){
return person + " was paid "+ salary;
}
在第二节课中,我按一个按钮,它会将值分配给工资单类:
for (int i=0; i<;listOfPeople.size();i++){
Person person = listOfPeople.get(i);
double salary =listOfPeople.get(i).getSalary();
newPayroll = new PayrollReportItem(person, salary);
}
我现在想要在按下按钮时在第三个类的jTextFeild中显示PayrollReports toString()。
花了好几个小时之后,我所做的就是让jTextFeild显示一行null。
答案 0 :(得分:0)
在您的代码中,您一次又一次地将值分配给同一个对象,即newPayroll
。因此,只会存储最后一个值,并且将覆盖所有值。
你需要做的是这个
toString()
方法像这样的东西
Class Second{
public void printAll(){
for (int i=0; i<;listOfPeople.size();i++){
Person person = listOfPeople.get(i);
double salary =listOfPeople.get(i).getSalary();
newPayroll = new PayrollReportItem(person, salary);
newPayroll.toString(); //call method to print values
}
}
Class Third{
//when button pressed to the following
Second obj=new Second(); //create an object of second class
obj.printAll(); //call method to print
}
希望这会有所帮助:)