修改以前设置的JEditorPane CSS样式

时间:2016-06-16 06:37:55

标签: java css swing

如何修改JEditorPane的CSS样式。我设置了这样的风格(并且有效):

StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-family:"Arial"; font-size:12; } ");

HTMLEditorKit hTMLEditorKit = new HTMLEditorKit();
hTMLEditorKit.setStyleSheet(styleSheet);

JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setEditorKit(messageEditorPaneHTMLEditorKit);

现在,我想修改该样式(在这种情况下设置不同的字体)。我试过这个,但它什么都没做。样式保持不变。

Style style = hTMLEditorKit.getStyleSheet().getRule("body");
style.addAttribute("font-family", "Helvetica");
style.addAttribute("font-size", 14);

如何修改样式?

1 个答案:

答案 0 :(得分:2)

您可以使用Style修改StyleConstants,这样可以提供您可以修改的属性,从而减少对字符串文字的依赖,例如"font-size"

例如,您可以修改:

style.addAttribute("font-family", "Helvetica");
style.addAttribute("font-size", 14);

为:

StyleConstants.setFontSize(style, 14);
StyleConstants.setFontFamily(style, "Helvetica");

如果您使用我在下面列出的printStyleAttributes方法,您会看到更改现已反映在Style中。但是,这不会自动将更改应用到编辑器窗格

为了体现样式更改,您需要在文档上使用setCharacterAttributes(),提供应该应用的位置以及是否应覆盖/替换找到的任何现有样式

例如:document.setCharacterAttributes(0, document.getLength(), style, true);

将更新整个文档,用更改替换样式

SSCCE:

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import java.awt.*;
import java.util.Enumeration;

public class EditorStylingExample {
    public static void main(String[] args) {
        StyleSheet styleSheet = new StyleSheet();
        styleSheet.addRule("body {font-family:\"Arial\"; font-size:12; } ");

        HTMLEditorKit messageEditorPaneHTMLEditorKit = new HTMLEditorKit();
        messageEditorPaneHTMLEditorKit.setStyleSheet(styleSheet);
        HTMLDocument document = (HTMLDocument) messageEditorPaneHTMLEditorKit.createDefaultDocument();

        JEditorPane editorPane = new JEditorPane("text/html", "");
        editorPane.setEditorKit(messageEditorPaneHTMLEditorKit);
        editorPane.setDocument(document);

        JButton changeStyleButton = new JButton("Change style");
        changeStyleButton.addActionListener(e -> {
            Style style = styleSheet.getStyle("body");
            StyleConstants.setBold(style, true);
            StyleConstants.setFontSize(style, 14);
            StyleConstants.setFontFamily(style, "Helvetica");
            printStyleAttributes(style);
            document.setCharacterAttributes(0, document.getLength(), style, true);
        });

        JFrame frame = new JFrame("Styling example");
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        contentPane.add(editorPane);
        contentPane.add(changeStyleButton);

        editorPane.setAlignmentX(Component.CENTER_ALIGNMENT);
        changeStyleButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        frame.setContentPane(contentPane);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static void printStyleAttributes(Style style) {
        Enumeration styleAttributes = style.getAttributeNames();
        while (styleAttributes.hasMoreElements()) {
            Object attribute = styleAttributes.nextElement();
            String attributeName = attribute.toString();
            Object attributeValue = style.getAttribute(attribute);
            System.out.println(attributeName + ": " + attributeValue);
        }
    }
}