我創建了一個表單,其中存在兩個組件,按鈕和進度條(Netbeans拖放).Form包含我的應用程序啓動時的主要方法。我創建了另一個類,其中我寫了一個函數。我想要的是,當我按下按鈕時,應用程序進入函數並且進度條與它同時運行,並且當該函數完成其功能時,進度條顯示100%complete.Now此函數可以隨時完成,所以我不能設置進度條的最大值。所以,在這種情況下做什麼?任何人都可以請給我一個很好的例子。進度條與一個函數同時運行(在另一個類中)
2
A
回答
4
4
由於什麼樣的,你是所謂的「被調用函數」中做了工作的,所以很難說,你要什麼的情況下,雖然你可以把你的線條像progressBar.setValue(someProgress);
在常規間隔與它的Indeterminate State
到true
,並且在函數的結尾處,您可以簡單地說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
循環之後。
1
您可以查看my answer in a previous SO question,其中包含使用JProgressBar
的示例,該示例使用SwingWorker
從另一個Thread
獲取更新。是否使用SwingWorker
取決於您的用例。如果該功能需要一段時間才能運行,則最好使用SwingWorker
以避免阻塞UI。
+0
+1,我總是儘可能遠離SwingWorker,但看起來像這種情況是爲了SwingWorker :-)。不錯的代碼示例:-) – 2012-04-01 09:54:11
相關問題
- 1. 在後臺運行一個函數,同時啓動另一個函數
- 2. 在另一個線程中運行進度條
- 3. N次在另一個函數中運行一個函數
- 4. 當另一個任務正在運行時運行進度條android
- 5. javascript在另一個函數下運行一個函數
- 6. 如何從一個類同時運行兩個函數? C#
- 7. jQuery函數查找類和fadeToggle另一個div與同一類
- 8. 如何在另一個線程中運行一個函數
- 9. 當另一個結束時運行一個函數,iOS,Swift
- 10. 如何在python中的另一個def函數中運行一個def函數?
- 11. 在C++中的同一類中的另一個函數內使用類函數
- 12. 在JQuery中運行另一個函數之後加載函數
- 13. 停止一個函數,如果另一個正在運行
- 14. 如何在另一個完成後運行一個lambda函數
- 15. PHP - 如何從一個類中的一個類中運行一個函數?
- 16. 從另一個佈局進度條,並在另一個佈局中顯示它
- 17. 如何在類中調用函數以在php中運行另一個類?
- 18. 在另一個類中調用一個類的構造函數
- 19. 運行時更換/交換基類與另一個基類
- 20. 如何在一個不同的類中運行一個類?
- 21. 與不同參數並行運行一個函數 - python
- 22. PHP:運行另一個類方法時運行類方法?
- 23. 在保存後類型時運行在wordpress中運行一個函數
- 24. Javascript - 在另一個函數加載後運行函數*?
- 25. 實現一個進度條類
- 26. 找出多少個線程同時運行一個函數
- 27. 如何在同一個對象中調用另一個函數?
- 28. 運行一個接一個的函數
- 29. 從另一個類調用函數,從另一個類
- 30. 同一個js函數可以同時進行AJAX調用嗎?
實際上,在一個函數內部,一個while循環正在運行,所以我不能在每隔幾行代碼之後設置進度條的值.... – Xara 2012-04-01 08:14:08
然後,我想你在開始和結束後只是簡單的設置了'setIndeterminate(true)'循環按照答案中的建議進行操作,以向用戶顯示結果。我修改了類似的程序,給出了一個想法:-) – 2012-04-01 08:28:23