HTML 页面未显示在 Java Swing JTextPane 中
我正在尝试使用非 JTextArea
Swing 文本组件,并在这段代码中尝试在 JTextPane
中显示一个非常简单的网页。我能够读取网页并将其放入 JTextPane
的文档中,如打印出在我的计算机上调用 getText
时返回的字符串时所示HTMLDocument
,但 JTextPane 中没有显示任何内容。我觉得我好像缺少一些基本的东西。提前致谢。
我的SSCCE:
import java.awt.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
@SuppressWarnings("serial")
public class TestStyledDoc2 extends JPanel {
public static final String GETTY_FILE = "http://www.d.umn.edu/~rmaclin/" +
"gettysburg-address.html";
private HTMLEditorKit htmlKit = new HTMLEditorKit();
private HTMLDocument htmlDocument = (HTMLDocument) htmlKit.createDefaultDocument();
private JTextPane htmlPane = new JTextPane(htmlDocument);
public TestStyledDoc2() {
JScrollPane scrollPane1 = new JScrollPane(htmlPane);
try {
htmlPane.setEditorKit(htmlKit);
URL gettyUrl = new URL(GETTY_FILE);
htmlKit.read(gettyUrl.openStream(), htmlDocument, 0);
System.out.println(htmlDocument.getText(0, htmlDocument.getLength()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
scrollPane1.getViewport().setPreferredSize(new Dimension(400, 400));
setLayout(new BorderLayout());
add(scrollPane1, BorderLayout.CENTER);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TestStyledDoc");
frame.getContentPane().add(new TestStyledDoc2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
I'm trying to experiment with the non-JTextArea
Swing text components, and in this code am trying to display a very simple web page in a JTextPane
. I'm able to read the web page and able to put it into the JTextPane
's document, as shown when I print out the String that returns on calling getText
on my HTMLDocument
, but nothing shows up in the JTextPane. I feel as though I'm missing something basic. Thanks in advance.
My SSCCE:
import java.awt.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
@SuppressWarnings("serial")
public class TestStyledDoc2 extends JPanel {
public static final String GETTY_FILE = "http://www.d.umn.edu/~rmaclin/" +
"gettysburg-address.html";
private HTMLEditorKit htmlKit = new HTMLEditorKit();
private HTMLDocument htmlDocument = (HTMLDocument) htmlKit.createDefaultDocument();
private JTextPane htmlPane = new JTextPane(htmlDocument);
public TestStyledDoc2() {
JScrollPane scrollPane1 = new JScrollPane(htmlPane);
try {
htmlPane.setEditorKit(htmlKit);
URL gettyUrl = new URL(GETTY_FILE);
htmlKit.read(gettyUrl.openStream(), htmlDocument, 0);
System.out.println(htmlDocument.getText(0, htmlDocument.getLength()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
scrollPane1.getViewport().setPreferredSize(new Dimension(400, 400));
setLayout(new BorderLayout());
add(scrollPane1, BorderLayout.CENTER);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TestStyledDoc");
frame.getContentPane().add(new TestStyledDoc2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
setEditorKit()
会删除您最初分配的文档并将其替换为新文档。只需在后面添加另一行即可恢复正确的文档。或从文本窗格重新获取文档
The call of
setEditorKit()
removes your initially assigned document and replaces it with a new one. Just add another line right after to restore the correct document.or reget the document from your textpane
您不需要了解正在使用的实际编辑器工具包或文档:
You don't need to be aware of the actual editor kit or document that is being used: