6 Replies - 1209 Views - Last Post: 22 September 2009 - 11:31 PM Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Output chinese character in GUI

Post icon  Posted 21 September 2009 - 04:50 PM

Hi! does anyone know how to output chinese character into a window in java.. I have google so many times and found nothing..any help would be nice ...thank you
Is This A Good Question/Topic? 0
  • +

Replies To: Output chinese character in GUI

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Output chinese character in GUI

Posted 22 September 2009 - 04:31 AM

Yes, you can do it.
You have to set the encoding from default(Cp1252) to UTF-8.
It worked for me on Eclipse.

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;

public class UseCustomFrame extends JFrame{

	private JLabel jlblChin = new JLabel("可以打華語字耶!!");
	public UseCustomFrame(){
		JPanel p1 = new JPanel();
		p1.add(jlblChin);
		add(p1, BorderLayout.CENTER);
	}
	public static void main(String[] args) {
		UseCustomFrame frame = new UseCustomFrame();
		frame.setTitle("Hehe");
		frame.setSize(200, 100);
		frame.setLocation(200, 100);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.getContentPane().setBackground(Color.yellow);
		
	}

}


This post has been edited by AntonWebsters: 22 September 2009 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: Output chinese character in GUI

Posted 22 September 2009 - 05:53 AM

Cut and paste unicode support can be a pitfall. I'd used Java's unicode notation. This assumes the OS has some level of font support for the language in question.

import java.awt.*;
import javax.swing.*;

public class ChineseTest extends JFrame{
	public ChineseTest() {
		addText(
			"\u4F60" // unicode ni
			+ "\u597D" // unicode hao
		);
		setTitle("Does your Java speak Chinese?");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
	}
	
	private void addText(String s) {
		JLabel lab = new JLabel(s);
		Font f = lab.getFont();
		lab.setFont(new Font(f.getFontName(), f.getStyle(), f.getSize()*5));
		add(lab);
	}
	
	public static void main(String[] args) {
		new ChineseTest().setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • ¿Dónde están mis pantalones?
  • member icon





Reputation: 1970
  • View blog
  • Posts: 14,482
  • Joined: 24-September 08

Re: Output chinese character in GUI

Posted 22 September 2009 - 07:06 AM

Does Locale come into play in this? *is curious....*
Was This Post Helpful? 0
  • +
  • -

#5 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: Output chinese character in GUI

Posted 22 September 2009 - 09:39 PM

Hey! thx all...anton. I tried your method tho, if other computer does not support the chinese font ??? will appear...and baavgi I tried yours hence, do you know where I might find a list of the chinese uni-code??
Was This Post Helpful? 0
  • +
  • -

#6 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Output chinese character in GUI

Posted 22 September 2009 - 11:08 PM

Here's a Chinese character to Unicode converter...
http://weber.ucsd.ed...icodemaker.html
After converting, make sure you add "\u" in front of the unicodes when you want to use them.
For example, 597D should be \u597D.
Was This Post Helpful? 1
  • +
  • -

#7 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: Output chinese character in GUI

Posted 22 September 2009 - 11:31 PM

ohh cool..i found that program on google before but wasn't sure how to use it..now i got it i have to type the chinese character in the it translate for me and uses the hex value...

Thx a lot..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1