Ebookmgr multi-language and icon

Source: Internet
Author: User

Icon

First, let's talk about how to display an icon in a jbutton or jmenuitem:

 

 

The icons supported by jbutton or jmenuitem usually have a constructor. Take jmenuitem as an example:

 

Jmenuitem (string text, icon)
 

 

You only need to use this constructor, but how to obtain an icon object?

To obtain an icon, you must first have an image file and put it in a specific location. This location is usually in the same directory as Java or in its subdirectory (the runtime environment is required, for example, in the classes directory or jar file, these image files are also located at the same location ).

Take ebookmgr as an example,CodeIn the ebookmgr directory, the image file is placed in the ebookmgr. Images directory of the subdirectory. There are two steps to get the icon:

Step 1:

URL imageurl = getclass (). getresource ("images/menu/setting.png ")
 

Step 2:

Icon imageicon = new imageicon (imageurl)
 

Then you can use the constructor mentioned above. Note that during the imageurl generation process, the relative path between the class of the constructed icon and the image file is used (that is, images/menu/setting.png rather than ebookmgr/images/menu/setting.png ).

 

I18n

Now let's talk about the slightly more complicated i18n. The main idea of i18n is to divide all text labels into different sets by language and put them in different files (Here we take the attribute file as an example) when using a dynamic method to access these files, the dynamic meaning is thatProgramThe running region and language automatically obtain the required text tags from the corresponding file type.

 

The meaning of "dividing all text labels by language" is:

Define a series of files named basename_languagecode_countrycode.properties, which are usually stored in a specific directory (usually the resources subdirectory ).

The following file is defined in the ebookmgr. Resources Directory:

Ui. properties default property file. This file is used when the corresponding region cannot match a specific property file (for example, Puerto Rico)

Ui_en_us.properties English United States

Ui_zh_cn.properties (Chinese)

Ui_ja_jp.properties Japanese

 

 

The "dynamic method" is supported by the java. util. resourcebundle class.

Resourcebundle res = resourcebundle. getbundle ("ebookmgr. Resources. UI", locale. getdefault (); <br/> string value = res. getstring (key );

Pay attention to the following two points: ebookmgr. Resources. UI and locale. getdefault. Therefore, res indicates the content of a specific language version. (You can input a specified parameter to change the default region. This is the implementation principle of the English or Japanese topic on the Chinese platform)

Then you can use the getstring (string key) method to obtain the corresponding string.

 

By the way, non-ASCII characters in the properteis file will be automatically escaped. See the following:

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.