2011-06-11 86 views
4

我試圖用非JTextArea Swing文本組件進行實驗,並在此代碼中試圖在JTextPane中顯示一個非常簡單的網頁。我可以讀取網頁並將其放入JTextPane的文檔中,如我在HTMLDocument上打印返回的調用getText時返回的字符串所示,但在JTextPane中不顯示任何內容。我覺得我好像缺少一些基本的東西。提前致謝。HTML頁面不顯示在Java Swing 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(); 
     } 
     }); 
    } 
} 
+1

嘗試的JTextPane。 setPage(URL)來查看它是否可以呈現您的頁面。 – 2011-06-11 05:21:42

回答

7

setEditorKit()呼叫刪除您最初指定文件,並用新的替換它。只需添加另一行即可恢復正確的文檔。

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

或從textpane

htmlPane.setEditorKit(htmlKit); 
htmlDocument = (HTMLDocument) htmlPane.getDocument(); 
+1

感謝您的快速,簡短和正確的答覆!榮譽給你! – 2011-06-11 05:43:07

5

你並不需要知道正在使用的實際編輯器工具包或文檔reget文件:

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); 
    } 
}