Java 与JTextPane关联的StyledDocument的字体
与JTextPane关联的StyledDocument使用什么字体?默认情况下,它是否使用与JTextPane相同的字体?特别是,我想知道字体大小。相关的键是Java 与JTextPane关联的StyledDocument的字体,java,swing,jtextpane,styleddocument,Java,Swing,Jtextpane,Styleddocument,与JTextPane关联的StyledDocument使用什么字体?默认情况下,它是否使用与JTextPane相同的字体?特别是,我想知道字体大小。相关的键是TextPane.fontUIManager.get()可用于确定所选L&F的值。例如,在Mac OS X上,此代码生成以下控制台输出: System.out.println(UIManager.get("TextPane.font")); 控制台: com.apple.laf.AquaFonts$DerivedUIResourceFon
TextPane.font
UIManager.get()
可用于确定所选L&F的值。例如,在Mac OS X上,此代码生成以下控制台输出:
System.out.println(UIManager.get("TextPane.font"));
控制台:
com.apple.laf.AquaFonts$DerivedUIResourceFont[
family=Lucida Grande,name=Lucida Grande,style=plain,size=13]
附录:下面是遍历窗格样式的代码:
JTextPane jtp = new JTextPane();
...
HTMLDocument doc = (HTMLDocument) jtp.getDocument();
StyleSheet styles = doc.getStyleSheet();
Enumeration rules = styles.getStyleNames();
while (rules.hasMoreElements()) {
String name = (String) rules.nextElement();
Style rule = styles.getStyle(name);
System.out.println(rule.toString());
}
StyledDocument只是一个接口。界面没有任何字体 如果您查看DefaultStyledDocument类(实现接口) 然后在StyleContext的源代码中
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中
public static int getFontSize(AttributeSet a) {
Integer size = (Integer) a.getAttribute(FontSize);
if (size != null) {
return size.intValue();
}
return 12;
}
从经验上讲,这种违约一直持续到它被改变,但我从来没有追查过它。
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);
}
public static int getFontSize(AttributeSet a) {
Integer size = (Integer) a.getAttribute(FontSize);
if (size != null) {
return size.intValue();
}
return 12;
}