java 菜單 中文 亂碼問題解決方案(使用idea/eclipse) swing MenuItem亂碼

 

java swing MenuItem亂碼

2016年12月30日 13:32:08751人閱讀 評論(0) 收藏 舉報
用java開發一個帶有托盤圖標的程序, 
其它模塊的中文顯示都是正常的,好比: 
 
就只有托盤中點擊小圖標時彈出的菜單中的中文是方框(中文方塊),以下: 
 

按照通常的經驗,覺得是字體沒設置好,就用如下代碼設置字體: 
Java代碼   收藏代碼
  1. Font f = new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 11);//宋體  
  2. UIManager.put("Label.font",f);  
  3.         UIManager.put("Label.foreground",Color.black);  
  4.         UIManager.put("Button.font",f);  
  5.         UIManager.put("Menu.font",f);  
  6.         UIManager.put("MenuItem.font",f);  
  7.         UIManager.put("List.font",f);  
  8.         UIManager.put("CheckBox.font",f);  
  9.         UIManager.put("RadioButton.font",f);  
  10.         UIManager.put("ComboBox.font",f);  
  11.         UIManager.put("TextArea.font",f);  
  12.         UIManager.put("EditorPane.font",f);  
  13.         UIManager.put("ScrollPane.font",f);  
  14.         UIManager.put("ToolTip.font",f);  
  15.         UIManager.put("TextField.font",f);  
  16.         UIManager.put("TableHeader.font",f);  
  17.         UIManager.put("Table.font",f);  

完了中文仍是方塊,那就用如下代碼打印字體: 
Java代碼   收藏代碼
  1. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  2. String script[] = ge.getAvailableFontFamilyNames();  
  3. for(String s:script){  
  4. System.out.print(s+",");  
  5. }  

發現明明寫着有宋體。這下鬱悶了,試了一下午,仍是沒有搞定。 
終於在網上找到了救星(參考:http://scnjl.iteye.com/blog/1282172)。 
原來是myeclipse的運行參數問題,真想罵人啊我。。。 
下面說說解決: 
第一: 
在你的具備main函數的類也即你應用運行的主類上點擊右鍵,選擇Run As中的Run Configurations,以下圖: 


第二,在Arguments標籤下的VM arguments中添加下面這行參數代碼,而後點擊應用。 
      
      -Dfile.encoding=GB18030 


在次運行 

 
相關文章
相關標籤/搜索