2

我正在尝试使用非JTextAreaSwing 文本组件,并在此代码中尝试在JTextPane. 我能够阅读网页并将其放入JTextPane's 文档中,如我打印出调用getTextmy时返回的字符串时所示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();
         }
      });
   }
}
4

2 回答 2

7

的调用setEditorKit()会删除您最初分配的文档并将其替换为新文档。只需在之后添加另一行即可恢复正确的文档。

htmlPane.setEditorKit(htmlKit);
htmlPane.setDocument(htmlDocument);

或从您的文本窗格中重新获取文档

htmlPane.setEditorKit(htmlKit);
htmlDocument = (HTMLDocument) htmlPane.getDocument();
于 2011-06-11T05:21:46.713 回答
5

您无需了解正在使用的实际编辑器工具包或文档:

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

public class EditorPaneLoad extends JFrame
{
    public EditorPaneLoad()
        throws Exception
    {
        FileReader reader = new FileReader("a.html");
//      JEditorPane editor = new JEditorPane();
        JTextPane editor = new JTextPane();
        editor.setContentType( "text/html" );
        editor.setEditable( false );
        editor.read(reader, null);
        System.out.println(editor.getText());
        System.out.println("\n------------\n");
        Document doc = editor.getDocument();
        System.out.println(doc.getText(0, doc.getLength()));
        JScrollPane scrollPane = new JScrollPane( editor );
        scrollPane.setPreferredSize( new Dimension(300, 200) );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
        throws Exception
    {
        EditorPaneLoad frame = new EditorPaneLoad();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
于 2011-06-11T15:41:23.800 回答