我的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(); } }); }}解决方法 setEditorKit()的调用将删除最初分配的文档,并将其替换为新文档.只需在其后添加另一行即可恢复正确的文档.
HTMLPane.setEditorKit(HTMLKit);HTMLPane.setdocument(HTMLdocument);
或者从您的textpane中重新获取文档
HTMLPane.setEditorKit(HTMLKit);HTMLdocument = (HTMLdocument) HTMLPane.getdocument();总结
以上是内存溢出为你收集整理的HTML页面没有出现在Java Swing JTextPane中全部内容,希望文章能够帮你解决HTML页面没有出现在Java Swing JTextPane中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)