2014-04-07 17 views
0

我有一個語音識別系統,這是listen(button)功能我的java swing應用程序jcomponents不會繪製,僅在計算完成後重繪?

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 

,但沒有工作,請大家幫幫我,我想使該鍵可以改變它的圖標圖像而計算...

回答

2

很可能recognizer.recognize()是一個阻塞方法,也就是說,直到完成,它不會返回。

這是什麼意思是,直到方法返回,你是阻塞事件調度線程,它負責,其中包括處理繪製請求。

這意味着,直到你的方法返回,沒有任何東西可以被繪或更新(或響應)。

簡單的解決辦法是使用SwinWorker,這樣可以讓你建立一個後臺進程,以執行阻塞調用,但也提供了一些方法通過EDT

同步到UI的任何更新

仔細看看Concurrency in SwingSwimgWorker瞭解更多詳情

+0

但是爲什麼不在擺到另一條線上時修復繪製未上漆的圖形,除了'SwingWorker'之外,還有什麼解決方案嗎?再次感謝 –

+0

@malekh因爲Swing是一個單線程框架,就像大多數GUI框架一樣。在EDT的上下文中調用'actionPerformed',這意味着在該方法存在之前,它不能處理下一系列事件(包括繪畫事件)。這就是它的工作原理。 – MadProgrammer

+0

如果使用EDIT的上下文調用'actionPerformed',是否有使用'鼠標點擊'功能或'on-release'功能。 –