JTextPane に関連付けられた StyledDocument が使用するフォントは? デフォルトでは、JTextPane と同じフォントを使用しますか? 特にフォントサイズが気になります。
2392 次
2 に答える
3
StyledDocument は単なるインターフェースです。インターフェイスにフォントがありません。
DefaultStyledDocument クラス (インターフェースの実装) を見ると、
public Font getFont(AttributeSet attr) {
StyleContext styles = (StyleContext) getAttributeContext();
return styles.getFont(attr);
}
次に、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;
}
于 2011-08-05T05:24:50.787 に答える
2
関連するUIManager
キーはTextPane.font
. UIManager.get()
選択した L&F の値を決定するために使用できます。たとえば、Mac OS X では、このコードは次のコンソール出力を生成します。
System.out.println(UIManager.get("TextPane.font"));
コンソール:
com.apple.laf.AquaFonts$DerivedUIResourceFont[ ファミリー=ルシダ グランデ、名前=ルシダ グランデ、スタイル=無地、サイズ=13]
補遺: この例に示すように、デフォルトはStyleContext.NamedStyle
UI のデフォルトと一致する です。
NamedStyle:デフォルト { 名前=デフォルト、フォントスタイル=、 FONT_ATTRIBUTE_KEY=com.apple.laf.AquaFonts$DerivedUIResourceFont[ ファミリー=ルシダ グランデ、名前=ルシダ グランデ、スタイル=無地、サイズ=13]、 font-weight=normal, font-family=ルシダ・グランデ, フォントサイズ=4, }
補遺: ペインのスタイルを反復処理するコードは次のとおりです。
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());
}
于 2011-08-05T00:11:06.163 に答える