Java 修改以前设置的JEditorPane的CSS样式
如何修改JEditorPane的CSS样式。我设置了这样的风格(而且很有效): 现在,我想修改这个样式(在本例中设置不同的字体)。我试过了,但没用。样式保持不变Java 修改以前设置的JEditorPane的CSS样式,java,css,swing,Java,Css,Swing,如何修改JEditorPane的CSS样式。我设置了这样的风格(而且很有效): 现在,我想修改这个样式(在本例中设置不同的字体)。我试过了,但没用。样式保持不变 Style style = hTMLEditorKit.getStyleSheet().getRule("body"); style.addAttribute("font-family", "Helvetica"); style.addAttribute("font-size", 14); 如何修改样式?您可以使用StyleConst
Style style = hTMLEditorKit.getStyleSheet().getRule("body");
style.addAttribute("font-family", "Helvetica");
style.addAttribute("font-size", 14);
如何修改样式?您可以使用
StyleConstants
修改样式
,这样做的另一个好处是提供了可以修改的属性,减少了对字符串文字的依赖性,例如字体大小
例如,您可以修改:
style.addAttribute("font-family", "Helvetica");
style.addAttribute("font-size", 14);
致:
如果使用我在下面介绍的printStyleAttributes
方法,您将看到这些更改现在反映在样式中。但是,这不会自动将更改应用于编辑器窗格
为了反映样式更改,您需要在文档上使用,提供样式更改的应用位置以及是否应覆盖/替换找到的任何现有样式
例如: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);
}
}
}
哇!这对我帮助很大。谢谢setFontSize(样式,14);虽然没有将字体大小设置为14,但似乎出于某种原因将其设置为5或6。很高兴听到:)您提供的值仍在后台,您可以使用System.out.println(StyleConstants.getFontSize(style))确认这一点
较低的值来自与支持的最小字体大小8的偏差,即输出中的1
StyleConstants.setFontSize(style, 14);
StyleConstants.setFontFamily(style, "Helvetica");
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);
}
}
}