2011-04-15 54 views
3

我需要修改JTextPane中的字母間距(字體跟蹤),並且我無法使其工作。如何修改JTextPane中的字母間距?

當我使用一個JTextArea,我可以這樣做:

Font font = new Font("Courier New", Font.PLAIN, 10); 
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>(); 
attrs.put(TextAttribute.TRACKING, -0.1); 
font = font.deriveFont(attrs); 
textArea.setFont(font); 

但我需要改變行距,我需要使用的JTextPane,而這樣做的:

textPane.setFont(font) 

正如我對JTextArea所做的那樣不起作用。我試過的另一件事是:

MutableAttributeSet set = new SimpleAttributeSet(); 
StyleConstants.setLineSpacing(set, -0.2); 
StyleConstants.setFontFamily(set,"Courier New"); 
StyleConstants.setFontSize(set, 10); 
set.addAttribute(TextAttribute.TRACKING, -0.1); 
ta.setParagraphAttributes(set, true); 

但是跟蹤屬性不起作用。

我在做什麼錯?

回答

5

你的意思是字距嗎?這一個顯示如何指定自定義字距和一些更多的文字效果 http://java-sl.com/gp_effects.html

+1

很酷的文章,書籤:-) – kleopatra 2011-04-15 11:45:00

+0

我試過了,工作,但它打破了line-spacing屬性。所以這不夠好... – Gustavo 2011-04-15 19:09:08