Java user interface localization instance exploration

xiaoxiao2021-03-06  43

Internationalization is to make the program have sufficient flexibility to run in any region in any region in the world. The inevitable result of internationalization is localized - enabling a process to run in a specific region. This article attempts to demonstrate the Java user interface localization with a simple example. Java Language Nuclear Nuclear Based on Unicode3.0 (Java 1.4) provides internal support for different countries and different languages. Due to congenital reasons, Java is far more than C / C . In my opinion, localization must meet the following three conditions: 1, the program must be able to read, write, and operate localized text. 2, the program must meet local habits when displaying date and time, making digital formatting and sort substrings. (These requirements can be implemented through the java.text package) 3, all user visible text can be obtained at runtime, not directly written in the program. (Realize these requirements through the ResourceBundle class and his subclasses in Java.util package.) Implementing these three aspects can truly implement the internationalization of procedures. Let us first understand the area. Regions represent a geographical, political or cultural area. In Java, the area is represented by the Java.util.locale class. Regions often define in a language, the language is represented by its standard lowercase dual alphanumeric code. (For example, EN represents the UK, FR represents France, EN represents China), but sometimes language does not represent a region, then it is necessary to add a country or the region of the country (for example: EN_US represents the United States, en_tw). The Locale class saves a static default area that can be set and queried with local.setDefault () and local.getDefault (). A program can generate and use any number of non-default Locale objects. Let's take another UNICODE character encoding. Java uses Unicode's character encoding, which is itself a big step towards internationalization. Unicode encodes each of its characters to account for two bytes. Expressed in the form of / u ****. Unicode characters can be equivalent to other encoded characters (eg, from / u0020 to / u007e characters equivalent to 0x20 to 0x7e) of ASCII and ISO8859-1 characters. This article is mainly for the user interface, because I am using the resource bundle! So it is necessary to explain the resource bundle. To define a bunch of localized resources, you need to generate a subclass of a resourcebundle and provide the definition of the HandlegetObject () and getKeys () methods. In order to use the place from ResourceBundle in the program, you need to call a static getBundle () method, get a resourcebundle object with getBundle (), and then use the getObject () method to find resources according to the name. Of course, you can also use getString () to simply assign the return value of getObject () to a String object. GetBundle () method adopts BaseName_Language_Country_Variait ---- Didn't find it -> BaseName_Language_Country ---- Didn't find it -> BaseName_Language ---- Didn't find it -> BaseName algorithm to find the right resource. If you have not found it, you will throw a missingResourceException. Now let's take a simple example, how to make the Java program user interface. First our program needs to find the resource package associated with a specific Locale object, so you should define a local object to get the local default area! You can then call the RESourceBundle's getBundle method and incorporated the Locale object as a parameter.

Listing 1: Locale locale = locale.getDefault (); // Get area: Default // Get resource bundles. If not found, the MissingResourceException exception resourcebundle bundle = ResourceBundle.getBundle ("Properties.dorian", Locale);

Inventory "Properties.dorian" on behalf of the default resource file named Dorian under the Properties package. This will use the resource file! Let's take a look at how the resource file is defined. Listing 2:

# Dorian.properties is the default "DORIAN" resource bundle file. # As the Chinese, I use my own area as the default title = / u4E2D / u56fd; red.label = / u7Ea2 / u8272; green.label = / u7Eff / u8272; blue.label = / u84dd / u8272;

Listing 3:

# DORIAN_EN_US.PROPERTIES, is the resource bundle of the US area # It covers the default resource bundle title = American; red.label = red; green.label = green; blue.label = blue;

Listing one and second define a default resource file, and resource files in the US. The strings on the left side of the equal sign indicate the primary key, they are unique. In order to obtain a value corresponding to the primary key, you can call the GetString method of the ResourceBundle class and use the primary key as the parameter. In addition, the document is annotated in the document in the "#" number. It should be noted that "/ U4E2D / U56FD" in Listing 2 is the Unicode character code of the character "China". It is a Native2ASCII In.properties out.properties that uses Java's own Native2ASCII tool (Native2ASCII IN.Properties Out.properties), which is not garbled in the program interface. Listing 4:

Cmdred.Settext ("Red.Label"); cmdblue.setText (Bundle.GetString ("Blue.Label")); cmdgreen.setText (Bundle.GetString ("Green.Label");

CMDRED, CMDBLUE, CMDGreen in Listing II for the button. Bundle.getstring ("Red.Label") To get the value of the primary key in the resource file is the value of Red.label. Ok, this is so simple that the localization of the Java program user interface is so simple. However, to remind you that you should be extra care when writing event listener code for user interface events. Please see the code below. Listing 5:

public class MyApplet extends Japplet implements ActionListener {public void init () {JButton cancelButton = new JButton ( "Cancel"); CancelButton.addActionListener (this); ...} public void actionPerformed (ActionEvent e) {String s = e.getActionCommand (); IF (arg.equals ("ca Zancel"); docancel (); else ......}}

If you don't log locally for the code of the list, she may run very well. But when your button is localized into Chinese, "Cancel" changes to "cancel". At this time, you will not be willing to see. There are three ways to eliminate this potential problem! 1> Use the internal class without using a separate ActionPerformed program. 2> Use quotation marks without using tags to identify components. 3> Using the Name property to identify the component this example later is the first method to eliminate this problem. Listing 6: Complete code //:Native.java/**copyright (c) 2003 Dorian. All Rights reserved @ (#) MyNative.java 2003-12-21 @ Author Dorian @ version 1.0.0visit http: // www .Dorian.com / java / * / import java.awt. *; Import java.awt.Event. *; Import javax.swing. *; Import java.util. *; / ** This is a Java program interface place Example of this example This example uses a read attribute file to achieve purposes @see java.util.locale; @see java.util.resourceBundle; @see java.util.missingResourceException; * / public class mynative {public static void main (String " ] args) {JFrame frame = new MyNativeFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setResizable (false); frame.setVisible (true);. // Pop the window up}} class MyNativeFrame extends JFrame {public MyNativeFrame () {locale locale = locale.getDefault (); // Get the region: Default // Get the resource bundle.

If not found it will throw a MissingResourceException //"Properties.Dorian "Dorian is in the Properties to the default properties file filename ResourceBundle bundle = ResourceBundle.getBundle (" Properties.Dorian ", locale); setTitle (bundle.getString ("Title"); // Set Title Setsize (Width, Height); // set the window size. Panel = new mynativePanel (); container contentpane = getContentPane (); contentpane.add (//) The three buttons set to the three buttons by getting the value of the * .Label; bundle.getstring ("BLUE. label ")); panel.setCmdGreen (bundle.getString (" green.label "));} private MyNativePanel panel; private static final int WIDTH = 400; private static final int HEIGHT = 100;} class MyNativePanel extends JPanel {public MyNativePanel () {Layout = new borderLayout (); setout (layout); txt = new jtextfield (50); add (txt, layout.center); cmdred = new jbutton (); cmdblue = new jbutton (); cmdgreen = new jbutton (); Panel.add (cmdred); panel.add (cmdblue); panel.add (cmdgreen); add (panel, layout.south); cmdred.addacti onListener (new ActionListener () {public void actionPerformed (ActionEvent e) {String s = e.getActionCommand (); txt.setBackground (Color.red); txt.setText (s);}}); cmdBlue.addActionListener (new ActionListener () {Public void actionperformed (ActionEvent E) {string s = E.GetActionCommand (); txt.setBackground; txt.setText (s);}}); cmdgreen.addactionListener (new actionListener () {public Void ActionPerformed (ActionEvent E) {string s = E.GetActionCommand (); txt.setBackground (color.green); txt.setText (s);}});}}});}})}}} );

} Public void setCmdBlue (String s) {cmdBlue.setText (s);} public void setCmdGreen (String s) {cmdGreen.setText (s);} JPanel panel = new JPanel (); BorderLayout layout; private JTextField txt; private JButton Cmdred, cmdblue, cmdgreen;} // ~ resource file:

# Dorian.properties is the default "DORIAN" resource bundle file. # As the Chinese, I use my own area as the default title = / u4E2D / u56fdred.Label = / u7EA2 / u8272Green.label = / u7Eff / u8272blue.Label = / u84dd / u8272 # file Dorian_en_us.properties, is the resources of the United States Bouquet # It covers the default resource bundle = American.Label = GreenBlue.Label = Blue # file Dorian_zh_cn.properties, is the resource bundle in mainland China # This file does not have any resource definition, from the default Chinese resource bundle inheritance

The following is the screenshot after this program is running!

转载请注明原文地址:https://www.9cbs.com/read-86948.html

New Post(0)