下面列出了javax.swing.UIDefaults#keySet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* setting global font.
*
* @param f Font object
*/
public void setApplicationFont(Font f)
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
synchronized (defaults)
{
for (Object ui_property : defaults.keySet())
{
if (ui_property.toString().endsWith(".font"))
{
UIManager.put(ui_property, f);
}
}
}
}
/**
* 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));
}
}
}
private void initFonts( UIDefaults defaults ) {
FontUIResource uiFont = null;
if( SystemInfo.IS_WINDOWS ) {
Font winFont = (Font) Toolkit.getDefaultToolkit().getDesktopProperty( "win.messagebox.font" );
if( winFont != null )
uiFont = createCompositeFont( winFont.getFamily(), winFont.getStyle(), winFont.getSize() );
} else if( SystemInfo.IS_MAC ) {
String fontName;
if( SystemInfo.IS_MAC_OS_10_15_CATALINA_OR_LATER ) {
// use Helvetica Neue font
fontName = "Helvetica Neue";
} else if( SystemInfo.IS_MAC_OS_10_11_EL_CAPITAN_OR_LATER ) {
// use San Francisco Text font
fontName = ".SF NS Text";
} else {
// default font on older systems (see com.apple.laf.AquaFonts)
fontName = "Lucida Grande";
}
uiFont = createCompositeFont( fontName, Font.PLAIN, 13 );
} else if( SystemInfo.IS_LINUX ) {
Font font = LinuxFontPolicy.getFont();
uiFont = (font instanceof FontUIResource) ? (FontUIResource) font : new FontUIResource( font );
}
if( uiFont == null )
uiFont = createCompositeFont( Font.SANS_SERIF, Font.PLAIN, 12 );
uiFont = UIScale.applyCustomScaleFactor( uiFont );
// use active value for all fonts to allow changing fonts in all components
// (similar as in Nimbus L&F) with:
// UIManager.put( "defaultFont", myFont );
Object activeFont = new ActiveFont( 1 );
// override fonts
for( Object key : defaults.keySet() ) {
if( key instanceof String && (((String)key).endsWith( ".font" ) || ((String)key).endsWith( "Font" )) )
defaults.put( key, activeFont );
}
// use smaller font for progress bar
defaults.put( "ProgressBar.font", new ActiveFont( 0.85f ) );
// set default font
defaults.put( "defaultFont", uiFont );
}