我想知道在JTextPane中编辑内容(设置字符串不同的属性)然后能用getText()方法得到html文档格式。关键是JTextPane中的文本属性是怎么设置的?JEditorPane是怎么用的?
例如:在JTextPane中输入“你好”(红色),调用getText()方法后输出:
<html>
  <head>  </head>
  <body>
    <p style="margin-top: 0">
      <font color="#ff0000">你好</font>
    </p>
  </body>
</html>
最好能添加一个按钮能改变字体的样式,例如将字体改为粗体等当然调用getText()方法后得到的被容也要变化,谢谢!
问题补充:
java HTML文件文档编辑器 使用 JTextPane

解决方案 »

  1.   


    java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置,很全!!
      

  2.   

    大家还是看看这个吧,感觉挺好的,我就是用这个改的我的代码
    http://blog.csdn.net/rcyl2003/archive/2007/05/29/1629985.aspx
      

  3.   

    使用HTML的文档格式:HTMLEditorKit editorKit = new HTMLEditorKit();
    HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument();textPane = new JTextPane(document);
    textPane.setContentType("text/html");JEditorPane已经提供了字体,颜色,字号,加粗等基本操作的ActionAction[] actions = TextAction.augmentList(editorPane.getActions(), defaultActions);
    //颜色:ActionListener colorActionListener = new StyledEditorKit.ForegroundAction("set-foreground-", color);
    colorActionListener.actionPerformed(new ActionEvent(editorPane, 0, ""));//字体:
    Action fontAction = new StyledEditorKit.FontFamilyAction(font, font);
    fontAction.actionPerformed(new ActionEvent(fontAction, 0,""));
    //字体大小设置
    Action fontSizeAction = new StyledEditorKit.FontSizeAction(
    fontsize, Integer.parseInt(fontsize));
    fontSizeAction.actionPerformed(new ActionEvent(fontSizeAction, 0,
    sendCotent.getSelectedText()));
    //行间距:if(editorPane instanceof JTextPane)
    {
    StyledDocument doc = ((JTextPane) editorPane).getStyledDocument();
    MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setLineSpacing(attr, (float) DoubleUtil.getAsDouble(t));
    doc.setParagraphAttributes(editorPane.getSelectionStart(), editorPane.getSelectionEnd(), attr, false);}//插入图片(针对JTextPane):((JTextPane)editorPane).insertIcon(new ImageIcon(file.getPath()));