Input array into GUI Jtextarea

半腔热情 提交于 2019-12-31 07:33:47

问题


Been trying to add my array, year and size of a house into a GUI, more precisely JTextarea. What's the simplest way of doing so? Not quite grasped data input yet.

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:


JTextArea has a simple append method that should make it easy to append the results

For example...

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

Take a look at How to use text areas for more deatils



来源:https://stackoverflow.com/questions/18567072/input-array-into-gui-jtextarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!