2012-05-18 30 views
1

當按下開始按鈕並且當我的任務完成時,我開始我的進度條,我調用了我試圖阻止進度條的不確定模式的函數,但我仍然無法要做到這一點(我正在使用SwingWorker爲我的應用程序)刪除JProgressBar的不確定模式

這是我的代碼,用於啓動進度條;此代碼寫入開始按鈕內部:

private void StartButtonMouseClicked(java.awt.event.MouseEvent evt) { 
    Main f22 = new Main(); 

    f2.getfile(FileName, 0); 
    f2.execute(); 

    SwingUtilities.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      jProgressBar1.setIndeterminate(true); 

     } 
    }); 

這是函數中的代碼,一旦任務完成後調用它。

jProgressBar1.setVisible(false); 
+0

從哪個線程更新進度條?你有沒有把這個調用包裝在invokeLater中的setVisible(true)...? – serg10

+0

@ serg10當後臺任務完成後,調用完成方法(swing worker)。在該Done方法中,我調用了將進度條的可見性設置爲不可見的函數。我不明白你打包電話的意思,請你詳細說明一下嗎? – Xara

+1

嘗試調用jProgressBar1.setIndeterminate(false);當你完成。 – Martin

回答

2

請嘗試去通過這個代碼,JProgressBar的使用的SwingWorker其工作代碼,然後,一旦你瞭解它的工作,可隨時實現它自己的方式。

import java.awt.*; 

import java.awt.event.*; 

import java.util.List; 

import javax.swing.*; 


public class ProgressBarTest 
{ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
      { 
      public void run() 
      { 
       JFrame frame = new ProgressBarFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

/** 
* A frame that contains a button to launch a simulated activity, a progress bar, and a 
    * text area for the activity output. 
    */ 
class ProgressBarFrame extends JFrame 
{ 
    public ProgressBarFrame() 
    { 
     setTitle("ProgressBarTest"); 
     setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 

     // this text area holds the activity output 
     textArea = new JTextArea(); 

     // set up panel with button and progress bar 
     final int MAX = 1000; 
     JPanel panel = new JPanel(); 
     startButton = new JButton("Start"); 
     progressBar = new JProgressBar(0, MAX); 
     progressBar.setStringPainted(true); 
     panel.add(startButton); 
     panel.add(progressBar); 

     checkBox = new JCheckBox("indeterminate"); 
     checkBox.addActionListener(new ActionListener() 
      { 
      public void actionPerformed(ActionEvent event) 
      { 
       progressBar.setIndeterminate(checkBox.isSelected()); 
       progressBar.setStringPainted(!progressBar.isIndeterminate()); 
      } 
      }); 
     panel.add(checkBox); 
     add(new JScrollPane(textArea), BorderLayout.CENTER); 
     add(panel, BorderLayout.SOUTH); 

     // set up the button action 

     startButton.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       startButton.setEnabled(false); 
       activity = new SimulatedActivity(MAX); 
       activity.execute(); 
      } 
      }); 
    } 

    private JButton startButton; 
    private JProgressBar progressBar; 
    private JCheckBox checkBox; 
    private JTextArea textArea; 
    private SimulatedActivity activity; 

    public static final int DEFAULT_WIDTH = 400; 
    public static final int DEFAULT_HEIGHT = 200; 

    class SimulatedActivity extends SwingWorker<Void, Integer> 
    { 
     /** 
     * Constructs the simulated activity that increments a counter from 0 to a 
     * given target. 
     * @param t the target value of the counter. 
     */ 
     public SimulatedActivity(int t) 
     { 
      current = 0; 
      target = t; 
     } 
     protected Void doInBackground() throws Exception 
     { 
     try 
      { 
      while (current < target) 
      { 
       Thread.sleep(100); 
       current++; 
       publish(current); 
      } 
      } 
      catch (InterruptedException e) 
      { 
      } 
      return null; 
     } 

     protected void process(List<Integer> chunks) 
     { 
      for (Integer chunk : chunks) 
      { 
      textArea.append(chunk + "\n"); 
      progressBar.setValue(chunk); 
      } 
     } 

     protected void done() 
     { 
     startButton.setEnabled(true); 
     } 

     private int current; 
     private int target; 
    } 
}