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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值