gpt4 book ai didi

java - 进度监视器 :Progress interval

转载 作者:行者123 更新时间:2023-11-30 08:52:32 26 4
gpt4 key购买 nike

如下代码所示,我有 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;
}

最佳答案

int numberOfFunctions = 20;
int progress = 0;
...
function1();
setProgress((++progress * 100) / numberOfFunctions); // progress is 5%
...
function20();
setProgress((++progress * 100) / numberOfFunctions); // progress is 100%

关于java - 进度监视器 :Progress interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30153033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com