【问题标题】:Input array into GUI Jtextarea将数组输入到 GUI Jtextarea
【发布时间】:2013-09-05 04:47:55
【问题描述】:

一直在尝试将我的数组、房子的年份和大小添加到 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());

    }
    }

}

【问题讨论】:

  • 编写一个格式化程序,将房屋数组转换为字符串,并使用便捷方法 JTextArea.setText() 或模型模型 JTextArea.getDocument() 进行设置。

标签: java swing append jtextarea


【解决方案1】:

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了解更多详情

【讨论】:

  • 知道为什么它将房屋放在三个不同的文本区域中吗?
  • 在您的代码中,不知道,因为您没有使用JTextArea 提供任何示例
猜你喜欢
  • 1970-01-01
  • 2015-08-29
  • 2012-02-21
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多