将数组输入到GUI Jtextarea中

时间:2013-09-02 06:43:50

标签: java swing append jtextarea

试图将我的阵列,房子的年份和大小添加到GUI中,更准确地说是JTextarea。最简单的方法是什么?还没有完全掌握数据输入。

public class House {
private int year;
private int size;
private static int nbrOfHouses;
public static final int MIN_SIZE = 10;

public House(int year, int size) {
    this.year = year;
    this.size = size;
}

public static int getNbrHouses() {
    return nbrOfHouses;
}

public int getYear() {
    return year;
}

public int getSize() {
    return size;
}
}



House[] myHouse = new House[10];{
  myHouse[0] = new House(1902, 120);
  myHouse[1] = new House(1954, 180);
  myHouse[2] = new House(1995,90);

  for(int i=0; i< myHouse.length; i++){
        if(myHouse[i]!=null){
          System.out.println(myHouse[i].getSize());

    }
    }

}

1 个答案:

答案 0 :(得分:3)

JTextArea有一个简单的append方法,可以轻松追加结果

例如......

for(int i=0; i< myHouse.length; i++){
    if(myHouse[i]!=null){
        textArea.append("Year: ");
        textArea.append(Integer.toString(myHouse[i].getYear()));
        textArea.append(".  Size: ");
        textArea.append(Integer.toString(myHouse[i].getSize()));
        textArea.append("\n");
    }
}

请查看How to use text areas了解更多详情