我的java swing计时器限制在+ - 300毫秒的时间表

时间:2016-11-20 13:45:23

标签: java swing timer

所以我将Arduino设置为JSON服务器,然后我想在桌面上登录.log文件。但问题是计时器(javax.swing.timer)不能使用超过约+ - 300ms的任何值。其余的代码工作正常,但也许JSON库对计时器有一些影响?如果有人帮忙,我会很高兴。程序确实执行,在编译时它没有显示任何错误。这是大部分代码,我用一些星号标记了我认为失败的地方:

    public class ikd {

  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
  }

  public static void main(String[] args) throws IOException, JSONException {
      getData();
  }

  public static void getData() throws IOException, JSONException {
      //***********************************************
      //***********************************************
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              System.out.println("yea");
          }
      };
      new Timer(>+-300 works here but 400 doesn't!!!<, taskPerformer).start();  
      //***********************************************
      //***********************************************
      JSONObject json = readJsonFromUrl(">");
      System.out.println(json.toString());
      JSONArray arr = json.getJSONArray("kamers");
      JSONObject tempObj = (JSONObject)arr.get(0);
      String temp = tempObj.getString("temp");
      String humidity = tempObj.getString("humidity");
      String light = tempObj.getString("ldr");
      System.out.println("Temperatuur: " + temp + ", Luchtvochtigheid: " + humidity + ", Lichtsterkte: " + light);
      try{
          PrintWriter writer = new PrintWriter("data.log", "UTF-8");
          writer.println(temp + "," + humidity + "," + light);
          writer.close();
      } catch (Exception e) {
      }
  }

如果我将计时器放在Main()方法中,则根本不会发生任何事情。

这是控制台输出,定时器以301 ms的间隔运行:

301 ms间隔:

301 ms interval

这是400 ms间隔的控制台输出:

400 ms间隔:

400 ms interval

0 个答案:

没有答案