您的位置:首页 > 其它

当打印出来的字符不是乱码,而现实在控件或者draw出来的string是乱码时的解决方案

2008-06-16 21:11 531 查看
可能是JRE的中文字库不支持引起,所以添加一个可以支持的中文字库
解决办法:
第一步:下载中文字库,我使用的是simsun.ttc,可以直接在Windows(我用的是XP的系统)中找到,入径为C:/WINDOWS/Fonts下的simsun.ttc
也可以在网站上下载推荐天网网站:http://file.tianwang.com/cgi-bin/search?word=simsun.ttc
第二步:将中文字库simsun.ttc放入Jre的字库中,操作如下:
cd<JAVA_HOME>/lib/fonts或者cd<JAVA_HOME>/jre/lib/fonts
mkdirfallback(fallback代表存放后备语言的文件夹)
其中<JAVA_HOME>是你安装jdk/jre的路径,我的是/usr/lib/jdk1.5。复制或者链接一个中文字体至其下:
ln-s/usr/share/fonts/truetype/simsun.ttf/usr/lib/jdk1.5/jre/lib/fonts/fallback/simsun.ttf

第三步:有了上面的步骤之后,就可以在代码中编码实现显示中文了,操作如下:
在main函数中的开头处添加如下代码:
Fontf=newFont("宋体",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);

以上代码代表了在整个程序中的相关组件都使用定义好"f"字体,从而就不会存在中文乱码了,也省却了对所有组件单独设置的麻烦。



打印所有支持的字体

publicclassAA{

publicstaticvoidmain(String[]args){

//DeterminewhichfontssupportChinesehere...

Vectorchinesefonts=newVector();

Font[]allfonts=GraphicsEnvironment.getLocalGraphicsEnvironment()

.getAllFonts();

intfontcount=0;

Stringchinesesample="/u4e00";

for(intj=0;j<allfonts.length;j++){

System.out.println(j+"-"+allfonts[j].getFontName());

if(allfonts[j].canDisplayUpTo(chinesesample)==-1){

chinesefonts.add(allfonts[j].getFontName());

}

fontcount++;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐