基本不确定JProgress栏用法 [英] Basic Indeterminate JProgress Bar Usage

查看:209
本文介绍了基本不确定JProgress栏用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在长时间下载时在框架的左下角设置一个不确定的JProgressBar动画。

I simply want to have an indeterminate JProgressBar animate in the bottom left corner of my frame when a long download is being done.

我看了很多教程,这一切对我来说都不清楚。我只是想在后台下载文件时让它具有动画效果。我尝试过这种方式的每种方式都没有为进度条设置动画,直到完成下载后

I've looked through many tutorials, none of which are clear to me. I simply want to have it animate while the file is being downloaded in the background. Each way I've tried this, it doesn't animate the progress bar until after the download is done.

我需要帮助知道哪里放置我的download()调用。

I need help knowing where to place my download() call.

class MyFunClass extends JFrame {
  JProgressBar progressBar = new JProgressBar();

  public void buttonClicked() {
    progressBar.setVisible(true);
    progressBar.setIndeterminate(true);

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        progressBar.setIndeterminate(true);
        progressBar.setVisible(true);

        // Do I do my download() in here??

    }});

    // Do download() here???
    progressBar.setVisible(false);
  }
}

提前致谢!


编辑:对于将来遇到类似问题的人,这是基本问题的基本解决方案。这不是我的代码,而是一般草图。内部 buttonClicked()

For those who have a similar issue to me in the future, this is the basic solution for a basic problem. This isn't my code verbatim, but a general sketch. Inside buttonClicked():

public void buttonClicked() {
  class MyWorker extends SwingWorker(String, Object) {
     protected String doInBackground() {
       progressBar.setVisible(true);
       progressBar.setIndeterminate(true);

       // Do my downloading code
       return "Done."
     }

     protected void done() {
        progressBar.setVisible(false)
     }
  }

  new MyWorker().execute();

}


推荐答案

您当前的位置代码显示没有创建后台线程,而是显示您尝试在Swing线程 Swing线程中对代码进行排队,这对此问题没有意义(虽然有时你可能想要这样做,但是再次,不是在这里)。成功的唯一方法是使用后台线程。标准Oracle JProgressBar教程 Swing中的并发贯穿所有这些。

Your current code shows no creation of a background thread, but rather it shows you trying to queue code on the Swing thread from within the Swing thread which doesn't make sense for this problem (although there are occasional times when you may want to do this, but again, not here). The only way for this to succeed is to use a background thread. The standard Oracle JProgressBar tutorial and Concurrency in Swing goes through all of this.

基本的是你必须从Swing Thread更新JProgressBar才能在后台线程中执行长时间运行的进程,例如SwingWorker对象提供的进程。我们在这里查看的内容太多了,所以我所能做的就是提供一个链接,但是一旦您查看了教程,我们将很乐意帮助您了解详细信息。如果您仍然遇到问题,请查看教程并回答您的具体问题。

The basic thing is that you must update the JProgressBar from the Swing Thread will doing your long-running process in the background thread, such as that provided by a SwingWorker object. There are too many details for us to review all here, and so all I can do is provide a link, but we'll be happy to help you understand the details once you review the tutorials. Just check the tutorials and come on back with your specific questions if you're still stuck.

编辑1

你声明:

Edit 1
You state:


我可以在buttonClicked()函数中创建一个新的线程对象吗?

can I just create a new thread object within the buttonClicked() function?

是的,您可以在 buttonClicked()内创建一个SwingWorker对象方法并在那里执行。

Yes, you can create a SwingWorker object inside of the buttonClicked() method and execute it there.


问题是我拥有我正在开发GUI的所有功能的API和库,它看起来像一个longwinded将该函数调用包装在一个线程中的解决方法。

The thing is I have my API and library of all the functionality that I'm developing the GUI to, and it seems like a longwinded workaround to wrap that function call in a thread.

抱歉,我不知道你在这里说什么或者你有什么问题认为线程会导致。 buttonClicked()方法可能必须在EDT上运行而不是在后台线程中运行。

Sorry, but I have no idea what you're saying here or what issues you think threading will cause. The buttonClicked() method likely must run on the EDT and not in a background thread.

另请注意,在我的大多数复杂的Swing GUI中,我经常在不同的(模型)对象中下载文件并在不同的对象中创建我的SwingWorker(控件)来自GUI对象(视图)。这样做可能看起来更复杂,但是当我这样做的时候, 很多 更容易调试,维护和增强我的程序,特别是当我大量使用时接口允许我单独测试所有程序组件。

Also note that in most of my more complex Swing GUI's, I often do my file downloading in a different (model) object and create my SwingWorker in a different object still (control) from the GUI object (the view). It may seem more complicated to do it this way, but it's a lot easier to debug, maintain and enhance my program when I do it this way, especially when I heavily use interfaces to allow me to test all program components in isolation.

编辑2

对解决方案发布的一些更正。您发布了:

Edit 2
Some corrections to your solution post. You posted:

public void buttonClicked() {
  class MyWorker extends SwingWorker(String, Object) {
     protected String runInBackground() {
       progressBar.setVisible(true);
       progressBar.setIndeterminate(true);

       // ...

有问题


  • 它是 doInBackground(),而不是 runInBackground()

  • 但更重要的是,你是在后台线程中进行Swing调用,这是永远不应该做的事情(除非调用是线程安全的,即便如此......) 。

  • it's doInBackground(), not runInBackground()
  • but more importantly, you're making Swing calls from within a background thread, something that should never be done (unless the call is thread safe, and even then...).

所以改变它:

public void buttonClicked() {
  progressBar.setVisible(true);
  progressBar.setIndeterminate(true);
  class MyWorker extends SwingWorker<String, Void> {
     protected String doInBackground() {

       // ...

这篇关于基本不确定JProgress栏用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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