如何修改 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);
如何修改样式?
最佳答案
您可以使用 StyleConstants
修改 Style
,它的另一个好处是提供您可以修改的属性,减少对字符串文字的依赖,例如 "font -大小”
例如,您可以修改:
style.addAttribute("font-family", "Helvetica");
style.addAttribute("font-size", 14);
至:
StyleConstants.setFontSize(style, 14);
StyleConstants.setFontFamily(style, "Helvetica");
如果您使用下面包含的 printStyleAttributes
方法,您将看到所做的更改现在已反射(reflect)在 Style
中。但是,这不会自动将更改应用到编辑器 Pane
为了反射(reflect)样式更改,您需要使用 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);
}
}
}
关于java - 修改之前设置的JEditorPane的CSS样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851584/