java,awt,中文方框,中文亂碼

來源:互聯網
上載者:User

標籤:

用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,中文方框,中文亂碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.