码迷,mamicode.com
首页 > 编程语言 > 详细

java,awt,中文方框,中文乱码

时间:2015-06-08 06:18:37      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:

用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,中文方框,中文乱码

标签:

原文地址:http://my.oschina.net/airship/blog/463950

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!