我正在编写一个小的私人应用程序,目前它利用了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()方法合并两组 - 同不必要的行为
我在这里监督什么?
你尝试把钨的东西,在一个SwingWorker的?您可以在构造函数中打开加载特性,并在done()方法中将其关闭。另外,应用程序究竟在听什么_state的值?它可以帮助你直接重绘jPanel到一个加载动画。 – bhowden