使用 JProgressBar 运行 JFrame

发布于 2024-12-18 04:09:23 字数 450 浏览 5 评论 0原文

public void myMethod {
   MyProgessBarFrame progFrame = new MyProgressBarFrame(); // this is a JFrame
   progFrame.setVisible(true); // show my JFrame loading

   // do some processing here while the progress bar is running
   // .....

   progFrame.setvisible(false); // hide my progress bar JFrame
} // end method myMethod

我有上面的代码。但是当我运行它时,直到我关闭进度条 JFrame 后,“执行一些处理”部分才会进行处理。

我将如何显示进度条并告诉 Java 继续执行 do 处理部分?

public void myMethod {
   MyProgessBarFrame progFrame = new MyProgressBarFrame(); // this is a JFrame
   progFrame.setVisible(true); // show my JFrame loading

   // do some processing here while the progress bar is running
   // .....

   progFrame.setvisible(false); // hide my progress bar JFrame
} // end method myMethod

I have the code above. But when I run it, the do some processing section does not process until I close the progress bar JFrame.

How will I show my progress bar and tell Java to continue in the do processing section?

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

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

发布评论

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

评论(2

冬天旳寂寞 2024-12-25 04:09:23

您遇到了并发和 Swing 的经典问题。您的问题是,您正在主 Swing 线程(EDT 或事件调度线程)上执行一项长时间运行的任务,这将锁定该线程直到该进程完成,从而阻止它执行其任务,包括与用户交互和绘制 GUI 图形。

解决方案是在后台线程(例如 SwingWorker 对象给出的线程)中执行长时间运行的任务。然后,您可以通过 SwingWorker 的发布/处理对更新进度条(如果是决定因素)。有关这方面的更多信息,请阅读这篇关于 Swing 中的并发 的文章。

例如,

public void myMethod() {
  final MyProgessBarFrame progFrame = new MyProgessBarFrame();
  new SwingWorker<Void, Void>() {
     protected Void doInBackground() throws Exception {

        // do some processing here while the progress bar is running
        // .....
        return null;
     };

     // this is called when the SwingWorker's doInBackground finishes
     protected void done() {
        progFrame.setVisible(false); // hide my progress bar JFrame
     };
  }.execute();
  progFrame.setVisible(true);
}

另外,如果这是从另一个 Swing 组件显示的,那么您可能应该显示模式 JDialog 而不是 JFrame。这就是为什么我在 SwingWorker 代码之后在窗口上调用 setVisible(true) 的原因——这样,如果它是一个模态对话框,它就不会阻止 SwingWorker 的执行。

You've got a classic problem with concurrency and Swing. Your problem is that you're doing a long-running task on the main Swing thread, the EDT or Event Dispatch Thread, and this will lock the thread until the process is complete, preventing it from doing its tasks including interacting with the user and drawing GUI graphics.

The solution is to do the long-running task in a background thread such as that given by a SwingWorker object. Then you can update the progressbar (if determinant) via the SwingWorker's publish/process pair. For more on this, please read this article on Concurrency in Swing.

e.g.,

public void myMethod() {
  final MyProgessBarFrame progFrame = new MyProgessBarFrame();
  new SwingWorker<Void, Void>() {
     protected Void doInBackground() throws Exception {

        // do some processing here while the progress bar is running
        // .....
        return null;
     };

     // this is called when the SwingWorker's doInBackground finishes
     protected void done() {
        progFrame.setVisible(false); // hide my progress bar JFrame
     };
  }.execute();
  progFrame.setVisible(true);
}

Also, if this is being displayed from another Swing component, then you should probably show a modal JDialog not a JFrame. This is why I called setVisible(true) on the window after the SwingWorker code -- so that if it is a modal dialog, it won't prevent the SwingWorker from being executed.

左秋 2024-12-25 04:09:23

您可能已经注意到,当显示进度条时,随着进度条的处理,实际任务也正在完成。所以从这里你就可以明白是有多线程在进行了。

在 Java 中,您可以创建一个单独的线程来仅显示进度条,并在主线程中执行您的任务。因此,这两个进程将同时运行,它将满足您的需求。

*注意:进度条中显示的进度应取决于主线程中正在进行的处理。

您可以检查这些链接的线程 & Java 中的进度条

You would have noticed that when a progress bar is shown, along with processing of the progress bar the actual task is also being done. So you can understand from here that there is multi-threading going on.

In Java you can make a separate thread to just show the progress bar and in main thread you can do your task. So both the processes will run simultaneously and it will serve your need.

*NOTE : the progress shown in progress bar should depend on processing being done in main thread.

you can check these links for Threads & Progress Bar in Java.

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