1

我正在尝试从我的JTextPane.

问题是,当我插入带有指定AttributeSet的文本时,尝试将其写入文件时不会输出内容文本,但样式是。

我不确定这是否与我如何插入文本或我如何尝试将其写入文件有关。

这是一个例子:

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.BorderLayout;
import java.io.*;
import java.awt.Color;

public class SOExample extends JFrame
{
    public static void main (String[] args)
    {
        SwingUtilities.invokeLater(
            new Runnable()
            {
                @Override
                public void run()
                {
                    SOExample aFrame = new SOExample();
                    aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    aFrame.setVisible(true);
                }
            }
        );
    }

    public SOExample()
    {
        initComponents();
        addText("This is my plain text", null);

        SimpleAttributeSet BOLD = new SimpleAttributeSet();
        StyleConstants.setBold(BOLD, true);
        StyleConstants.setForeground(BOLD, Color.BLUE);
        addText("This is my BLUE BOLD text",BOLD);

        outputHTMLfile();
    }

    private void initComponents()
    {
        this.setBounds(300,300,300,300);
        jtp = new JTextPane();
        jtp.setContentType("text/html");
        jsp = new JScrollPane();
        JPanel jp = new JPanel(new BorderLayout());
        jp.add(jtp);
        jsp.add(jp);
        jsp.setViewportView(jp);
        this.add(jsp, BorderLayout.CENTER);
    }

    private void addText(String text, SimpleAttributeSet attr)
    {
        try
        {
            HTMLDocument doc = (HTMLDocument)jtp.getDocument();
            doc.insertString(doc.getLength(), text +"\n", attr);
        }
        catch (BadLocationException blex)
        {
            blex.printStackTrace();
        }
    }

    private void outputHTMLfile()
    {
        File f = new File("C:\\Temp", "TestFile.html");
        try
        {
            BufferedOutputStream br = new BufferedOutputStream(new FileOutputStream(f));
            HTMLEditorKit kit = new HTMLEditorKit();
            kit.write(br, (HTMLDocument)jtp.getDocument(),  0, ((HTMLDocument)jtp.getDocument()).getLength() );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }   
    }

    private JTextPane jtp;
    private JScrollPane jsp;
    }

这会给我这样的html输出文件

 <html>
   <head>

   </head>
   <body>
     <p style="margin-top: 0">
       This is my plain text
     </p>
     <p style="margin-top: 0">
       <b><font color="#0000ff"><p>
 </font></b>    </p>
   </body>
 </html>

如您所见,这缺少文本"This is my BLUE BOLD text",但它会在框架中正确显示。

我也试过jtp.getText()直接写入文件并得到相同的结果。

4

1 回答 1

2

当我测试你的代码时,我发现了一些奇怪的东西。如果您仔细查看 JTextPane 上的最后一个字符串(这是我的 BLUE BOLD 文本),虽然它以粗体显示,但它的字体与前面的文本并不完全相同。

粗体无修正

这是一个明确的迹象,表明某些属性已经丢失。另请注意,插入该字符串(上面发布的那个)后生成的 HTML 缺少一个</p>标签:在字体标签之后打开一个新段落。再一次,在路上丢失了一些东西。

那么,这里发生了什么?好吧,当您将属性传递给insertString方法时,这些将覆盖任何已经存在的属性。您需要做的是,在插入之前,将这些属性与新的粗体和颜色属性合并。实际上,您必须稍微更改构造函数的代码:

public SOExample() {
    initComponents();
    addText("This is my plain text", null);

    //Retrieve existing attributes.
    SimpleAttributeSet previousAttribs = new SimpleAttributeSet
                                             (jtp.getInputAttributes()
                                                 .copyAttributes());

    SimpleAttributeSet BOLD = new SimpleAttributeSet();
    StyleConstants.setBold (BOLD, true);
    StyleConstants.setForeground (BOLD, Color.BLUE);

    //Merge new attributes with existing ones.    
    previousAttribs.addAttributes (BOLD);

    //Insert the string and apply merged attributes.
    addText ("This is my BLUE BOLD text", previousAttribs);

    outputHTMLfile();

}

加粗修复

看出字体的不同了吗?至于getInputAttributes()在代码中的coppyAttributes()中,它给出了将在任何后续插入中使用的当前属性集。这几乎是我们所需要的。

您可能想知道如果尚未插入文本,可能存在哪些属性。简而言之,文本中的每个元素,即文本,都将由一个称为content的特殊内部“标签”标识。此标记作为属性存储。正是这种属性的丧失在这里造成了严重破坏。

希望这可以帮助!

于 2013-11-04T17:53:40.960 回答