Java Backgroundworker:要更新的 Widget 范围尚不清楚

发布于 2024-08-26 18:16:09 字数 868 浏览 8 评论 0 原文

我试图理解 org.jdesktop.swingx.BackgroundWorker 的机制。他们的 javadoc提供以下示例:

final JLabel label;
class MeaningOfLifeFinder implements BackgroundListener {
    public void doInBackground(BackgroundEvent evt) {
        String meaningOfLife = findTheMeaningOfLife();
        evt.getWorker().publish(meaningOfLife);
    }

    public void process(BackgroundEvent evt) {
        label.setText("" + evt.getData());
    }

    public void done(BackgroundEvent evt) {}
    public void started(BackgroundEvent evt) {}
}

(new MeaningOfLifeFinder()).execute();

除了我怀疑结果是否会被发布之外,我想知道如何将 label 传递到正在更新它的 process 方法。我认为它的范围仅限于 BackgroudListener 实现的外部。我很困惑......有什么答案可以给我吗?

提前致谢

I am trying to understand the mechanism of org.jdesktop.swingx.BackgroundWorker. Their javadoc presents following example:

final JLabel label;
class MeaningOfLifeFinder implements BackgroundListener {
    public void doInBackground(BackgroundEvent evt) {
        String meaningOfLife = findTheMeaningOfLife();
        evt.getWorker().publish(meaningOfLife);
    }

    public void process(BackgroundEvent evt) {
        label.setText("" + evt.getData());
    }

    public void done(BackgroundEvent evt) {}
    public void started(BackgroundEvent evt) {}
}

(new MeaningOfLifeFinder()).execute();

Apart from the fact that I doubt the result will ever get published, I wonder how label is passed to the process method, where it is being updated. I thought its scope was limited to the outside of the BackgroudListener implementation. Quite confused I am ... any answers for me?

Thanks in advance

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

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

发布评论

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

评论(2

独享拥抱 2024-09-02 18:16:09

这段代码显然已经过时且不正确;此外,您必须将其放回到某个上下文中(我猜 MeaningOfLifeFinder 是在另一个类中定义的)。

它不会编译,因为 BackgroundListener 不存在(应该是 BackgroundWorker)并且是一个类而不是接口,因此 implements 在这里不正确。

我认为该片段应如下所示:

class Something {
   final JLabel label;

   Something() {
       // Instantiate label here
   }

   class MeaningOfLifeFinder implements BackgroundWorker {
       public void doInBackground(BackgroundEvent evt) {
           String meaningOfLife = findTheMeaningOfLife();
           evt.getWorker().publish(meaningOfLife);
       }

       public void process(BackgroundEvent evt) {
           label.setText("" + evt.getData());
       }

       public void done(BackgroundEvent evt) {}
       public void started(BackgroundEvent evt) {}
   }

   void someAction() {
       (new MeaningOfLifeFinder()).execute();
   }
}

这应该效果更好。在这种情况下,您会立即看到 MeaningOfLifeFinder 的实例如何能够访问标签(因为它可以访问其嵌入类的任何成员)。

您只需确保以某种方式调用 someAction() (例如,通过用户操作)。

This snippet is obviously out of date and incorrect; in addition, you have to put it back into some context (I guess MeaningOfLifeFinder is defined inside another class).

It won't compile because BackgroundListener doesn't exist (should be BackgroundWorker) and is a class not an interface, hence implements is incorrect here.

I think the snippet should read like:

class Something {
   final JLabel label;

   Something() {
       // Instantiate label here
   }

   class MeaningOfLifeFinder implements BackgroundWorker {
       public void doInBackground(BackgroundEvent evt) {
           String meaningOfLife = findTheMeaningOfLife();
           evt.getWorker().publish(meaningOfLife);
       }

       public void process(BackgroundEvent evt) {
           label.setText("" + evt.getData());
       }

       public void done(BackgroundEvent evt) {}
       public void started(BackgroundEvent evt) {}
   }

   void someAction() {
       (new MeaningOfLifeFinder()).execute();
   }
}

This should work better. In this case, you immediately see how the instance of MeaningOfLifeFinder will be able to access label (since it can access any member of its embedding class).

You just have to make sure that someAction() gets called somehow (eg from a user action).

汹涌人海 2024-09-02 18:16:09

我不熟悉 < code>BackgroundWorker,但它“是基于 SwingWorker。"如果没有设置 ExecutorServiceBackgroundWorker 默认为 SwingWorker,这可能是一个更易于学习的模型。比较“BackgroundWorker" 和 "SwingWorker 可以同时操作三个。"

I'm unfamiliar with BackgroundWorker, but it "is built upon SwingWorker." Absent setting an ExecutorService, BackgroundWorker defaults to SwingWorker, which may be a more accessible model for study. Compare the "two threads involved in the life cycle of a BackgroundWorker" with "SwingWorker which can operate with three."

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