2014-06-24 95 views
0

我正在编写一个小的私人应用程序,目前它利用了Wolfram Alpha Java绑定。应用程序的GUI使用Swing实现。我的Wolfram Alpha的插件有看起来像这样的功能:Java Swing重绘延迟

public void fire(String value) { 

    // This sets the field state to loading 
    _state = loading; 

    // This tells the controller to redraw the window 
    // When reading the state of the plugin it sets an 
    // animated gif at a JPanel. This should be displayed 
    // before the application is blocked by the Wolfram Alpha 
    // Request. 

    // updateInput does nothing more then call repaint after some 
    // other function calls. No special threading included. 

    getController().updateInput(""); 


    // My wolfram request follows: 

    WAQuery query = _engine.createQuery(); 
    query.setInput(value); 
    try { 
     // For educational purposes, print out the URL we are about to send: 
     System.out.println("Query URL:"); 
     System.out.println(_engine.toURL(query)); 
     System.out.println(""); 

     // This sends the URL to the Wolfram|Alpha server, gets the XML result 
     // and parses it into an object hierarchy held by the WAQueryResult object. 
     WAQueryResult queryResult = _engine.performQuery(query); 

     if (queryResult.isError()) { 
      System.out.println("Query error"); 
      System.out.println(" error code: " + queryResult.getErrorCode()); 
      System.out.println(" error message: " + queryResult.getErrorMessage()); 
     } else if (!queryResult.isSuccess()) { 
      System.out.println("Query was not understood; no results available."); 
     } else { 
      // Got a result. 
      System.out.println("Successful query. Pods follow:\n"); 
      for (WAPod pod : queryResult.getPods()) { 
       if (!pod.isError()) { 
        System.out.println(pod.getTitle()); 
        System.out.println("------------"); 
        for (WASubpod subpod : pod.getSubpods()) { 
         for (Object element : subpod.getContents()) { 
          if (element instanceof WAPlainText) { 
           System.out.println(((WAPlainText) element).getText()); 
           System.out.println(""); 
          } 
         } 
        } 
        System.out.println(""); 
       } 
      } 
      // We ignored many other types of Wolfram|Alpha output, such as warnings, assumptions, etc. 
      // These can be obtained by methods of WAQueryResult or objects deeper in the hierarchy. 
     } 
    } catch (WAException e) { 
     e.printStackTrace(); 
    } 


} 

的问题是,之前的窗口重新绘制请求启动。我有一个约1秒的请求延迟,然后显示加载动画。该动画应在请求开始前显示。

我试过至今:在钨查询前面

  • Thread.sleep()方法。但那会阻止重绘呢?! - 同样的意外行为
  • 把请求转换成一个可运行的 - 同样不受欢迎的行为
  • 把请求转换成主题推广一类 - 同样不受欢迎的行为
  • 与Thread.sleep()方法合并两组 - 同不必要的行为

我在这里监督什么?

+0

你尝试把钨的东西,在一个SwingWorker的?您可以在构造函数中打开加载特性,并在done()方法中将其关闭。另外,应用程序究竟在听什么_state的值?它可以帮助你直接重绘jPanel到一个加载动画。 – bhowden

回答

2

我有一个约1秒的请求延迟,然后显示加载动画。该动画应在请求开始前显示。

永远不要在Swing应用程序中使用Thread.sleep(),它有时会挂起整个应用程序,因为它已经在您的帖子中声明。在这种情况下,你应该使用Swing Timer

请看看How to Use Swing Timers

示例代码:

Timer timer = new Timer(1000, new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent arg0) {    
     // start loading animation now 
    } 
}); 
timer.setRepeats(false); 
timer.start() 
+0

这就像一个魅力。现在我的动画在查询时停止,但我想这可以通过下面的答案来解决。谢谢! – smoes

+0

听起来不错,你非常欢迎。 – Braj

2

我的建议是:

  • 使用后台线程,特别是SwingWorker的”
  • 执行长时间运行代码在SwingWorker的doInBackground方法中。
  • 在执行SwingWorker之前启动您的动画显示。
  • 给SwingWorker一个PropertyChangeListener,当SwingWorker的状态完成时,然后停止动画。
  • 确保动画不会与Swing事件线程相关联。使用摆动计时器。

例如,

// start animation -- be sure to use a Swing Timer or 
    // other way to prevent tying up the Swing event thread 

    SwingWorker<Void, Void> myWorker = new SwingWorker<Void, Void>() { 
    protected Void doInBackground() throws Exception { 
     fire(someValue); 
     return null; 
    }; 
    }; 
    myWorker.addPropertyChangeListener(new PropertyChangeListener() { 

    @Override 
    public void propertyChange(PropertyChangeEvent pcEvt) { 
     if (pcEvt.getNewValue().equals(SwingWorker.StateValue.DONE)) { 
      // .... stop the animation here 
     } 
    } 
    }); 

    myWorker.execute(); 
+0

谢谢主席先生,我会尽快尝试解决方案,以便在Swing内部获得光滑干净的螺纹。 – smoes