JEditoPane和JTextPane

   JEditoPane和JTextPane均是帶格式的文本組件,其中JTextPane是JEditorPane的子類。

   1、使用EditorPane顯示HTML文件

   JEditorPane editorPane = new JEditorPane();
   editorPane.setEditable(false);
   java.net.URL helpURL = TextSamplerDemo.class.getResource(
                                "TextSamplerDemoHelp.html");
   if (helpURL != null) {
    try {
        editorPane.setPage(helpURL);
    } catch (IOException e) {
        System.err.println("Attempted to read a bad URL: " + helpURL);
    }
   } else {
    System.err.println("Couldn't find file: TextSamplerDemoHelp.html");
   }
   //Put the editor pane in a scroll pane.
   JScrollPane editorScrollPane = new JScrollPane(editorPane);
   editorScrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   editorScrollPane.setPreferredSize(new Dimension(250, 145));
   editorScrollPane.setMinimumSize(new Dimension(10, 10));

   2、JEditorPane VS JTextPane

   (1)鈞通過setPage方法從某個URL加載文本,注意,setPage方法會引起JEditorPane對應的Document和Editorkit發生改變,比如一個JEditorPane原來只包含普通文本,通過setPage加載一個HTML文檔,Document會變成HTMLDocument,Editorkit會變成HTMLEditorkit。

    (2)JEditorPane知道如何讀、寫、編輯普通文本、HTML文本、RTF文本。JTextPane繼承於JEditorPane,因此也具備上述能力,但做了一些限制。JTextPane始終堅持其Document實現StyledDocument的接口,比如HTMLDocument或RTFDocument。

   (3)在JTextPane中可以直接嵌入圖片或其他組件,比如按鈕,但在JEditorPane中只能從導入網頁間接獲得。

發佈了13 篇原創文章 · 獲贊 0 · 訪問量 1955
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章