[圖片] 默認
[圖片] 使用Nimbus
[圖片] 使用Nimbus並開啟抗鋸齒
[圖片] 使用Nimbus,開啟抗鋸齒並使用自選字體
代碼片段:
- package canghailan.ui;
- import Javax.swing.*;
- import Javax.swing.plaf.FontUIResource;
- import Java.awt.*;
- import Java.util.HashMap;
- import Java.util.Map;
- /**
- * @author canghailan
- * @datetime 2011-12-19 11:13
- */
- public class UIs {
- private static final String FALLBACK_FONT_FAMILY_NAME = Font.SANS_SERIF;
- private static final Map<String, String> FONT_FAMILY_NAMES = new HashMap<>();
- private static final String[] BEST_FONT_FAMILIES = {
- "微軟雅黑", "arial", "sans-serif"
- };
- private static final int BEST_FONT_SIZE = 12; // 12px
- static {
- GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
- String[] fontFamilyNames = env.getAvailableFontFamilyNames();
- for (String fontFamilyName : fontFamilyNames) {
- FONT_FAMILY_NAMES.put(fontFamilyName.toLowerCase(), fontFamilyName);
- }
- if (!FONT_FAMILY_NAMES.containsKey("serif")) {
- FONT_FAMILY_NAMES.put("serif", Font.SERIF);
- }
- if (!FONT_FAMILY_NAMES.containsKey("sans-serif")) {
- FONT_FAMILY_NAMES.put("sans-serif", Font.SANS_SERIF);
- }
- }
- public static void enableAntiAliasing() {
- System.setProperty("awt.useSystemAAFontSettings", "on");
- System.setProperty("swing.aatext", "true");
- }
- public static String getLookAndFeel() {
- try {
- for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- return info.getClassName();
- }
- }
- } catch (Exception ignore) {
- }
- return UIManager.getCrossPlatformLookAndFeelClassName();
- }
- public static String getFontFamily(String[] fontFamilIEs) {
- for (String fontFamily : fontFamilIEs) {
- fontFamily = fontFamily.toLowerCase();
- if (FONT_FAMILY_NAMES.containsKey(fontFamily)) {
- return FONT_FAMILY_NAMES.get(fontFamily);
- }
- }
- return FALLBACK_FONT_FAMILY_NAME;
- }
- public static String[] getBestFontFamilIEs() {
- return BEST_FONT_FAMILIES;
- }
- public static int getBestFontSize() {
- return BEST_FONT_SIZE;
- }
- /*########################################*/
- public static void setUI() {
- enableAntiAliasing();
- // set LookAndFeel
- try {
- UIManager.setLookAndFeel(getLookAndFeel());
- } catch (Exception ignore) {
- }
- // set DefaultFont
- String bestFontFamily = getFontFamily(getBestFontFamilIEs());
- for (Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()) {
- if (entry.getValue() instanceof FontUIResource) {
- FontUIResource fontUIRes = (FontUIResource) entry.getValue();
- entry.setValue(new FontUIResource(
- bestFontFamily,
- fontUIRes.getStyle(),
- getBestFontSize() > fontUIRes.getSize() ?
- getBestFontSize() : fontUIRes.getSize()
- ));
- }
- }
- }
- }
最新版本:UIs.Java
源碼下載:http://down.51cto.com/data/319345