如何修改JTextPane中的字母间距?

时间:2011-04-15 06:49:44

标签: java swing fonts jtextpane letter-spacing

我需要修改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);

但是跟踪属性不起作用。

我做错了什么?

1 个答案:

答案 0 :(得分:5)

你是说kerning吗?这个展示了如何指定自定义字距和一些更多的文本效果 http://java-sl.com/gp_effects.html