2012-04-01 58 views
2

我創建了一個表單,其中存在兩個組件,按鈕和進度條(Netbeans拖放).Form包含我的應用程序啓動時的主要方法。我創建了另一個類,其中我寫了一個函數。我想要的是,當我按下按鈕時,應用程序進入函數並且進度條與它同時運行,並且當該函數完成其功能時,進度條顯示100%complete.Now此函數可以隨時完成,所以我不能設置進度條的最大值。所以,在這種情況下做什麼?任何人都可以請給我一個很好的例子。進度條與一個函數同時運行(在另一個類中)

回答

4

由於什麼樣的,你是所謂的「被調用函數」中做了工作的,所以很難說,你要什麼的情況下,雖然你可以把你的線條像progressBar.setValue(someProgress);在常規間隔與它的Indeterminate Statetrue,並且在函數的結尾處,您可以簡單地說progressBar.setValue(100);Indeterminate State將在此處變爲false,以便它可以向最終用戶顯示。

看一看這個示例程序:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ProgressExample 
{ 
    public static JProgressBar progressBar; 

    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("Progress Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout(5, 5)); 

     progressBar = new JProgressBar(0, 100); 
     progressBar.setValue(0);     

     JButton button = new JButton("START"); 
     button.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       progressBar.setIndeterminate(true); 
       WorkingDialog wd = new WorkingDialog(); 
       wd.createAndDisplayDialog(); 
      } 
     }); 

     contentPane.add(progressBar, BorderLayout.PAGE_START); 
     contentPane.add(button, BorderLayout.PAGE_END); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new ProgressExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

class WorkingDialog extends JDialog 
{ 
    private String message = "HelloWorld"; 
    private int count = 0; 
    private JTextField tfield; 
    private Timer timer; 

    private ActionListener timerAction = new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      if (count == 10) 
      { 
       timer.stop(); 
       ProgressExample.progressBar.setIndeterminate(false); 
       ProgressExample.progressBar.setValue(100); 
       ProgressExample.progressBar.setStringPainted(true); 
       dispose(); 
       return; 
      } 
      tfield.setText(tfield.getText() + message.charAt(count)); 
      count++; 
     } 
    }; 

    public void createAndDisplayDialog() 
    { 
     setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
     setLocationByPlatform(true); 

     JPanel panel = new JPanel(); 
     tfield = new JTextField(10); 

     panel.add(tfield); 

     add(panel); 
     pack(); 
     setVisible(true); 

     timer = new Timer(1000, timerAction); 
     timer.start(); 
    } 
} 

所以,好像你是寫

ProgressExample.progressBar.setIndeterminate(false); 
ProgressExample.progressBar.setValue(100); 
ProgressExample.progressBar.setStringPainted(true); 

while循環之後。

+0

實際上,在一個函數內部,一個while循環正在運行,所以我不能在每隔幾行代碼之後設置進度條的值.... – Xara 2012-04-01 08:14:08

+0

然後,我想你在開始和結束後只是簡單的設置了'setIndeterminate(true)'循環按照答案中的建議進行操作,以向用戶顯示結果。我修改了類似的程序,給出了一個想法:-) – 2012-04-01 08:28:23

1

您可以查看my answer in a previous SO question,其中包含使用JProgressBar的示例,該示例使用SwingWorker從另一個Thread獲取更新。是否使用SwingWorker取決於您的用例。如果該功能需要一段時間才能運行,則最好使用SwingWorker以避免阻塞UI。

+0

+1,我總是儘可能遠離SwingWorker,但看起來像這種情況是爲了SwingWorker :-)。不錯的代碼示例:-) – 2012-04-01 09:54:11

相關問題