如何在 Java 中使用 SwingWorker?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/782265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use SwingWorker in Java?
提问by Relequestual
Related to my previous question: Call repaint from another class in Java?
与我之前的问题相关:从 Java 中的另一个类调用重绘?
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.
我是 Java 新手,我看过一些关于 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 来处理我的代码片段和/或给我指出一个体面的教程吗?我看过了,但我不确定我是否理解。
采纳答案by coobird
Generally, SwingWorker is used to perform long-running tasks in Swing.
通常,SwingWorker 用于在 Swing 中执行长时间运行的任务。
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
).
在事件调度线程 (EDT) 上运行长时间运行的任务会导致 GUI 锁定,因此已完成的一件事是使用SwingUtilities.invokeLater
并invokeAndWait
保持 GUI 响应,在运行所需任务之前优先处理其他 AWT 事件(以 a 的形式Runnable
)。
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.
但是,问题SwingUtilities
在于它不允许将数据从执行Runnable
的方法返回到原始方法。这就是SwingWorker
旨在解决的问题。
The Java Tutorial has a section on SwingWorker.
Java 教程有一个关于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.
下面是一个示例,其中 aSwingWorker
用于在单独的线程上执行耗时的任务,并在一秒钟后显示一个带有答案的消息框。
First off, a class extending SwingWorker
will be made:
首先,SwingWorker
将进行一个类扩展:
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();
}
}
}
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.
doInBackground
andget
方法的返回类型指定为 的第一种类型,SwingWorker
第二种类型是publish
andprocess
方法用于返回的类型,本例中不使用。
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
:
然后,为了调用SwingWorker
,调用该execute
方法。在这个例子中,我们将把 an 挂接ActionListener
到 aJButton
来执行AnswerWorker
:
JButton b = new JButton("Answer!");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new AnswerWorker().execute();
}
});
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:
可以将上面的按钮添加到JFrame
,然后单击以稍后获得消息框。以下内容可用于初始化 Swing 应用程序的 GUI:
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);
}
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.
应用程序运行后,将出现两个按钮。一个标有“回答!” 和另一个“无”。当您点击“答案”时!按钮,一开始什么都不会发生,但是单击“无”按钮将起作用并证明 GUI 具有响应性。
And, one second later, the result of the AnswerWorker
will appear in the message box.
并且,一秒钟后,结果AnswerWorker
将出现在消息框中。
回答by dalvarezmartinez1
Agree:
同意:
Running long-running tasks on the Event Dispatch Thread (EDT) can cause the GUI to lock up.
在事件调度线程 (EDT) 上运行长时间运行的任务会导致 GUI 锁定。
Do not agree:
不同意:
so one of the things which were done is to use SwingUtilities.invokeLater and invokeAndWait which keeps the GUI responsive..
所以所做的一件事是使用 SwingUtilities.invokeLater 和 invokeAndWait 来保持 GUI 响应..
invokeLater still runs the code on the EDT, and can freeze your UI!! Try this:
invokeLater 仍然在 EDT 上运行代码,并且可以冻结您的 UI!尝试这个:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
At least I, cannot move my mouse once I click the button which triggers the actionPerformed with the above code. Am I missing something?
至少,一旦我点击了用上面的代码触发 actionPerformed 的按钮,我就无法移动我的鼠标。我错过了什么吗?