2013-04-12 75 views
0

在來到這裏之前,我已經在網絡上搜索並閱讀了數十個話題,但我無法解決我的問題。循環中的Java JFrame更新

我想顯示上傳的進度。在下面的代碼中,除了我的JFrame沒有更新之外,所有的工作都可以正常工作。我正在使用我在另一個主題上找到的技術,但似乎並不奏效。我認爲如果你看看我的代碼會更簡單(我刪除了與問題無關的說明)。

/* 
* Correct imports have been done 
*/ 

class GUI extends JFrame { 
    public JPanel pan; 

    public GUI(JPanel panel) { 
     super("Uploading..."); 
     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
     setPreferredSize(new Dimension(600, 500)); 
     setMinimumSize(new Dimension(600, 500)); 
     setMaximumSize(new Dimension(600, 500)); 
     setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE); 
     setLocationRelativeTo(null); 

     pan = panel; 
     pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS)); 

     setContentPane(pan); 
     setVisible(true); 
    } 
} 

public class GUIUpload { 
    private static GUI ui; 

    public static void main(String args[]) { 
     JPanel main = new JPanel(); 
     ui = new GUI(main); // create and display GUI   
     uploadLoop(args, main); // start the upload loop 

     /* 
     * After upload is finished 
     */ 
      JButton jb = new JButton("Ok"); 
     jb.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       ui.setVisible(false);    
      } 
     }); 

     ui.getContentPane().add(jb); 
     ui.getContentPane().repaint(); 
    } 

    private static void uploadLoop(String[] paths, JPanel monitor) { 
     /* 
     * Upload starts here 
     */ 
     long transfered; 
     long size; 
     InputStream inputStream;   

     FTPClient ftpClient = new FTPClient(); 
     try { 
     ftpClient.connect("xxxxxx", 21); 
      boolean success = ftpClient.login("xxxxxx", "xxxxxx"); 
      /* 
      * Sending 
      */ 
      for (int i = 0; i < 4; i++){ 
       if (paths[i] != null){ 
        File localFile = new File(paths[i]); 
        String remoteFile = "/public_html/papers/" + i + ".pdf"; 
        JLabel label = new JLabel("Uploading..."); 
        ui.getContentPane().add(label); 
        ui.repaint(); 

        inputStream = new FileInputStream(localFile); 
        // Monitoring misc 
        size = localFile.length(); 
        transfered = 0; 
        int percentage = 0; 
            // Progress bar 
        JProgressBar pgb = new JProgressBar(); 
        pgb.setValue(0); 
        ui.getContentPane().add(pgb); 
        ui.repaint(); 
        // Upload routine 
        OutputStream outputStream = ftpClient.storeFileStream(remoteFile);; 
        byte[] bytesIn = new byte[4096]; 
        int read = 0; 
        while ((read = inputStream.read(bytesIn)) != -1) { 
         outputStream.write(bytesIn, 0, read); 
         transfered += read; 
         percentage = (int)(transfered * 100.0/size + 0.5); 
         System.out.println(percentage); 
         pgb.setValue(percentage); 
         ui.repaint(); 
        } 
        inputStream.close(); 
        outputStream.close(); 
        boolean completed = ftpClient.completePendingCommand(); 
        /* 
        * End of upload 
        */ 
       } 
      } 
     } // end try 
     catch (Exception e){ 
      // Do nothing} 
     } // end catch 
    } // end upload method 
} 

百分比工作正常。文件傳輸工作正常。當我在GUIUpload類的主要方法中重新繪製它時,GUI框架纔會更新。當它重新繪製,我可以看到,所有的標籤和progressbars已正確添加和更新(進度條顯示的最大值。

所以..它已經很長一段時間我正在尋找如何做到這一點,並且我嘗試過使用線程,我嘗試了很多東西,但都沒有工作(或者我嘗試了它們時做了錯誤處理)。

非常感謝任何能夠幫助我的人。

最好的問候。

回答

1
  • Swing是單線程的。當您執行resou由於文件下載等繁重任務,您會阻止Swing重新繪製。
  • 毫無疑問,原始Threads不起作用,因爲Swing擁有它自己的concurrency features,它提供了一種處理耗時後臺任務的方法。 Threads未設計爲與鞦韆組件交互。
  • 使用SwingWorker
+0

嗯,我會說......甜心!我跟着你的SwingWorker線程(哈哈),並找到我的解決方案!謝謝 !謝謝 !謝謝 ! (不能相信我浪費了很多時間來研究這個問題) – user2154283