如何在 Java 中使用 SwingWorker? [英] How do I use SwingWorker in Java?

查看:100
本文介绍了如何在 Java 中使用 SwingWorker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我之前的问题有关:从 Java 中的另一个类调用重绘?

我是 Java 新手,我看过一些关于 SwingWorker 的教程.但是,我不确定如何使用我在上一个问题中提供的示例代码来实现它.

I'm new to Java and I've had a look at some tutorials on SwingWorker. Yet, I'm unsure how to implement it with the example code I gave in the previous question.

任何人都可以解释如何使用 SwingWorker 来处理我的代码片段和/或为我指出一个体面的教程吗?我看过了,但我不确定我是否理解.

Can anyone please explain how to use SwingWorker with regards to my code snippet and/or point me towards a decent tutorial? I have looked but I'm not sure I understand yet.

推荐答案

一般来说,SwingWorker 用于在 Swing 中执行长时间运行的任务.

Generally, SwingWorker is used to perform long-running tasks in Swing.

在事件调度线程 (EDT) 上运行长时间运行的任务会导致 GUI 锁定,因此已完成的操作之一是使用 SwingUtilities.invokeLaterinvokeAndWait 保持 GUI 响应,通过在运行所需任务之前优先处理其他 AWT 事件(以 Runnable 的形式).

Running long-running tasks on the Event Dispatch Thread (EDT) can cause the GUI to lock up, so one of the things which were done is to use SwingUtilities.invokeLater and invokeAndWait which keeps the GUI responsive by which prioritizing the other AWT events before running the desired task (in the form of a Runnable).

然而,SwingUtilities 的问题在于它不允许将数据从执行的 Runnable 返回到原始方法.这就是 SwingWorker 旨在解决.

However, the problem with SwingUtilities is that it didn't allow returning data from the the executed Runnable to the original method. This is what SwingWorker was designed to address.

Java 教程有一节介绍了 SwingWorker.

The Java Tutorial has a section on SwingWorker.

这是一个示例,其中 SwingWorker 用于在单独的线程上执行耗时的任务,并在一秒钟后显示一个带有答案的消息框.

Here's an example where a SwingWorker is used to execute a time-consuming task on a separate thread, and displays a message box a second later with the answer.

首先,将创建一个扩展 SwingWorker 的类:

First off, a class extending SwingWorker will be made:

class AnswerWorker extends SwingWorker<Integer, Integer>
{
    protected Integer doInBackground() throws Exception
    {
        // Do a time-consuming task.
        Thread.sleep(1000);
        return 42;
    }

    protected void done()
    {
        try
        {
            JOptionPane.showMessageDialog(f, get());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

doInBackgroundget方法的返回类型指定为SwingWorker的第一种类型,第二种类型为用于返回 publishprocess 方法,本示例中未使用.

The return type of the doInBackground and get methods are specified as the first type of the SwingWorker, and the second type is the type used to return for the publish and process methods, which are not used in this example.

然后,为了调用SwingWorker,调用execute方法.在这个例子中,我们将一个 ActionListener 挂接到一个 JButton 来执行 AnswerWorker:

Then, in order to invoke the SwingWorker, the execute method is called. In this example, we'll hook an ActionListener to a JButton to execute the AnswerWorker:

JButton b = new JButton("Answer!");
b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        new AnswerWorker().execute();
    }
});

上面的按钮可以被添加到一个JFrame中,点击之后会出现一个消息框.以下内容可用于初始化 Swing 应用程序的 GUI:

The above button can be added to a JFrame, and clicked on to get a message box a second later. The following can be used to initialize the GUI for a Swing application:

private void makeGUI()
{
    final JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new FlowLayout());

    // include: "class AnswerWorker" code here.
    // include: "JButton" b code here.

    f.getContentPane().add(b);
    f.getContentPane().add(new JButton("Nothing"));
    f.pack();
    f.setVisible(true);
}

应用程序运行后,将出现两个按钮.一个标有回答!"和另一个无".当您点击答案"时!按钮,一开始什么都不会发生,但点击无"按钮将起作用并证明 GUI 具有响应性.

Once the application is run, there will be two buttons. One labeled "Answer!" and another "Nothing". When one clicks on the "Answer!" button, nothing will happen at first, but clicking on the "Nothing" button will work and demonstrate that the GUI is responsive.

并且,一秒钟后,AnswerWorker 的结果将出现在消息框中.

And, one second later, the result of the AnswerWorker will appear in the message box.

这篇关于如何在 Java 中使用 SwingWorker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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