Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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_Swing_Jtextpane_Styleddocument - Fatal编程技术网

Java 无法设置JTextPane的样式并同时显示HTML图像

Java 无法设置JTextPane的样式并同时显示HTML图像,java,swing,jtextpane,styleddocument,Java,Swing,Jtextpane,Styleddocument,我一直在使用JTextPane来显示基本的HTML内容,包括本地保存的图像。我一直在尝试使用文本窗格的输入属性设置JTextPane的样式。每当我修改窗格的样式时,我得到的HTML图像都会中断,并且在不注释样式代码的情况下不会出现 这是代码的SSCE: import java.awt.Color; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.s

我一直在使用JTextPane来显示基本的HTML内容,包括本地保存的图像。我一直在尝试使用文本窗格的输入属性设置JTextPane的样式。每当我修改窗格的样式时,我得到的HTML图像都会中断,并且在不注释样式代码的情况下不会出现

这是代码的SSCE:

import java.awt.Color;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class JTextPaneImageTest {

    public static void main(String[] args) {
        new JTextPaneImageTest();
    }

    public JTextPaneImageTest() {
        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setEditable(false);

        String content = "<html><body><p>Test text here</p>"
                + "<img src=\"file:\\\\\\C:\\Users\\racha_000\\AppData\\Local\\Temp\\ebookr\\test.jpg\" />"
                + "</body></html>";
        System.out.println(content);

        HTMLEditorKit editorKit = (HTMLEditorKit) textPane.getEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) textPane.getDocument();
        MutableAttributeSet mats = textPane.getInputAttributes();
        StyleConstants.setForeground(mats, Color.RED);

        try {
            editorKit.insertHTML(htmlDoc, htmlDoc.getLength(), content, 0, 0, null);
        } catch (BadLocationException | IOException e) {
            e.printStackTrace();
        } finally {
            htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), mats, true);
        }

        JFrame frame = new JFrame();
        frame.add(textPane);
        frame.setVisible(true);
    }
}
不带样式:

样式:


以这种方式对文档进行样式化是唯一对我有效的方法,因为我一次将内容全部插入为HTML,并以相同的方式对其进行样式化。其他方法不起作用,所以我希望能够保持这种面板样式设置方法。

调用带有最后一个参数(replace)的
setCharacterAttributes()
方法将
true
在某种程度上删除与图像相关的HTML代码

只要用
false
调用它,它就可以工作:

htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), mats, false);

setCharacterAttributes()方法在实际程序中调用了多次,还是只调用了一次?将其最后一个参数设置为false可以解决SSCE中的问题。@如果只调用了一次,请在实际程序中尝试,它可以正常工作。非常感谢,我甚至没有想到这个arg。好吧,我写一个正式的回答:)
htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), mats, false);