Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与JTextPane关联的StyledDocument的字体_Java_Swing_Jtextpane_Styleddocument - Fatal编程技术网

Java 与JTextPane关联的StyledDocument的字体

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

与JTextPane关联的StyledDocument使用什么字体?默认情况下,它是否使用与JTextPane相同的字体?特别是,我想知道字体大小。

相关的键是
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;
}