Java添加全局JTextPane样式/属性



我想添加一个全局AttributeSet到我的JTextPane。

我发现了这个:

SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
从http://java-sl.com/tip_hanging_first_line.html

我想知道如何设置"默认样式表"?(不使用HTML)。然后我试着这样做:

StyleContext style = new StyleContext();
Style s = style.addStyle("test", null);
StyleConstants.setForeground(s, Color.BLUE);
StyledDocument d = (StyledDocument) console.getOutputField().getDocument();

From http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm without luck.

我知道StyledDocument有特定的属性来设置像前景颜色这样的东西-这就是为什么这可能不起作用-但是有人能告诉我如何使用其他样式属性吗?例如左缩进和第一行缩进

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
StyleConstants.setForeground(style, Color.BLUE);
doc.setParagraphAttributes(0, doc.getLength(), style, true);

最新更新