java - 当我在 jar 文件上运行时,Unicode 未显示在我的应用程序中

标签 java netbeans unicode

我构建了一个 Java GUI 应用程序,并且有一些带有 Unicode 文本的 jLabel。当我从 NetBeans IDE 运行应用程序时,文本显示为正确的形式,但是当我从 .jar 文件运行时,文本显示为变形的形式。

我的代码:

try {
    jLabel1.setText(new String("ژمارا ناسنامی".getBytes(), "UTF-8"));
} catch (UnsupportedEncodingException ex) {
    Logger.getLogger(dataEntry.class.getName()).log(Level.SEVERE, null, ex);
}

输出:

enter image description here

最佳答案

仅尝试此操作:

jLabel1.setText("ژمارا ناسنامی");

事实上,您的错误是首先使用 默认编码 使用 getBytes() 对您的 String 进行编码,然后将其解码为 UTF-8,这是不正确的,因为您依赖的默认编码不能是 UTF-8 并且无用,因为 String 已经是 UTF-16 编码的,因此它已经覆盖了阿拉伯字符。

这是方法 String#getBytes() 的 Javadoc提醒一下:

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

如果你想正确编码String,你需要使用String#getBytes(Charset)String#getBytes(String)反而。但再一次,在这种特殊情况下甚至不需要它。

关于java - 当我在 jar 文件上运行时,Unicode 未显示在我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932296/

相关文章:

java - 如何过滤流映射期间抛出异常的元素

java - netbeans 6.* 中的解析延迟

java - 无法访问项目目录中的 XML 文件

java - 如何在Java Swing标签中显示X-Bar统计符号?

java - 修改表内 libGDX 堆栈收集的 actor 的维度属性

java - org.dom4j.DocumentException : Connection timed out: connect and Nested exception: Connection Nested exception

java - 优化sqlite应用程序启动

java - 替换 jtable 的内容

java - 以编程方式添加时无法识别带重音的 GWT 字符

mysql - 如何在mysql中保存像🎅🌠🎄🐰这样的符号?