程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 代碼分享:Swing外觀 抗鋸齒 字體設置

代碼分享:Swing外觀 抗鋸齒 字體設置

編輯:關於JAVA

[圖片] 默認

[圖片] 使用Nimbus

[圖片] 使用Nimbus並開啟抗鋸齒

[圖片] 使用Nimbus,開啟抗鋸齒並使用自選字體

代碼片段:

  1. package canghailan.ui;
  2. import Javax.swing.*;
  3. import Javax.swing.plaf.FontUIResource;
  4. import Java.awt.*;
  5. import Java.util.HashMap;
  6. import Java.util.Map;
  7. /**
  8. * @author canghailan
  9. * @datetime 2011-12-19 11:13
  10. */
  11. public class UIs {
  12. private static final String FALLBACK_FONT_FAMILY_NAME = Font.SANS_SERIF;
  13. private static final Map<String, String> FONT_FAMILY_NAMES = new HashMap<>();
  14. private static final String[] BEST_FONT_FAMILIES = {
  15. "微軟雅黑", "arial", "sans-serif"
  16. };
  17. private static final int BEST_FONT_SIZE = 12; // 12px
  18. static {
  19. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  20. String[] fontFamilyNames = env.getAvailableFontFamilyNames();
  21. for (String fontFamilyName : fontFamilyNames) {
  22. FONT_FAMILY_NAMES.put(fontFamilyName.toLowerCase(), fontFamilyName);
  23. }
  24. if (!FONT_FAMILY_NAMES.containsKey("serif")) {
  25. FONT_FAMILY_NAMES.put("serif", Font.SERIF);
  26. }
  27. if (!FONT_FAMILY_NAMES.containsKey("sans-serif")) {
  28. FONT_FAMILY_NAMES.put("sans-serif", Font.SANS_SERIF);
  29. }
  30. }
  31. public static void enableAntiAliasing() {
  32. System.setProperty("awt.useSystemAAFontSettings", "on");
  33. System.setProperty("swing.aatext", "true");
  34. }
  35. public static String getLookAndFeel() {
  36. try {
  37. for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  38. if ("Nimbus".equals(info.getName())) {
  39. return info.getClassName();
  40. }
  41. }
  42. } catch (Exception ignore) {
  43. }
  44. return UIManager.getCrossPlatformLookAndFeelClassName();
  45. }
  46. public static String getFontFamily(String[] fontFamilIEs) {
  47. for (String fontFamily : fontFamilIEs) {
  48. fontFamily = fontFamily.toLowerCase();
  49. if (FONT_FAMILY_NAMES.containsKey(fontFamily)) {
  50. return FONT_FAMILY_NAMES.get(fontFamily);
  51. }
  52. }
  53. return FALLBACK_FONT_FAMILY_NAME;
  54. }
  55. public static String[] getBestFontFamilIEs() {
  56. return BEST_FONT_FAMILIES;
  57. }
  58. public static int getBestFontSize() {
  59. return BEST_FONT_SIZE;
  60. }
  61. /*########################################*/
  62. public static void setUI() {
  63. enableAntiAliasing();
  64. // set LookAndFeel
  65. try {
  66. UIManager.setLookAndFeel(getLookAndFeel());
  67. } catch (Exception ignore) {
  68. }
  69. // set DefaultFont
  70. String bestFontFamily = getFontFamily(getBestFontFamilIEs());
  71. for (Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()) {
  72. if (entry.getValue() instanceof FontUIResource) {
  73. FontUIResource fontUIRes = (FontUIResource) entry.getValue();
  74. entry.setValue(new FontUIResource(
  75. bestFontFamily,
  76. fontUIRes.getStyle(),
  77. getBestFontSize() > fontUIRes.getSize() ?
  78. getBestFontSize() : fontUIRes.getSize()
  79. ));
  80. }
  81. }
  82. }
  83. }

最新版本:UIs.Java

源碼下載:http://down.51cto.com/data/319345

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved