本文介绍了与JTextPane关联的StyledDocument的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与JTextPane关联的StyledDocument使用什么字体?默认情况下,它使用与JTextPane相同的字体吗?特别是,我想知道字体大小.
What font does the StyledDocument associated with a JTextPane use? By default, does it use the same font as the JTextPane? In particular, I'm wondering about the font size.
推荐答案
StyledDocument只是接口.界面没有任何字体.
StyledDocument is just interface. Interface doesn't have any font.
如果您查看DefaultStyledDocument类(实现接口).
If you take a look at the DefaultStyledDocument class (implementing the interface).
public Font getFont(AttributeSet attr) {
StyleContext styles = (StyleContext) getAttributeContext();
return styles.getFont(attr);
}
然后在StyleContext的源代码中
Then in the StyleContext's sources
public Font getFont(AttributeSet attr) {
// PENDING(prinz) add cache behavior
int style = Font.PLAIN;
if (StyleConstants.isBold(attr)) {
style |= Font.BOLD;
}
if (StyleConstants.isItalic(attr)) {
style |= Font.ITALIC;
}
String family = StyleConstants.getFontFamily(attr);
int size = StyleConstants.getFontSize(attr);
/**
* if either superscript or subscript is
* is set, we need to reduce the font size
* by 2.
*/
if (StyleConstants.isSuperscript(attr) ||
StyleConstants.isSubscript(attr)) {
size -= 2;
}
return getFont(family, style, size);
}
然后进入StyleConstants.
Then in the StyleConstants.
public static int getFontSize(AttributeSet a) {
Integer size = (Integer) a.getAttribute(FontSize);
if (size != null) {
return size.intValue();
}
return 12;
}
这篇关于与JTextPane关联的StyledDocument的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!