gpt4 book ai didi

Java Event-Dispatch Thread 示例程序挂起

转载 作者:行者123 更新时间:2023-12-02 06:52:56 25 4
gpt4 key购买 nike

我已阅读关于 EDT(事件调度线程)的这篇文章 javaworld.com它展示了如何在 EDT 上正确设置 Swing GUI 并将修改 GUI 的长时间运行任务放入 Runnables 中。

这一切都是有道理的,但是示例程序(我粘贴在下面)中我所做的唯一修改是 Thread.sleep(6000) 来模拟长延迟,使得界面在几秒钟内没有响应。

我错过了什么吗?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ViewPage
{

public static void main(String[] args)
{
Runnable r;
r = new Runnable()
{
@Override
public void run()
{
final JFrame frame = new JFrame("View Page");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(new JLabel("Enter URL"));
final JTextField txtURL = new JTextField(40);
panel.add(txtURL);
frame.getContentPane().add(panel, BorderLayout.NORTH);
final JTextArea txtHTML = new JTextArea(10, 40);
frame.getContentPane().add(new JScrollPane(txtHTML),
BorderLayout.CENTER);
ActionListener al;
al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
txtURL.setEnabled(false);
Runnable worker = new Runnable()
{
public void run()
{
InputStream is = null;
try
{
URL url = new URL(txtURL.getText());
is = url.openStream();
final StringBuilder sb;
sb = new StringBuilder();
int b;
while ((b = is.read()) != -1)
{
sb.append((char) b);
}
Runnable r = new Runnable()
{
public void run()
{
try
{
Thread.sleep(6000);
}
catch (InterruptedException ex)
{
Logger.getLogger(ViewPage.class.getName()).log(Level.SEVERE, null, ex);
}
txtHTML.setText(sb.toString());
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
}
catch (final IOException ioe)
{
Runnable r = new Runnable()
{
public void run()
{
txtHTML.setText(ioe.getMessage());
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
}
finally
{
Runnable r = new Runnable()
{
public void run()
{
txtHTML.setCaretPosition(0);
txtURL.setEnabled(true);
}
};
try
{
EventQueue.invokeAndWait(r);
}
catch (InterruptedException ie)
{
}
catch (InvocationTargetException ite)
{
}
if (is != null)
{
try
{
is.close();
}
catch (IOException ioe)
{
}
}
}
}
};
new Thread(worker).start();
}
};
txtURL.addActionListener(al);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

最佳答案

Am I missing something?

是的。

Runnable 只是一个接口(interface),而不是另一个线程。通过这一行,您将调用包装为稍后在 Event Dispatch Thread 中执行。 .

EventQueue.invokeLater(r); // you can use SwingUtilities.invokeLater(r) too 

然后你打电话

Thread.sleep(6000);

这在 EDT 中执行,并使 gui 没有响应直到完成。

对于长时间任务,您应该使用另一个线程或 SwingWorker对于一些重复的短期任务SwingTimer 。了解 Concurrency in Swing

关于Java Event-Dispatch Thread 示例程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17798986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com