`
joyo_fly
  • 浏览: 87015 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

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中只能从导入网页间接获得。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics