2013-05-05 65 views
1

我想將一個全局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中的一個用於設置的東西像前景色的特殊性能 - 這就是爲什麼這可能無法正常工作 - 但任何人都可以點我一下,如何使用其它樣式屬性?如左縮進和第一行縮進。

回答

3
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); 
+0

這是不是顯示了我,Windows 7的jdk 1.6?此外,即使在向文檔添加更多文本之後,此樣式是否也適用? – Raekye 2013-05-05 21:39:06

+0

適用於各種版本的Windows和JDK。發佈顯示問題的SSCCE。 – camickr 2013-05-05 21:46:47

+0

因此,當我在添加文本後設置樣式時,它不起作用。儘管在我添加/插入任何文本之前我堅持使用它。事件,如果我做'doc.setParagraphAttributes(0,0,風格,假);'。知道爲什麼它工作,即使我將長度設置爲0? – Raekye 2013-05-05 22:05:27