【问题标题】:How a for loop can put text in 3 JTextAreafor 循环如何将文本放入 3 JTextArea
【发布时间】:2013-03-31 06:15:27
【问题描述】:

我有一个 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("");

所以我想我必须在这个循环上放一个循环,但不知道从哪里开始。

【问题讨论】:

    标签: java swing for-loop while-loop jtextarea


    【解决方案1】:

    如果您可以使用文本区域数组而不是 3 个不同的变量,则可以执行以下操作

    JTextArea  days[] = new JTextArea[3];
            int i=0;   
            for (ForecastForday1 day : forecast)  {
                days[i++].append("Append string");
            }
    

    【讨论】:

      【解决方案2】:

      我可以从许多可能的方法中提出一种方法来解决这个问题,

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

      我想这就是你想要的。

      【讨论】:

      • 感谢您的回复。但它在 day1 (第一个文本区域)中全部打印。 ://
      【解决方案3】:

      您正在寻找这样的东西吗? 告诉我这是否有帮助或是否需要调整。

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

      【讨论】:

        【解决方案4】:

        这里是解决方案。感谢@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++;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-02
          • 1970-01-01
          • 1970-01-01
          • 2020-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多