下面列出了怎么用javax.swing.plaf.FontUIResource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object createValue( UIDefaults table ) {
Font defaultFont = UIManager.getFont( "defaultFont" );
if( lastDefaultFont != defaultFont ) {
lastDefaultFont = defaultFont;
if( scaleFactor != 1 ) {
// scale font
int newFontSize = Math.round( defaultFont.getSize() * scaleFactor );
font = new FontUIResource( defaultFont.deriveFont( (float) newFontSize ) );
} else {
// make sure that font is a UIResource for LaF switching
font = (defaultFont instanceof UIResource)
? defaultFont
: new FontUIResource( defaultFont );
}
}
return font;
}
@Override
protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
if (font2D == null) { // Not expected, just a precaution.
return new FontUIResource(family, style, size);
}
/* The name of the font will be that of the physical font in slot,
* but by setting the handle to that of the CompositeFont it
* renders as that CompositeFont.
* It also needs to be marked as a created font which is the
* current mechanism to signal that deriveFont etc must copy
* the handle from the original font.
*/
FontUIResource fuir =
new FontUIResource(font2D.getFamilyName(null), style, size);
FontAccess.getFontAccess().setFont2D(fuir, font2D.handle);
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
/**
* Returns Neon-specific font resource.
*
* @param systemFont The default system font.
* @param toBoldify If <code>true</code>, the original font (the first parameter) is
* boldified.
* @param extraFontSize Extra font size in pixels.
* @return Neon-specific font resource.
*/
private FontUIResource getNeonFont(FontUIResource systemFont, boolean toBoldify,
int extraFontSize) {
boolean isOrigItalic = systemFont.isItalic();
int newStyle = systemFont.getStyle();
if (toBoldify) {
if (isOrigItalic) {
newStyle = Font.ITALIC + Font.BOLD;
} else {
newStyle = Font.BOLD;
}
}
Font derived = systemFont.deriveFont((float) (systemFont.getSize() + extraFontSize))
.deriveFont(newStyle);
if (derived instanceof FontUIResource) {
return (FontUIResource) derived;
}
return new FontUIResource(derived);
}
@Override
protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
if (font2D == null) { // Not expected, just a precaution.
return new FontUIResource(family, style, size);
}
/* The name of the font will be that of the physical font in slot,
* but by setting the handle to that of the CompositeFont it
* renders as that CompositeFont.
* It also needs to be marked as a created font which is the
* current mechanism to signal that deriveFont etc must copy
* the handle from the original font.
*/
FontUIResource fuir =
new FontUIResource(font2D.getFamilyName(null), style, size);
FontAccess.getFontAccess().setFont2D(fuir, font2D.handle);
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
@Override
protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
if (font2D == null) { // Not expected, just a precaution.
return new FontUIResource(family, style, size);
}
/* The name of the font will be that of the physical font in slot,
* but by setting the handle to that of the CompositeFont it
* renders as that CompositeFont.
* It also needs to be marked as a created font which is the
* current mechanism to signal that deriveFont etc must copy
* the handle from the original font.
*/
FontUIResource fuir =
new FontUIResource(font2D.getFamilyName(null), style, size);
FontAccess.getFontAccess().setFont2D(fuir, font2D.handle);
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
/**
* This method initializes the UI resources like fonts, size, etc.
*/
private void setupUI() {
// Special handling to ensure that Japanese fonts are readable
if (Locale.getDefault().getLanguage().equals(Locale.JAPANESE.getLanguage())) {
Object key = null;
Object value = null;
Enumeration<?> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
key = keys.nextElement();
value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, ((FontUIResource) value).deriveFont(12.0f));
}
}
getMainContentPane().setFont(new Font("Japanese", Font.PLAIN, 12));
}
}
@Override
protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
if (font2D == null) { // Not expected, just a precaution.
return new FontUIResource(family, style, size);
}
/* The name of the font will be that of the physical font in slot,
* but by setting the handle to that of the CompositeFont it
* renders as that CompositeFont.
* It also needs to be marked as a created font which is the
* current mechanism to signal that deriveFont etc must copy
* the handle from the original font.
*/
FontUIResource fuir =
new FontUIResource(font2D.getFamilyName(null), style, size);
FontAccess.getFontAccess().setFont2D(fuir, font2D.handle);
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
/**
* Sets default Swing font.
* IMPORTANT! Needs to be called before main frame creation
*
* @param font the new font to use
*/
public static void setFont(@Nonnull final Font font)
{
final FontUIResource f = new FontUIResource(font);
final Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements())
{
final Object key = keys.nextElement();
final Object value = UIManager.get(key);
if (value instanceof FontUIResource)
{
UIManager.put(key, f);
}
}
}
/**
* Returns the style to use if there are no matching styles.
*/
private SynthStyle getDefaultStyle() {
if (_defaultStyle == null) {
_defaultStyle = new DefaultSynthStyle();
((DefaultSynthStyle)_defaultStyle).setFont(
new FontUIResource(Font.DIALOG, Font.PLAIN,12));
}
return _defaultStyle;
}
public Font getFont() {
Font font = super.getFont();
// Prefer the parent containers font if our font is a
// FontUIResource
if(font instanceof FontUIResource) {
Container parent = getParent();
if(parent != null && parent.getFont() != null)
font = parent.getFont();
}
return font;
}
@Override
protected FontUIResource getFontConfigFUIR(
String family, int style, int size)
{
String mappedName = FontUtilities.mapFcName(family);
if (mappedName == null) {
mappedName = "sansserif";
}
return new FontUIResource(mappedName, style, size);
}
protected FontUIResource getObject() {
return new FontUIResource(
new Font(
Collections.singletonMap(
TextAttribute.STRIKETHROUGH,
TextAttribute.STRIKETHROUGH_ON)));
}
public static void setFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys();
keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}
public Font getFont() {
Font font = super.getFont();
// Prefer the parent containers font if our font is a
// FontUIResource
if(font instanceof FontUIResource) {
Container parent = getParent();
if(parent != null && parent.getFont() != null)
font = parent.getFont();
}
return font;
}
/**
* Returns the style to use if there are no matching styles.
*/
private SynthStyle getDefaultStyle() {
if (_defaultStyle == null) {
_defaultStyle = new DefaultSynthStyle();
((DefaultSynthStyle)_defaultStyle).setFont(
new FontUIResource(Font.DIALOG, Font.PLAIN,12));
}
return _defaultStyle;
}
protected FontUIResource getObject() {
return new FontUIResource(
new Font(
Collections.singletonMap(
TextAttribute.STRIKETHROUGH,
TextAttribute.STRIKETHROUGH_ON)));
}
/**
* Returns the style to use if there are no matching styles.
*/
private SynthStyle getDefaultStyle() {
if (_defaultStyle == null) {
_defaultStyle = new DefaultSynthStyle();
((DefaultSynthStyle)_defaultStyle).setFont(
new FontUIResource(Font.DIALOG, Font.PLAIN,12));
}
return _defaultStyle;
}
public static void installMacOSXFonts(UIDefaults defaults) {
final String face = "Helvetica Neue";
final FontUIResource uiFont = getFont(face, 13, Font.PLAIN);
initFontDefaults(defaults, uiFont);
for (Object key : new HashSet<>(defaults.keySet())) {
if (!(key instanceof String)) continue;
if (!StringUtil.endsWithIgnoreCase(((String)key), "font")) continue;
Object value = defaults.get(key);
if (value instanceof FontUIResource) {
FontUIResource font = (FontUIResource)value;
if (font.getFamily().equals("Lucida Grande") || font.getFamily().equals("Serif")) {
if (!key.toString().contains("Menu")) {
defaults.put(key, getFont(face, font.getSize(), font.getStyle()));
}
}
}
}
FontUIResource uiFont11 = getFont(face, 11, Font.PLAIN);
defaults.put("TableHeader.font", uiFont11);
FontUIResource buttonFont = getFont("Helvetica Neue", 13, Font.PLAIN);
defaults.put("Button.font", buttonFont);
Font menuFont = getFont("Lucida Grande", 14, Font.PLAIN);
defaults.put("Menu.font", menuFont);
defaults.put("MenuItem.font", menuFont);
defaults.put("MenuItem.acceleratorFont", menuFont);
defaults.put("PasswordField.font", defaults.getFont("TextField.font"));
}
public Font getFont() {
Font font = super.getFont();
// Prefer the parent containers font if our font is a
// FontUIResource
if(font instanceof FontUIResource) {
Container parent = getParent();
if(parent != null && parent.getFont() != null)
font = parent.getFont();
}
return font;
}
@Override
protected FontUIResource getFontConfigFUIR(
String family, int style, int size)
{
String mappedName = FontUtilities.mapFcName(family);
if (mappedName == null) {
mappedName = "sansserif";
}
return new FontUIResource(mappedName, style, size);
}
public static void initGlobalFontSetting(Font fnt) {
FontUIResource fontRes = new FontUIResource(fnt);
for (Enumeration<?> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}
@Nonnull
@Override
public UIDefaults getDefaultsImpl(UIDefaults superDefaults) {
try {
final UIDefaults metalDefaults = new MetalLookAndFeel().getDefaults();
final UIDefaults defaults = base.getDefaults();
if (SystemInfo.isLinux && !Registry.is("darcula.use.native.fonts.on.linux")) {
Font font = findFont("DejaVu Sans");
if (font != null) {
for (Object key : defaults.keySet()) {
if (key instanceof String && ((String)key).endsWith(".font")) {
defaults.put(key, new FontUIResource(font.deriveFont(13f)));
}
}
}
}
LafManagerImplUtil.initInputMapDefaults(defaults);
defaults.put(SupportTextBoxWithExpandActionExtender.class, SupportTextBoxWithExpandActionExtender.INSTANCE);
defaults.put(SupportTextBoxWithExtensionsExtender.class, SupportTextBoxWithExtensionsExtender.INSTANCE);
initIdeaDefaults(defaults);
patchStyledEditorKit(defaults);
patchComboBox(metalDefaults, defaults);
defaults.remove("Spinner.arrowButtonBorder");
defaults.put("Spinner.arrowButtonSize", JBUI.size(16, 5).asUIResource());
MetalLookAndFeel.setCurrentTheme(createMetalTheme());
defaults.put("EditorPane.font", defaults.getFont("TextField.font"));
return defaults;
}
catch (Exception e) {
log(e);
}
return super.getDefaultsImpl(superDefaults);
}
/**
* Returns the style to use if there are no matching styles.
*/
private SynthStyle getDefaultStyle() {
if (_defaultStyle == null) {
_defaultStyle = new DefaultSynthStyle();
((DefaultSynthStyle)_defaultStyle).setFont(
new FontUIResource(Font.DIALOG, Font.PLAIN,12));
}
return _defaultStyle;
}
/**
* Set the default font size used in components.
*
* @param size new font size
*/
public void setDefaultFontSize(final int size) {
UIDefaults defaults = getDefaults();
for (Object key : defaults.keySet()) {
if ((key instanceof String) && (((String) key).endsWith(".font"))) {
FontUIResource font = (FontUIResource) defaults.get(key);
// For some reason changing it in defaults does not work
// reliably. Going through UIManager fixes it.
UIManager.put(key, new FontUIResource(font.getName(), font.getStyle(), size));
}
}
}
@Override
protected FontUIResource getFontConfigFUIR(
String family, int style, int size)
{
String mappedName = FontUtilities.mapFcName(family);
if (mappedName == null) {
mappedName = "sansserif";
}
return new FontUIResource(mappedName, style, size);
}
protected FontUIResource getObject() {
return new FontUIResource(
new Font(
Collections.singletonMap(
TextAttribute.STRIKETHROUGH,
TextAttribute.STRIKETHROUGH_ON)));
}
/**
* Returns the style to use if there are no matching styles.
*/
private SynthStyle getDefaultStyle() {
if (_defaultStyle == null) {
_defaultStyle = new DefaultSynthStyle();
((DefaultSynthStyle)_defaultStyle).setFont(
new FontUIResource(Font.DIALOG, Font.PLAIN,12));
}
return _defaultStyle;
}
@Nonnull
@Override
public UIDefaults getDefaultsImpl(UIDefaults superDefaults) {
try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod("getDefaults");
superMethod.setAccessible(true);
final UIDefaults metalDefaults = (UIDefaults)superMethod.invoke(new MetalLookAndFeel());
final UIDefaults defaults = (UIDefaults)superMethod.invoke(base);
if (SystemInfo.isLinux && !Registry.is("darcula.use.native.fonts.on.linux")) {
Font font = findFont("DejaVu Sans");
if (font != null) {
for (Object key : defaults.keySet()) {
if (key instanceof String && ((String)key).endsWith(".font")) {
defaults.put(key, new FontUIResource(font.deriveFont(13f)));
}
}
}
}
LafManagerImplUtil.initInputMapDefaults(defaults);
defaults.put(SupportTextBoxWithExpandActionExtender.class, SupportTextBoxWithExpandActionExtender.INSTANCE);
defaults.put(SupportTextBoxWithExtensionsExtender.class, SupportTextBoxWithExtensionsExtender.INSTANCE);
initIdeaDefaults(defaults);
patchStyledEditorKit(defaults);
patchComboBox(metalDefaults, defaults);
defaults.remove("Spinner.arrowButtonBorder");
defaults.put("Spinner.arrowButtonSize", new Dimension(16, 5));
MetalLookAndFeel.setCurrentTheme(createMetalTheme());
defaults.put("EditorPane.font", defaults.getFont("TextField.font"));
return defaults;
}
catch (Exception e) {
log(e);
}
return super.getDefaultsImpl(superDefaults);
}
protected FontUIResource getObject() {
return new FontUIResource(
new Font(
Collections.singletonMap(
TextAttribute.STRIKETHROUGH,
TextAttribute.STRIKETHROUGH_ON)));
}
/**
* Subclassed to map <code>FontUIResource</code>s to null. If
* <code>font</code> is null, or a <code>FontUIResource</code>, this
* has the effect of letting the font of the JTree show
* through. On the other hand, if <code>font</code> is non-null, and not
* a <code>FontUIResource</code>, the font becomes <code>font</code>.
*/
public void setFont(Font font) {
if (font instanceof FontUIResource) {
font = null;
}
super.setFont(font);
}