Warm tip: This article is reproduced from stackoverflow.com, please click
java swing unicode jbutton

How to create a JButton with unicode image in Java?

发布于 2020-03-27 10:28:08

I have the following code :

  JButton Get_Unicode_Button(String unicodeText)
  {
    JButton button=new JButton("\\u"+unicodeText);
//    JButton button=new JButton("\u2605");
//    JButton button=new JButton("\u267b");
//    JButton button=new JButton("\u1F602");  // ?
    return button;
  }

I want to get a button displaying an image from unicode, I have a list of unicodes like this : "2605", "267b", "1F602", but it seems the way I implemented it above doesn't work, what's the right way to do it ?

Especially the 3rd line "\u1F602", even if I hard code it like above, it won't work, why ?

Questioner
Frank
Viewed
101
Frank 2019-07-13 23:01

OK, I got it :

  JButton Get_Unicode_Button(String unicodeText)
  {
    int emojiCodePoint=Integer.parseInt(unicodeText,16);
    String emojiAsString=new String(Character.toChars(emojiCodePoint));
    JButton button=new JButton(emojiAsString);
    return button;
  }