编程语言
首页 > 编程语言> > java-在JEditorPane中保留空白HTML行的格式

java-在JEditorPane中保留空白HTML行的格式

作者:互联网

我注意到,当HTML JEditorPane上有一个空行时,所有以前设置的样式都会消失.例如,请参见下面的代码示例:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;


public class BlankLineTester {
    private JEditorPane jep;

    public BlankLineTester() {
        String html = "<html><head></head><body>" +
                "<p><b>Line 1</b></p>" +
                "<p><b></b></p>" +
                "<p><b>Line 3</b></p>" +
                "</body></html>";

        jep = new JEditorPane();
        jep.setContentType("text/html");
        jep.setText(html);

        JFrame frame = new JFrame("Blank Line Test");
        frame.getContentPane().add(jep);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        System.out.println("Line 3 is " + isInputAttributeBold());

        jep.getCaret().setDot(8);
        System.out.println("Line 2 is " + isInputAttributeBold());

    }

    private boolean isInputAttributeBold() {
        AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
        return StyleConstants.isBold(attSet);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BlankLineTester();
            }
        });
    }
}

第2行最初设置为粗体的空行,但在解析后似乎没有保留bold属性.还要注意,如果您自己运行此代码,将光标置于第3行,然后删除该行中的所有内容,则您键入的下一个字符将不会加粗.我想当HTMLDocument树中的叶子元素消失时,它们所代表的文本消失了,但是当用户运行它时,这似乎开始出现错误.

是否有人有想法让样式属性在空行上解析并在删除样式行上的所有内容时保留?

谢谢!
-安迪

解决方法:

如果您不是这样写的,那么如果您这样写的话,我想您会得到表现,就像b& / b,标记是旧样式,尽管我使用的是JRE 1.7更新3,以下代码对此也适用:

String html = "<html><head></head><body>" +
                "<p style = \"font-weight:bold\">Line 1</p>" +
                "<p style = \"font-weight:bold\"></p>" +
                "<p style = \"font-weight:bold\">Line 3</p>" +
                "</body></html>";

在运行程序时尝试使用此代码,尝试通过将光标移到开始处按Delete键,然后将保留Color GREEN,因为这是鼠标指针触摸的最后一个字符的颜色,然后尝试通过按Backspace键将光标移到最底端,则它会保留“蓝色”,因为这是鼠标指针触摸的最后一个字符的颜色.第三次,只需尝试在已经提供的两行之间的每一行中写一个单词,就有一行将显示红色字符,请尝试查找该行.我附上下面的图片,以阐明红线情况.

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;


public class BlankLineTester {
    private JEditorPane jep;

    public BlankLineTester() {
        String html = "<html><head></head><body>" +
                "<p style = \"font-weight:bold; color: blue\">Line 1</p><br />" +
                "<p style = \"font-weight:bold; color: red\"></p><br />" +
                "<p style = \"font-weight:bold; color: green\">Line 3</p>" +
                "</body></html>";

        jep = new JEditorPane();
        jep.setContentType("text/html");
        jep.setText(html);

        JFrame frame = new JFrame("Blank Line Test");
        frame.getContentPane().add(jep);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        System.out.println("Line 3 is " + isInputAttributeBold());

        jep.getCaret().setDot(8);
        System.out.println("Line 2 is " + isInputAttributeBold());

    }

    private boolean isInputAttributeBold() {
        AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
        return StyleConstants.isBold(attSet);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BlankLineTester();
            }
        });
    }
}

标签:jeditorpane,swing,html,java
来源: https://codeday.me/bug/20191201/2081159.html