在java中使用图像报告运行任务的进度

发布于 2024-11-14 21:56:06 字数 489 浏览 4 评论 0原文

我正在尝试执行一个程序,该程序在为用户显示 GUI 后需要大约 6-7 秒 才能执行。这里的问题是用户必须等待 6-7 秒而没有任何处理或进度通知。

所以我想要做的是,当 MainApp ma = new MainApp(); 代码执行时,我想为用户显示一张 gif 图片程序正在执行。显示图片的代码写在 ShowProgressPic spp = new ShowProgressPic();

我有一个名为 end 的布尔数据类型变量,它将在 MainApp() 的最后一个语句中设置为 true > 构造函数。当MainApp ma = new MainApp();执行结束时,我想停止显示gif图像。

请帮我解决这个问题。 谢谢。

I am trying to execute a program which takes around 6-7 seconds to execute after displaying GUI for the user. Problem here is that the user has to wait for 6-7 seconds without any processing or progress notification.

So want i am trying to do is that, while the MainApp ma = new MainApp(); code executes, i want to display a gif picture for the user saying that the program is being executed. Displaying of picture code is written in
ShowProgressPic spp = new ShowProgressPic();

I have a boolean datatype variable named end which will be set to true in the last statement of MainApp() constructor. When the execution of the MainApp ma = new MainApp(); ends, i want to stop displaying the gif image.

Please help me out with this.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花之痕靓丽 2024-11-21 21:56:06

假设您的问题涉及 Swing GUI,我建议您使用 SwingWorker 在后台线程中执行长效程序。然后,当线程完成时,您有两种或多种方法可以从那里继续。您可以让 SwingWorker 在其 did 方法中调用代码来关闭进度显示,也可以让保存并执行 SwingWorker 的代码通过 PropertyChangeListener 侦听 SwingWorker,并在状态属性为 SwingWorker.StateValue.DONE 时执行操作。

Assuming that you're problem involves a Swing GUI, I would suggest that you use a SwingWorker to do the long-acting program execution in a background thread. Then when the thread is done, you have two or more ways of proceeding from there. You can have the SwingWorker call code in its done method to close the progress display or you can have the code that holds and executes the SwingWorker listen to the SwingWorker via a PropertyChangeListener and act when the state property is SwingWorker.StateValue.DONE.

最笨的告白 2024-11-21 21:56:06

查看线程。您必须在与主线程不同的线程中加载该进程才能显示进度。为了显示进度,您可以使用 JProgressBar,

这里是一个部分伪代码示例,其中包含用于显示百分比的输出打印:

int maxSufftoDo = 500;
new Thread() {
public void run() {
    while not done {
        do part of stuff;
        updateStatus(numOfStuffDone * 100 / maxStufftoDo);
    }
}
}.start();

public void updateStatus(int s) {
    System.out.println(i + "% done");
}

look into threads. You would have to load the process in a different thread from the main therad to show the progress. For showing the progress you could use JProgressBar

here is a part-pseudocode example example with a output print for displaying percent:

int maxSufftoDo = 500;
new Thread() {
public void run() {
    while not done {
        do part of stuff;
        updateStatus(numOfStuffDone * 100 / maxStufftoDo);
    }
}
}.start();

public void updateStatus(int s) {
    System.out.println(i + "% done");
}
雨后彩虹 2024-11-21 21:56:06

您应该在 GUI 中使用事件和侦听器进行多线程处理(通常也推荐使用)。

You should use Events and Listeners for multithreading in GUI (and in general also recommended).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文