Solution for displaying Chinese garbled characters in MenuItem

Source: Internet
Author: User
Tags echo message

The first time I tried to add a System Tray Icon (TrayIcon) to the java program, I found that Chinese characters are garbled and small boxes are displayed in the menu, later, I found a netizen's article "MenuItem display Chinese garbled problem solution" on the Internet and solved it according to the ideas in it.
The reason for the problem is referenced in the article: 1. for the problem of the local system Regional Language Character Set, our system may use the default Character Set GB2312 or GBK. 2. Java source file encoding character problems, if you use eclipse, you can view the java source file encoding method. 3. The character issue during javac compilation is easily ignored. When you run javac, you can see that there is an encoding parameter that can be set-this is very important. 4. the encoding of the file to be read and the character set to be read are incorrect. If the string we want to use is not hard-coded in java source code, but read from the resource file, pay attention to this problem.
Solution: In the first case, we cannot control the language characters of the local system;
In the second case, convert the file encoding;
In the third case, when we check to determine that the Java source file encoding character is a UTF-8, set the parameters for javac compilation and add the encoding parameter, such as javac-encoding utf8 Test. java, but the relevant settings are not found in Eclipse, so we have to use the following methods: 1. currently, only java is found. the awt package will have this problem, and the swing package can well solve this character set conversion problem.
2. Use other compilation methods, such as ant and Maven, to configure javac. First, we will give a Maven example provided by a netizen article: <build>
<Plugins>
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-compiler-plugin </artifactId>
<Version> 2.3.2 </version>
<Configuration>
<Sources> 1.6 </source>
<Target> 1.6 </target>
<Encoding> utf8 </encoding>
</Configuration>
</Plugin>
....
However, I use Ant: <target name = "compile" depends = "copyfiles">
<Javac srcdir = "$ {sourcedir }"
Destdir = "$ {targetdir }"
Encoding = "UTF-8"
Target = "1.6"
Classpathref = "libraries"/>
</Target>
4. the character encoding and stream reading encoding of the file to be read. For a resource file, we also need to set its encoding to utf8 (refer to the Code 2nd, which is the same as setting the java source file encoding ), at the same time, you also need to set the stream read encoding when using stream reading (the default java runtime stream is jvm encoding, which is consistent with the system character set ).
The read stream code is as follows: BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (langFile), "utf8 "));
Note that when copying the resource file, it is best to set the encoding. For example, when copying the resource file with ant: <echo message = "copy conf"/>
<Copy file = "D: \ klaudisk-client \ conf \ settings. ini"
Tofile = "D: \ klaudisk-client \ target \ conf \ settings. ini" overwrite = "true" encoding = "utf8"/>
You can also use the Properties. load () method to load the. properties file (this file is a file processed by the navie2asc program under java bin), so you don't have to worry about the 4th point problem.
My problem is in 3rd cases. I used Ant to compile and generate the jar!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.