avatar

大兜

右手寫程式,左手寫音樂

讓Java Swing GUI在Un*x中顯示中文

 作者  tonytonyjan (南洋大兜蟲)                                    看板  java   

 標題  [J2SE] 在Un*x中的中文亂碼解決方法                                        

 時間  Wed Mar 31 22:55:05 2010                                                 

───────────────────────────────────────  

                                                                                

換過作業系統後還沒有想到在Swing上顯示中文會有問題                               

只是因為幫別人寫一個小程式,今天就好死不死被我遇見了XD                          

尋求解決同時搜了一下版,不過似乎版上很少人討論到這東西…                        

於是研究過後把我的經驗貼出來分享給大家@@                                      

                                                                                

環境:                                                                          

$ uname -ai                                                                     

FreeBSD CS329.dorm8.nctu.edu.tw 8.0-RELEASE-p2 FreeBSD 8.0-RELEASE-p2 #6: Fri   

Feb 12 01:37:32 CST 2010     root@:/usr/obj/usr/src/sys/TONYBSD  i386 TONYBSD   

$ pkg_info grep jdk                                                           

diablo-jdk-1.6.0.07.02_8 Java Development Kit 1.6.0_07.02                       

                                                                                

遇到問題:                                                                      

Swing上除了Banner之外,其餘中文顯示出現問題                                     

                                                                                

方法:                                                                          

                                                                                

$ cd <JavaHome>/jre/lib/fonts/                                                  

JavaHome 是各位jdk的安裝路徑,我這裡是/usr/local/diablo-jdk1.6.0                

                                                                                

$ mkdir fallback                                                                

                                                                                

接下來就是把想要的字型,看是用複製的或是symbolic link,方到fallback目錄下       

這時可以運用find /usr/local -name *.ttf 來尋找                                  

以下拿我的當例子                                                                

                                                                                

$ find /usr/local -name "*.ttf"                                                 

發現/usr/local/lib/X11/fonts有很多字型                                          

其中有一個資料夾叫TTF裡面有個"文鼎PL中楷",這是我要的                         

                                                                                

$ cd /usr/local/lib/X11/fonts/TTF                                               

$ ln -s *.ttf /usr/local/diablo-jdk1.6.0/jre/lib/fonts/fallback/          

                                                                                

這樣就完成了,之後只要在main的開頭加上以下程式碼(或寫在constructor),凡是Swi\

ngGUI(JLabel, FileChooser…etc)從此就都能正常顯示中文了,祝各位使用愉快^^:  

                                                                                

Font f =  new Font("文鼎PL中楷",Font.PLAIN,12);                               

UIManager.put("Label.font",f);                                                  

UIManager.put("Label.foreground",Color.black);                                  

UIManager.put("Button.font",f);                                                 

UIManager.put("Menu.font",f);                                                   

UIManager.put("MenuItem.font",f);                                               

UIManager.put("List.font",f);                                                   

UIManager.put("CheckBox.font",f);                                               

UIManager.put("RadioButton.font",f);                                            

UIManager.put("ComboBox.font",f);                                               

UIManager.put("TextArea.font",f);                                               

UIManager.put("EditorPane.font",f);                                             

UIManager.put("ScrollPane.font",f);                                             

UIManager.put("ToolTip.font",f);                                                

UIManager.put("TextField.font",f);                                              

UIManager.put("TableHeader.font",f);                                            

UIManager.put("Table.font",f);                                                  

                                                                                

                                                                                

                                                                                

–                                                                              

※ 發信站: 批踢踢實業坊(ptt.cc)                                                 

◆ From: 140.113.242.246