簡體   English   中英

如何從其他方法獲取JTextArea文本

[英]How to get JTextArea text from another method

我想使用另一個類中的方法添加到我的JTextArea中(我的代碼中為TEXT ),該怎么做?

 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 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**');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM