我有一个加载新闻的课程。
package swing;
import java.awt.Color;
import java.net.URL;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import net.Logger;
import net.Util;
public class WebPanel extends JScrollPane implements Runnable {
private JTextPane editorPane;
private String link;
public WebPanel(final String link) {
this.link = link;
editorPane = new JTextPane();
editorPane.setContentType("text/html");
editorPane.setBackground(Color.DARK_GRAY);
editorPane.setEditable(false);
editorPane.setMargin(null);
editorPane.setBorder(null);
setBorder(null);
editorPane.setBackground(Color.DARK_GRAY);
editorPane.setText("<html><body><font color=\"#808080\"><br><center>Getting data</center></font></body></html>");
}
@Override
public void run() {
try {
editorPane.setPage(new URL(link));
} catch (Exception e) {
Logger.logError("setting web page failed ", e);
editorPane.setContentType("text/html");
editorPane.setText("<html><body><font color=\"#808080\"><br><center>Failed to get data<br>" + e.toString() + "</center></font></body></html>");
}
editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent he) {
if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
Util.openLink(he.getURL().toURI());
} catch (Exception e) {
Logger.logError("hyperlinkUpdate failed", e);
}
}
}
});
setViewportView(editorPane);
}
public String GetLink() {
return editorPane.getPage().toString();
}
public final void setLink(final String link) {
this.link = link;
}}
它是可运行的,但是当我更新页面时(我有 GUI 类我这样做)Lo
public static WebPanel scrollPane = new WebPanel(Util.newslink);
...
LoginForm.scrollPane.setLink(Util.newslink);
new Thread(LoginForm.scrollPane).start();
我的程序在加载页面之前无法运行(无法按下任何按钮)。我尝试使用创建线程invokeLater()
,但没有任何帮助。