我正在尝试创建简单的 Swing 应用程序,其中包含很少的 JEditorPanes。每个 JEditorPane 都包含带有 html 标记的文本。还有一些窗格包含带有标签的html <img src='http://some.url' />
,这意味着图像可能在网络的某个地方。问题是 - 如果其中一个图像 url 不可用 - 我的所有 JEditorPanes 和整个应用程序都会挂起。(我在自己的线程中构建 JEditorPanes,并在构建后将它们放入主框架中SwingUtilities.invokeLater(...)
)
我相信图像会异步下载到 JEditorPanes 中,是否有能力杀死这些挂起的图像下载线程?
或者,也许有更好的解决方案?
谢谢
使用 PS SwingWorker。问题是 - 如果某些图像 url 不可用 - 所有 JEditorPanes 都无法下载他们的图片。实际上他们没有挂起,但无法下载图像。为什么?
聚苯乙烯
后台线程:
JEditorPane jtp=new JEditorPane();
jtp.setContentType("text/html");
jtp.setPreferredSize(newDimension(20,250));
StringBuilder sb=new StringBuilder();
sb.append("<img src='").append(url).append("'/>");
jtp.setText(sb.toString());
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
myPanel.add(rigid,0);
myPanel.add(jtp,0);
myPanel.revalidate();
}
});