Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JTextPane获取html-缺少内容文本_Java_Html_Swing_File Io_Jtextpane - Fatal编程技术网

Java 从JTextPane获取html-缺少内容文本

Java 从JTextPane获取html-缺少内容文本,java,html,swing,file-io,jtextpane,Java,Html,Swing,File Io,Jtextpane,我正在尝试从我的JTextPane获取html格式的内容 问题是,当我使用指定的属性集插入文本时,内容文本在尝试将其写入文件时不会输出,但样式设置是无效的 我不确定这是否与我如何插入文本或如何尝试将其写入文件有关 以下是一个例子: import javax.swing.*; import javax.swing.text.*; import javax.swing.text.html.*; import java.awt.BorderLayout; import java.io.*; impor

我正在尝试从我的
JTextPane
获取html格式的内容

问题是,当我使用指定的
属性集插入文本时,内容文本在尝试将其写入文件时不会输出,但样式设置是无效的

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

以下是一个例子:

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>

这是我的纯文本

如您所见,这缺少文本
“这是我的蓝色粗体文本”
,但它将正确显示在框架中


我还尝试将
jtp.getText()
直接写入文件,并得到相同的结果。

在测试代码时,我注意到一些奇怪的事情。如果仔细查看JTextPane上的最后一个字符串(这是我的蓝色粗体文本),尽管它以粗体显示,但它的字体与前面的文本不完全相同

这是一个明确的信号,表明某些属性已经丢失。还请注意,插入该字符串(上面发布的字符串)后生成的HTML缺少一个

标记:一个新段落在字体标记之后打开。同样,有些东西在路上丢失了

那么,这是怎么回事?当您将属性传递给该方法时,这些属性将覆盖任何现有属性。您需要做的是,在插入之前,将这些属性与新的粗体和彩色属性合并。实际上,您必须稍微更改构造函数的代码:

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();
}

看到字体的区别了吗?至于这个。在代码中,它给出了将在任何后续插入中使用的当前属性集。这正是我们需要的

您可能想知道,如果尚未插入文本,可能存在哪些属性。简言之,文本中的每一个元素,即文本,都将由一个特殊的内部“标记”标识,该标记名为。此标记存储为属性。而正是这种属性的丧失,才造成了这场浩劫


希望这有帮助

它“可能”试图将文本添加到
边界之外,这可能会使编辑无效…另请参见此相关内容。问题在于插入之前操作属性的方式。看看下面我的答案。注意到不同的字体,我都没注意到。我觉得我做的不对,只是不知道它在哪里或什么地方。谢谢。谢谢你的接受。几个月前,我帮助开发了一个开源辅助编辑器,名为。自Java7以来,处理所有这些编辑内容的Swing类的文档有所改进,但仍然不够好。为了找到这种行为的解释,我花了很多时间绞尽脑汁。所以,你遇到这个问题是完全可以理解的。再次感谢!