我有一个语音识别系统,这是listen(button)
功能
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.setIcon( loading_icon );
microphone.startRecording()
jButton1.setIcon( speak_icon );
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
result = recognizer.recognize();
if (result != null) {
String bestfinalnofiller = result.getBestFinalResultNoFiller();
String hypothesis = result.getBestPronunciationResult();
String getresult = result.getBestResultNoFiller();
System.out.println("You said: " + bestfinalnofiller + '\n');
System.out.println("You said: " + hypothesis + '\n');
System.out.println("You said: " + getresult + '\n');
} else {
}
}
我的结果将是:
按钮图标保持不变,并在工作完成后更改
当工作进行字符串"start speaking..."
recognizer.recognize()
功能:
public Result recognize(String referenceText) throws IllegalStateException {
Result result = null;
checkState(State.READY);
try {
setState(State.RECOGNIZING);
result = decoder.decode(referenceText);
} finally {
setState(State.READY);
}
return result;
}
我试图添加:
SwingUtilities.invokeLater
但没有用,请帮帮我,我想让按钮在计算时改变它的图标图像......
答案 0 :(得分:2)
recognizer.recognize()
可能是一种阻止方法,也就是说,在它完成之前,它不会返回。
这意味着在该方法返回之前,您将阻止事件调度线程,该线程负责处理绘制请求等。
这意味着在您的方法返回之前,不能绘制或更新(或响应)任何内容。
一个简单的解决方案是使用SwinWorker
,这将允许您设置后台进程以执行阻塞调用,但也提供了许多方法通过EDT同步UI的任何更新
详细了解Concurrency in Swing和SwimgWorker了解详情