【发布时间】:2016-07-29 07:32:45
【问题描述】:
我正在用 J2SE 开发聊天应用程序,它还可以发送表情符号给其他用户。
应用程序使用https://github.com/vdurmont/emoji-java(Vdurmont Emoji-java-2.1 jar),
我遵循了该链接上描述的所有内容,在开发环境中一切都运行良好,但是当我为相同的 jar 制作时,当我在网络上向其他用户发送表情符号时,它会显示代码(ðŸ~¡ 和?)。
首先我认为这是从文件夹加载文件的问题,所以使用了 ClassLoader, 为了获得正确的图像,但在创建 jar 时它显示?(问号),所以我删除了该代码以便更好地理解你。
代码如下:
public ChatUI() {
initComponents();
this.setLayout(new WrapLayout(FlowLayout.LEFT, 5, 5));
for (int i = 0; i < imageHexaCode.length; i++)
{
final javax.swing.JLabel imogis = new javax.swing.JLabel("&#x" + imageHexaCode[i] + ";");
imogis.setCursor(new Cursor(Cursor.HAND_CURSOR));
imogis.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[i] + ".png")));
imogis.setHorizontalTextPosition(JLabel.CENTER);
imogis.setVerticalTextPosition(JLabel.BOTTOM);
imogis.setFont(new Font(null, Font.PLAIN, 1));
imogis.setForeground(Color.white);
final int aa = i;
imogis.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel jl = new JLabel("&#x" + imageHexaCode[aa] + ";");
jl.setName("&#x" + imageHexaCode[aa] + ";");
jl.setFont(new Font(null, Font.PLAIN, 1));
jl.setHorizontalTextPosition(JLabel.CENTER);
jl.setVerticalTextPosition(JLabel.BOTTOM);
jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png")));
jl.setForeground(Color.white);
ChatPaneWrite.jtp.insertComponent(jl);
System.out.println("" + imogis.getText());
// you can open a new frame here as
// i have assumed you have declared "frame" as instance variable
}
});
this.add(imogis);
}
this.revalidate();
this.repaint();
}
其中 imageHexaCode 是表情符号字符串数组。
static String[] imageHexaCode = {
"1f621",
"1f608",
"2764",
"1f494"
};
& jtp 是 JTextPane,当用户点击标签时插入表情符号组件
ChatPaneWrite.jtp.insertComponent(jl);
表情符号存储在我正在编写的同一个包中,这就是我没有直接使用 ClassLoader 的原因
jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png")));
也可以写成ChatUI包
jl.setIcon(new javax.swing.ImageIcon(getClass().ClassLoader.getResource("ChatUI/emoji_" + imageHexaCode[aa] + ".png")));
这里是快照:
在接收端的 jar 中显示这样的表情
请帮我解决。
提前非常感谢大家
【问题讨论】:
-
您是否也在导出 JAR 中的图像?
-
@abdulrahmank 是的,图像可以正确导出并显示在用户界面上,只有发送和接收有问题。
-
尚未进入您的代码,但从 UI 上显示的内容来看,它对我来说看起来很垃圾,可能是由不正确的字符编码引起的。 (例如,发件人正在发送 UTF-16 字符,而您将它们解释为扩展的 ASCII)。如果是这种情况,它也可以解释为什么您未能查找表情符号
-
@AdrianShum:如果我按照你说的做,那为什么表情符号在开发环境中可以正常工作?
-
可能,它与 NetBeans IDE 当前运行时项目工作正常但是在@DheerajUpadhyay 使用所有必要的 lib 构建/制作 .jar 文件后,会出现 Emoji unicode 解析问题