简体   繁体   English

如何从其他方法获取JTextArea文本

[英]How to get JTextArea text from another method

I want to use method from another class to append in my JTextArea ( TEXT IS HERE in my code), how to do it? 我想使用另一个类中的方法添加到我的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);`

`And here is a method: `这是一种方法:

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

Well, the easiest way would be to implement a public static method in your ClassA where the JTextArea is located. 好吧,最简单的方法是在JTextArea所在的ClassA中实现一个公共静态方法。

public static setJTextAreaText(String text){
   tArea.setText(text);
}

And in your ClassB you import ClassA and then call this method from your method calcAnnuity() 然后在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