2014-01-24 83 views
0

我正在使用NetNeans 我有一個JPanel應該顯示一些已經輸入的數據以及進度條,而程序的其餘部分執行一些冗長的處理(大約1分鐘)。一旦處理完成,這個相同的JPanel應該用包含處理數據的幾個JTextFields進行更新。 問題: 在整個處理過程中,JPanel顯示爲完全空白。處理完成後,所有數據都會顯示出來。它令我困惑。下面是我如何編程它:動態更新JFrame的問題

/*The parent class which calls the output JPanel 
    *Lister is the output JPanel. The constructor calls initComponents(), and fills 
    *text fields that contain the data that has already been input. 
    *fillresult then triggers the processing function that takes a while. Once the 
    *processing function is done, fillresult fills the processed data into the 
    *respective Text Fields 
    */ 
    ListWords = new Lister(); 
    System.out.println("l"); 
    ListWords.setVisible(true); 
    ListWords.fillResult(); 

    //Lister constructor: 
    Lister(){ 
     initComponents(); 
     MatchWords = new Matcher();//Matcher() only contains the initialization of a List 

     jTextField1.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(0, 1)); 
     jTextField2.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(1, 2)); 
     jTextField3.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(2, 3)); 
     jTextField4.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(3, 4)); 
     jTextField5.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(4, 5)); 
     jTextField6.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(5, 6)); 
     jTextField7.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(6)); 
    } 

    //fillresult(): 
public void fillResult(){ 
    int rIndex = 0; 
    MatchWords.makeWords(rIndex); 
    while(MatchWords.realWords.Head == null && rIndex < 2){ 
     rIndex++; 
     updateStatusBar(); 
     MatchWords.makeWords(rIndex); 
    } 
    Wordpoints = new String[MatchWords.realWords.getSize()]; 
    for(int i=0; i<Wordpoints.length; i++){ 
     Wordpoints[i] = ""; 
    } 

    for(int i=0; i<MatchWords.realWords.getSize(); i++){ 
     int total = 0; 
     for(int j=0; j<MatchWords.realWords.getNode(i).getWord().length(); j++){ 
      for(int k=0; k<Scrabbulous.scrab.scralphabet.length; k++){ 
       if(MatchWords.realWords.getNode(i).getWord().charAt(j) == Scrabbulous.scrab.scralphabet[k].getLetter()){ 
        total += Scrabbulous.scrab.scralphabet[k].getValue(); 
        Wordpoints[i] = ""+total; 
       } 
      } 
     } 
    } 

    try{ 
     jTextField8.setText(MatchWords.realWords.Head.getWord()); 
     jTextField13.setText(Wordpoints[0]); 
    }catch(NullPointerException e){ 
     jTextField8.setText("No Match Found"); 
     jTextField13.setText("-"); 
    } 
    try{ 
     jTextField9.setText(MatchWords.realWords.getNode(1).getWord()); 
     jTextField14.setText(Wordpoints[1]); 
    }catch(NullPointerException e){ 
     jTextField9.setText("No Match Found"); 
     jTextField14.setText("-"); 
    } 
    try{ 
     jTextField10.setText(MatchWords.realWords.getNode(2).getWord()); 
     jTextField15.setText(Wordpoints[2]); 
    }catch(NullPointerException e){ 
     jTextField10.setText("No Match Found"); 
     jTextField15.setText("-"); 
    } 
    try{ 
     jTextField11.setText(MatchWords.realWords.getNode(3).getWord()); 
     jTextField16.setText(Wordpoints[3]); 
    }catch(NullPointerException e){ 
     jTextField11.setText("No Match Found"); 
     jTextField16.setText("-");  
    } 
    try{ 
     jTextField12.setText(MatchWords.realWords.getNode(4).getWord()); 
     jTextField17.setText(Wordpoints[4]); 
    }catch(NullPointerException e){ 
     jTextField12.setText("No Match Found"); 
     jTextField17.setText("-"); 
    } 

有人可以請幫助嗎?哦,是的,我讀過關於添加.refactor()和/或repaint(),但至少在構造函數中初始化的jForms應該顯示? :/ 另外,值得一提的是,當前在fillresult()函數中的所有代碼最初都在構造函數中,並且輸出顯示。但是,由於我想在這裏使用進度條,所以我將它轉換爲新的函數。請幫助?

回答

1

「問題:在整個處理過程中,JPanel顯示完全空白,處理完成後,所有數據都顯示出來。

發生了什麼事情是一切正在發生在一個線程事件調度線程(EDT)上。爲了使事件發生,就像你的JPanel更新一樣,這個過程需要完成。

如前所述有關EDT:

「事件調度線程任務,必須儘快完成,如果他們不這樣做,未處理的事件備份和用戶界面變得無法響應。」

要解決此問題,您需要在後臺線程中運行您的長進程。您應該查看Concurrency in Swing - 特別是關於Worker Threads and SwingWorker的部分。

+0

非常感謝您確定問題,我正在查看。我希望我能夠學習到足以在今晚能夠實現它 – user3233029

+0

我試圖穿線它,它的噩夢。你能否提供一個線索,我應該把doInBackground()放在哪個部分,以及哪個部分放在done()中?謝謝 – user3233029