代碼分享:Swing外觀 抗鋸齒 字體設(shè)置
作者:canghailan
這個(gè)是一個(gè)界面顯示效果的處理,Swing中文顯示一直都是不怎么美觀,經(jīng)過修改,比默認(rèn)的好看多了。下文提供代碼。
[圖片] 默認(rèn) |
[圖片] 使用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
原文鏈接:http://www.oschina.net/code/snippet_116768_7750
【編輯推薦】
- Jease 2.6發(fā)布 Java開源內(nèi)容框架
- 一個(gè)Java程序員對2011年的回顧
- 用Java GUI編寫的畫板程序
- Java的動(dòng)態(tài)綁定機(jī)制
- Java中帶復(fù)選框的樹的實(shí)現(xiàn)和應(yīng)用
責(zé)任編輯:林師授
來源:
開源中國社區(qū)