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

可能是JRE的中文字库不支持引起,所以添加一个可以支持的中文字库 
    解决办法: 
    第一步:下载中文字库,我使用的是simsun.ttc,可以直接在Windows(我用的是XP的系统)中找到,入径为C:/WINDOWS/Fonts下的simsun.ttc 
    也可以在网站上下载 推荐天网网站: 
    http://file.tianwang.com/cgi-bin/search?word=simsun.ttc 

    第二步:将中文字库simsun.ttc放入Jre的字库中,操作如下: 
    cd /lib/fonts或者cd /jre/lib/fonts 
    mkdir fallback (fallback代表存放后备语言的文件夹) 
    其中 是你安装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 函数中的开头处添加如下代码: 
                Font f =  new Font("宋体",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"字体,从而就不会存在中文乱码了,也省却了对所有组件单独设置的麻烦。 
 
 
打印所有支持的字体
public class AA {

	public static void main(String[] args) {

		// Determine which fonts support Chinese here ...

		Vector chinesefonts = new Vector();

		Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment()

				.getAllFonts();

		int fontcount = 0;

		String chinesesample = "/u4e00";

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

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

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

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

			}

			fontcount++;

		}

	}

}

你可能感兴趣的:(string,fonts,java,vector,windows,class)