Java添加全局JTextPane样式/属性?
问题描述:
我想将一个全局AttributeSet添加到我的JTextPane中。Java添加全局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();
从http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm没有运气。
我知道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);
这是不是显示了我,Windows 7的jdk 1.6?此外,即使在向文档添加更多文本之后,此样式是否也适用? – Raekye 2013-05-05 21:39:06
适用于各种版本的Windows和JDK。发布显示问题的SSCCE。 – camickr 2013-05-05 21:46:47
因此,当我在添加文本后设置样式时,它不起作用。尽管在我添加/插入任何文本之前我坚持使用它。事件,如果我做'doc.setParagraphAttributes(0,0,风格,假);'。知道为什么它工作,即使我将长度设置为0? – Raekye 2013-05-05 22:05:27