我想使用另一个类中的方法附加到我的 JTextArea 中(在我的代码中文本在这里),该怎么做?
SecondWindow() {
super("Mortage Loan Calculator");
setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
tArea = new JTextArea(***TEXT IS HERE***, 30, 40);
scroll = new JScrollPane(tArea);
add(scroll);
setLocationRelativeTo(null);`
`这是一个方法:
public void calcAnnuity(int years, int months, double amount, double rate){
double totalMonths = (12 * years) + months;
double partOfRate = rate / 12.0 / 100.0;
double tempAmount = amount;
double payment = amount * partOfRate * Math.pow(1 + partOfRate, totalMonths) / (Math.pow(1 + partOfRate, totalMonths) - 1); //mathematical formula
DecimalFormat decFormat = new DecimalFormat("#.##");
System.out.println(1 + " Payment = " + decFormat.format(payment) + "--- Left to pay: " + decFormat.format(amount));
for(int i = 2; i <= totalMonths; i++) {
tempAmount -= (payment - partOfRate * amount);
amount -= payment;
**textishere.append**(i + " Payment = " + decFormat.format(payment) + " --- Left to pay: " + decFormat.format(tempAmount));
}
}
最佳答案
嗯,最简单的方法是在 JTextArea 所在的 ClassA 中实现一个公共(public)静态方法。
public static setJTextAreaText(String text){
tArea.setText(text);
}
在 ClassB 中导入 ClassA,然后从方法 calcAnnuity() 调用此方法
import ClassA;
public void calcAnnuity(int years, int months, double amount, double rate){
...
ClassA.setJTextAreaText('**textishere.append**');
}
关于java - 如何从另一种方法获取 JTextArea 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49335857/