有3个文件:
1. Annuuity.java
2. AnnuityDueGUI.java // GUI for Annuity Due
2. AnnuityDueResultGUI.java //GUI for the result
在AnnuityDueGUI.java下:
public double calculateFADGUI(){
//FVA = A{[(1+i)^n – 1] / i} (1+i)
String amountStr = amount.getText() ; //convert string to double
dAmount = Double.parseDouble(amountStr) ;
String iStr = iText.getText() ;
dInterest = Double.parseDouble(iStr) ;
String periodStr = period.getText() ;
dPeriod = Double.parseDouble(periodStr) ;
iPeriod = (int)dPeriod ;
due = new Annuity(dAmount, dInterest, iPeriod) ;
System.out.println(due.calculateFAD()) ;
return due.calculateFAD() ; //calculateFAD() is under Annuity.java
}
在AnnuityDueResultGUI.java下:
AnnuityDueGUI due ;
public AnnuityDueResultGUI(AnnuityDueGUI due){ //1st solution failed
this.due = due ;
}
public void grabResult(){ //1st solution failed
result = this.due.calculateFADGUI() ;
}
public AnnuityDueResultGUI(){
JPanel p6 = new JPanel() ;
p6.setLayout(new GridLayout(2, 1)) ;
p6.add(new JLabel("you will have approximately $" + result)) ;
// other codes
}
从AnnuityDueGUI.java我可以看到due.calculateFAD()的结果。但是,我想在AnnuityDueResultGUI.java下显示结果
我已经将它们放在名为'GUI'的包下,并且还导入了AnnuityDueGUI.java和Annuity.java。
我使用未注册的用户在我的其他同一问题上执行了此论坛中建议的步骤。但是,它不起作用(传递的结果为0)。这就是我再次发布相同问题的原因。
请提前协助并表示感谢。
答案 0 :(得分:1)
我想您忘了拨打grabResult()
方法。
public AnnuityDueResultGUI(AnnuityDueGUI due){
this.due = due ;
grabResult(); // calling this will store the value in the "result" instance variable
JPanel p6 = new JPanel() ;
p6.setLayout(new GridLayout(2, 1)) ;
p6.add(new JLabel("you will have approximately $" + result)) ;
// other codes
}
private void grabResult(){
result = this.due.calculateFADGUI() ;
}
另外,您应该避免在构造函数中调用非final类的公共方法。