// example1.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; public class example1 extends Applet implements ItemListener { Locale l; TextArea ta; // =============================================== // getInsets // // Return the layout margins of the four different // sides of the applet's panel container. By // default, these margins are set to 0. Setting // the margins to 5 pixels each gives us a nicer // looking applet. The panel container's layout // manager calls this method. // // Arguments: // // none // // Return: // // container's insets // =============================================== public Insets getInsets () { return new Insets (5, 5, 5, 5); } // ====================== // init // // Initialize the applet. // // Arguments: // // none // // Return: // // none // ====================== public void init () { // Set BorderLayout to be the applet's new layout manager (FlowLayout is the default). setLayout (new BorderLayout ()); // Create a text area object (which will hold locale information). ta = new TextArea (); // Add text area object to top of applet object's border area. add ("North", ta); // Create a panel object and add a label object to this panel. Panel p = new Panel (); p.add (new Label ("Language: ")); // Create a Choice object, languages, and add English, French, German, and Italian text items to this // object. Choice languages = new Choice (); languages.addItem ("English"); languages.addItem ("French"); languages.addItem ("German"); languages.addItem ("Italian"); p.add (languages); // Add a reference to the current applet object as a listener to languages. Whenever a new item // is selected from languages, the text area control will be repopulated with new locale information. // Add languages to the panel. languages.addItemListener (this); // Set the panel's background color. p.setBackground (Color.green); // Add languages label and choice list panel to bottom of applet object's border area. add ("South", p); // Create an initial locale object. The first argument to the constructor describes the // locale's language (en - English) while the second constructor argument describes the // locale's country (US - United States). Both arguments are defined by the International // Standards Organization. l = new Locale ("en", "US"); // Populate text area object with initial locale information. populate (); // Set default focus to the languages object. languages.requestFocus (); // Set the applet's background color. setBackground (Color.green.darker ()); } // =========================================================================== // itemStateChanged // // This method is called whenever a selection is made to the languages object. // // Arguments: // // e - event object describing selection change. // // Return: // // none // =========================================================================== public void itemStateChanged (ItemEvent e) { if (e.getStateChange () == ItemEvent.SELECTED) { // Obtain languages item (English, French, German). String language = (String) e.getItem (); // Based on selected languages item, establish a new default locale. // The first parameter to the Locale object describes a language and // the second parameter describes a country. Both values are defined // by the International Standards Organization. if (language.equals ("English")) l = new Locale ("en", "US"); else if (language.equals ("French")) l = new Locale ("fr", "FR"); else if (language.equals ("German")) l = new Locale ("de", "DE"); else l = new Locale ("it", "IT"); // Re-populate the text area object. populate (); } } // =================================================== // populate // // Populate text area control with locale information. // // Arguments: // // none // // Return: // // none // =================================================== private void populate () { StringBuffer sb = new StringBuffer (); sb.append ("Language Code = " + l.getLanguage () + "\n"); sb.append ("Country Code = " + l.getCountry () + "\n"); sb.append ("Variant = " + l.getVariant () + "\n"); sb.append ("ISO 3-letter Language Abbreviation = " + l.getISO3Language () + "\n"); sb.append ("ISO 3-letter Country Abbreviation = " + l.getISO3Country () + "\n"); sb.append ("Display Language = " + l.getDisplayLanguage (l) + "\n"); sb.append ("Display Country = " + l.getDisplayCountry (l) + "\n"); sb.append ("Display Variant = " + l.getDisplayVariant (l) + "\n"); sb.append ("Display Name = " + l.getDisplayName (l) + "\n"); // Populate text area control with locale information. ta.setText (sb.toString ()); } }