java 图片 按钮代码,如何在Java中使用Unicode图像创建JButton?

博客讨论了在Java中尝试通过Unicode创建带有图像的JButton时遇到的问题。作者发现直接使用Unicode字符串无法正确显示某些字符,特别是表情符号。解决方案是将Unicode转换为整数,然后将其转化为字符字符串,最后创建JButton。这种方法成功地显示了Unicode列表中的表情符号。
摘要由CSDN通过智能技术生成

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 ?

解决方案

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值