简体   繁体   English

Java:在不知道确切执行时间的情况下使用进度条

[英]Java: Using Progress Bar without knowing the exact execution time

I have been reading for a while about progress bars, but I still can't find the answer to my question. 我已经阅读了一段时间的进度条,但仍然找不到我的问题的答案。 Maybe it is something that I haven't realized. 也许这是我没有意识到的。

http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

I have a program written in java, where I also calculate the time needed to be finished, using 我有一个用Java编写的程序,其中我还使用以下方法计算完成所需的时间:

static long startTime = System.currentTimeMillis();
...
long endTime   = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);

I would like to know, if it possible to create a progress bar, with the percentage of the program that is completed. 我想知道,是否有可能创建进度条,其中包含已完成程序的百分比。 I am really confused, because I don't know if I can calculate the time needed before the execution. 我真的很困惑,因为我不知道我是否可以计算出执行之前所需的时间。

I would appreciate any help. 我将不胜感激任何帮助。 Regards 问候

A progress par doesn't show the percentage of time elapsed but the percentage of work . 进度标准不显示经过的时间百分比,而是工作的百分比。

So if your code needs to process 470 objects (= max value). 因此,如果您的代码需要处理470个对象(=最大值)。 Each time an object is processed, increment progress by 1. 每次处理对象时,将progress增加1。

You need some way to measure the progress of your task. 您需要某种方式来衡量任务的进度。 Are you processing a known amount of data? 您正在处理已知数量的数据吗? You should be able to calculate how much progress you have made and then periodically update your progress bar with the new value. 您应该能够计算出已取得的进度,然后定期使用新值更新进度条。 For example set up you progress bar to have a range of 0 to 100. 例如,将进度条设置为0到100。

JProgressBar myProgressBar = new JProgressBar(0, 100);

As you process your data (in a new thread), update your progress bar in the Swing thread: 在处理数据时(在新线程中),在Swing线程中更新进度条:

int[] myData = // data to process

for (int ctr = 0; ctr < myData.length; ctr++) {
    doMyProcess(myData[ctr]);
    int percentComplete = (int)Math.round(ctr / (float)myData.length);
    myProgressBar.setValue(percentComplete);
}

If you cannot measure your progress in some way, then you will need to use an "indeterminate" progress bar, which will indicate that your program is doing something, but the task is not measurable. 如果您无法以某种方式衡量进度,则将需要使用“不确定”进度条,该进度条将指示您的程序正在执行某些操作,但是该任务无法衡量。

myProgressBar.setIndeterminate(true);

It's quite hard to estimate time needed for program to finish. 很难估计程序完成所需的时间。 But maybe you should rather concentrate on data set you're processing (files you're copying, database rows you're processing, etc...) and measure that instead? 但是也许您应该宁愿专注于正在处理的数据集(正在复制的文件,正在处理的数据库行等),然后进行衡量呢? It's something you probably know and can calculate completed percentage out of it. 您可能已经知道了这一点,可以从中计算出完成的百分比。 If you really don't have a finite data set, then maybe you should show a indeterminate progress bar instead of showing user a progress bar that doesn't give any meaningful information. 如果您确实没有有限的数据集,那么也许应该显示一个不确定的进度条,而不是向用户显示一个不提供任何有意义信息的进度条。

What you need in this case is indeterminate ProgrssBar . 在这种情况下,您需要的是不确定的ProgrssBar Here is a paragraph from official documentation : 这是官方文档中的一段:

Sometimes you can't immediately determine the length of a long-running task, or the task might stay stuck at the same state of completion for a long time. 有时您无法立即确定长时间运行的任务的长度,或者该任务可能长时间停留在相同的完成状态。 You can show work without measurable progress by putting the progress bar in indeterminate mode. 您可以通过将进度条置于不确定模式来显示没有可衡量进度的工作。 A progress bar in indeterminate mode displays animation to indicate that work is occurring. 不确定模式下的进度条显示动画,以指示正在进行工作。 As soon as the progress bar can display more meaningful information, you should switch it back into its default, determinate mode. 进度条可以显示更多有意义的信息后,应立即将其切换回默认的确定模式。

Below is the snippet to use ProgressBar in indeterminate mode 以下是在不确定模式下使用ProgressBar的代码段

public void propertyChange(PropertyChangeEvent evt) {
if (!done) {
    int progress = task.getProgress();
    if (progress == 0) {
        progressBar.setIndeterminate(true);
        taskOutput.append("No progress yet\n");
    } else {
        progressBar.setIndeterminate(false); 
        progressBar.setString(null);
        progressBar.setValue(progress);
        taskOutput.append(String.format(
                "Completed %d%% of task.\n", progress));
    }
}
}

The snippet has been taken from here . 该片段摘自此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM