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

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

问题描述

与我的previous问题:在从另一个类调用repaint Java的?

Related to my previous question: Call repaint from another class in Java?.

我是新来的Java,和我有一个看的SwingWorker上的一些教程,但我不能确定如何与例如code我在previous问题了。实现它

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

任何人都可以解释如何使用SwingWorker类的问候我的code的一点​​,或点我朝着一个像样的教程吗?我已经看过,但我不知道我的理解呢。

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

推荐答案

一般来说,SwingWorker类是用来在摇摆执行长时间运行的任务。

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

运行在事件指派线程(EDT)长时间运行的任务可能会导致GUI锁定起来,这是做了这样的事情之一就是使用<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29\"><$c$c>SwingUtilities.invokeLater和<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeAndWait%28java.lang.Runnable%29\"><$c$c>invokeAndWait这使响应的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 来返回数据原来的方法。这是<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html\"><$c$c>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

下面是一个例子,其中一个的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();
        }
    }
}

doInBackground GET 方法被指定为第一种类型的<$ C $的返回类型C>的SwingWorker ,而第二类是用于返回的发布过程方法,这没有在本实施例中使用

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 执行方法被调用。在这个例子中,我们就帮一个的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 ,并点击得到一个消息框,一秒钟后。以下可用于初始化GUI的Swing应用程序:

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天全站免登陆