java jprogressbar在繁重的操作期间挂起 [英] java jprogressbar hangs during heavy operation

查看:149
本文介绍了java jprogressbar在繁重的操作期间挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个java程序,在我调用一个大量使用CPU的方法之前,我在其上显示一个带有JProgressBar的框架。虽然我在调用方法之前显示它,但是在方法结束之前不会显示JProgressBar。进度条不会以任何方式与方法交互(但)这就是我感到困惑的原因。它类似于:

I'm writing a java program and, before I call a method that makes heavy use of the CPU, I display a frame with a JProgressBar on it. Although I display it before the method is called, the JProgressBar is not shown until the method is over. The progress bar does not interact with the method in any way (yet) and that's why I am confused. It is something like:

progressbarframe.setVisible(true); // it is a indeterminate progress bar
heavyLoadMethod();
progressbarframe.dispose();

框架显示,但进度条在方法期间未初始化。有没有办法在不使用线程的情况下解决这个问题?什么是最简单的方法?

The frame shows, but the progress bar doesn't initialize during the method. Is there any way I can solve this without using threads? What would be the simplest way?

感谢您的时间

推荐答案

你应该在一个单独的线程中调用 heavyLoadMethod 。您可以使用 java.util.concurrent.Executor

You should call the heavyLoadMethod in a separate thread. You can use a java.util.concurrent.Executor for it:

Executor executor = java.util.concurrent.Executors.newSingleThreadExecutor();
executor.submit(new Runnable() { public void run() { heavyLoadMethod();}});

现在该方法将在一个单独的线程中调用,进度条将正确显示。

Now the method will be called in a separate thread and the progress bar will be displayed properly.

这篇关于java jprogressbar在繁重的操作期间挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆