[英]How a for loop can put text in 3 JTextArea
我有一個API,它具有for循環並可以打印出接下來3天的天氣情況。
for (ForecastForday1 day : forecast)
{
// Print out what day the forecast is for, and
// the conditions on that day
System.out.println("The weather on " + day.getDayOfWeek()
+ " will be " + day.getInfo("Conditions"));
}
但是我有3個JTextArea,所以每次循環時我都希望每個循環將其放在一個文本區域中,然后放在下一個文本區域中。
我的文字區域:
day1.append("");
day2.append("");
day3.append("");
所以我想我必須在這個循環上放一個循環,但是不知道從哪里開始。
如果可以使用文本區域數組而不是3個不同的變量,則可以執行以下操作
JTextArea days[] = new JTextArea[3];
int i=0;
for (ForecastForday1 day : forecast) {
days[i++].append("Append string");
}
我可以從許多可能的方法中建議一種方法,
int i=0;
for (ForecastForday1 day : forecast)
{
if (i%3==0)
day1.append("string here");
else if (i%3==1)
day2.append("string here");
else if (i%3==2)
day3.append("string here");
i++;
// Print out what day the forecast is for, and
// the conditions on that day
System.out.println("The weather on " + day.getDayOfWeek()
+ " will be " + day.getInfo("Conditions"));
}
我想這就是你想要的。
您是否正在尋找這樣的東西? 告訴我這是否有幫助或是否需要調整。
{
ArrayList<JTextArea> list = new ArrayList<JTextArea>() ;
list.add(day1);
//add day2 and day3 etc
Int i=0;
for (ForecastForday1 day : forecast)
{
//add check to see of list size is greater than i
list.get(i).append( //day data);
i=i+1;
}
}
這是解決方案。 謝謝@redDevil的幫助。 它給了我這個主意。
int i=0;
for (ForecastForday1 day : forecast)
{
if (i==0){
day1Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
else if (i==1){
day2Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
else if (i==2){
day3Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
i++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.