【问题标题】:Progress Monitor:Progress interval进度监视器:进度间隔
【发布时间】:2015-07-21 02:14:36
【问题描述】:

如下代码所示,我有 4 个函数,我手动设置进度监视器的每个进度间隔 = 25%。

但如果我有 20 个或更多功能,我的方法手动设置进度间隔将更加不切实际。

在给定 X 个函数的情况下,设置进度间隔的更好方法是什么?

class Task extends SwingWorker<Void, Void> {
    @Override
    public Void doInBackground() {
        int progress = 0;
        setProgress(0);
        while (progress < 100 && !isCancelled()) 
        {   
            functionA();
            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex) {
                ex.printStackTrace();
            }
            progress += 25;
            setProgress(Math.min(progress, 100));

            taskOutput.setText("Step 2");
            functionB();
            progress += 25;
            setProgress(Math.min(progress, 100));

            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex) {
                ex.printStackTrace();
            }

            taskOutput.setText("Step 3");
            functionC();
            progress += 25;
            setProgress(Math.min(progress, 100));

            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex) {
                ex.printStackTrace();
            }

            taskOutput.setText("Step 4");
            functionD();
            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex) {
                ex.printStackTrace();
            }
            progress += 25;
            setProgress(Math.min(progress, 100));
        }
        return null;
    }

【问题讨论】:

    标签: java progressmonitor


    【解决方案1】:
    int numberOfFunctions = 20;
    int progress = 0;
    ...
    function1();
    setProgress((++progress * 100) / numberOfFunctions);  // progress is 5%
    ...
    function20();
    setProgress((++progress * 100) / numberOfFunctions);  // progress is 100%
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2011-03-12
      • 2011-09-17
      相关资源
      最近更新 更多