Problem in showing chinese characters from UTF-16 to GB2312
807569Aug 24 2006 — edited Aug 26 2006Hi All!
I'm having problem regarding showing chinese characters. I have Unicode of chinese characters and I want to show them with GB2312 [Simplified Chinese] encoding. But I'm failing in this regard. Please help me in this regard. Here is my sample code:
// Determine which fonts support Chinese here ...
String chinesesample = "\u5582";
Vector chinesefonts = new Vector();
Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
int fontcount = 0;
for (int j = 0; j < allfonts.length; j++) {
if (allfonts[j].canDisplayUpTo(chinesesample) == -1) {
chinesefonts.add(allfonts[j].getFontName());
}
fontcount++;
}
// at my machine output is only "Arial Unicode MS" so thats why
//im using the same font
for (int i = 0; i < chinesefonts.size(); i++) {
System.out.println("Chinese Fonts..: " + chinesefonts.get(i).toString());
}
//showing chinese character from unicode to gb2312
try {
System.out.println("Sample String..: " + new String(chinesesample.getBytes("GB2312")));
JOptionPane.showMessageDialog(null, "Chinese Text..: " + new String(chinesesample.getBytes("GB2312")));
}
catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
OR ---------------another sample code
//I'm unable to show the
//actuall charater. It is showing some garbage, which is totally
//not understandable...
mLabel = new JLabel();
try {
mLabel.setText(new String("\u5582".getBytes("GB2312")));
}
catch (UnsupportedEncodingException ex) {
}
mLabel.setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));
content.add(mLabel, BorderLayout.NORTH);
Please help me in this regard, because I'm stuck in there....
Regards,
KS