標籤:
用java開發一個帶有托盤表徵圖的程式,
其它模組的中文顯示都是正常的,比如:
就只有托盤中點擊小表徵圖時彈出的菜單中的中文是方框(中文方塊),如下:
按照一般的經驗,以為是字型沒設定好,就用以下代碼設定字型:
Font f = new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 11);//宋體
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);
完了中文還是方塊,那就用以下代碼列印字型:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String script[] = ge.getAvailableFontFamilyNames();
for(String s:script){
System.out.print(s+",");
}
發現明明寫著有宋體。這下鬱悶了,試了一下午,還是沒有搞定。
終於在網上找到了救星(參考:http://scnjl.iteye.com/blog/1282172)。
原來是myeclipse的運行參數問題,真想罵人啊我。。。
下面說說解決:
第一:
在你的具有main函數的類也即你應用啟動並執行主類上點擊右鍵,選擇Run As中的Run Configurations,如:
第二,在Arguments標籤下的VM arguments中添加下面這行參數代碼,然後點擊應用。
-Dfile.encoding=GB18030
ok了,再運行,就終於出現正常的中文了:
終於完成,哈哈,
沒有亂碼,全世界都清淨了!!!!
java,awt,中文方框,中文亂碼