2012-10-30 90 views
1

我有一個程序,通過我在主函數中調用的函數通過swing獲取用戶的輸入。提交按鈕具有附加的操作執行方法。我試圖在刪除輸入文件並設置文本以通知用戶後重新繪製屏幕。它不會重新繪製,直到try/catch中帶有自定義函數。不知道我在做什麼錯我雖然它會按順序執行?以下是我的行動預先附加到我的提交按鈕。一個注意事項是,如果我做一個frame.dispose()或setVisibility(假)它將刪除框架,任何幫助,將不勝感激。謝謝!!JFrame不會重繪,直到try/catch完成後

button.addActionListener(new ActionListener(){ 

       public void actionPerformed(ActionEvent e) { 
        loc = FileLoc.getText(); 
        name = FileName.getText(); 

        //inform user 
        area.setText("Attempting To Run Test...."); 
        //backGroundPane contains the user input fields and button  
        frame.remove(backGroundPane); 
        frame.repaint(); 

        if(loc != null && name != null && 
          !loc.equals("") && !name.equals("")) 
        { 
         try { 
          CallDrivers(); 
         } catch (InterruptedException e1) { 
          System.out.println("Error Running Function"); 
          //e1.printStackTrace(); 
         } 
        } 
        else{ 
         area.setText("There are Blank Fields"); 
         System.out.println("test"); 
        } 
       }}); 

回答

3

您正在阻止EDT(事件調度線程)。

事件調度線程負責按發佈的順序一次調度一個UI事件。事件可以是其中包括:

  • 關鍵事件(用戶按下的琴鍵)
  • 鼠標事件(用戶移動鼠標爲例)
  • 調用事件(你叫SwingUtilities.invokeLater()或JComponent.repaint()爲例如)
  • Paint事件(請求於塗抹部件)
  • 動作事件(邏輯事件通過中發生的InputEvent的觸發)

當您調用repaint時,您正在隊列上推送一個事件,但只要當前事件(actionPerformed的一個事件)未完成,重繪就不會發生。這就是爲什麼你的try/catch已完成

後您的重繪只發生在這裏閱讀更多:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

+0

這似乎只是想我需要謝謝! – Lazadon

相關問題