讓Java Swing顯示中文更美觀

來源:互聯網
上載者:User
首先感謝Could的回複和提醒

關鍵字:Swing JGoodies Look&Feel 中文 漢字

在前一篇文章中提到通過使用第三方的L&F來使Java程式的介面更美觀,但是JGoodies L&F的中文顯示有問題,漢字都變成了方框。 而且Swing Metal 預設的漢字顯示為12 粗體,非常難看。有沒有一種方法讓中文可以正常顯示,而且很美觀呢? 

找了些資料,發現是由於Swing中預設都是使用了tohoma字型,而不是Dialog 這樣的family font。 要使中文顯示正常,把字型設定成Dialog即可。 Could的方法要對每個組件設定字型比較麻煩,下面使用全域字型設定來解決這個問題。

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);
        }
    }

...........

try {
         
      UIManager.setLookAndFeel(      
          //UIManager.getCrossPlatformLookAndFeelClassName()
          //UIManager.getSystemLookAndFeelClassName()
          //new com.sun.java.swing.plaf.motif.MotifLookAndFeel()
            //"com.jgoodies.looks.windows.WindowsLookAndFeel"
              "com.jgoodies.looks.plastic.PlasticLookAndFeel"
             // "com.jgoodies.looks.plastic.Plastic3DLookAndFeel"
            //"com.jgoodies.looks.plastic.PlasticXPLookAndFeel"          

          );     
     
      initGlobalFontSetting(new Font("Dialog",Font.PLAIN,12));      
     
    }

這樣就完成了,來看看效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.