资源文件:
I。图像、声音文件。
II。带有信息字串和按钮标签的文本文件。
III。带有二进制数据的文件。
getBundle(String baseName);返回由baseName指定的ResourceBundle对象。

解决方案 »

  1.   

    Load resources dynamically
    [internationalDynamic.java (main program)] import java.util.*;
    import java.text.*;
    import java.awt.*;public class internationalDynamic {
      myFrame f;   
       
      public static void main(String args[]) {
        internationalDynamic i =
           new internationalDynamic();
        i.doit();
        }  public void doit() {
        f = new myFrame();
        f.initLocale();  
        f.initGUI();
        } 
      }
    class myFrame extends Frame {
      static Locale[] localesSupported = { 
         Locale.US, Locale.FRANCE, Locale.GERMANY 
         };  int localeChoosen = 0;
      Locale localeCurrent;
      ResourceBundle rb;  CheckboxGroup cbg;
      Button btnQuit;
      Checkbox aCb0, aCb1, aCb2;
      Label today;  void myFrame() {  }  public void initLocale(){
        localeCurrent = localesSupported[localeChoosen];
        this.setLocale(localeCurrent);        
        rb = ResourceBundle.getBundle("resourcesDynamic", localeCurrent);
        }  public void initGUI(){
        setTitle (rb.getString("title"));
        setLayout(new FlowLayout());
       
        // RADIO buttons 
        cbg = new CheckboxGroup();
        aCb0 = new Checkbox
          (rb.getString("aCb0"), cbg, false);
        aCb1 = new Checkbox
          (rb.getString("aCb1"), cbg, false);
        aCb2 = new Checkbox
          (rb.getString("aCb2"), cbg, false);
        add(aCb0);
        add(aCb1);
        add(aCb2);
          
        // default RADIO button
        String CbDef =  rb.getString("aCbdefault");
        if (CbDef.equals("aCb0")) 
           aCb0.setState(true);
        else if (CbDef.equals("aCb1"))  
           aCb1.setState(true);
        else
           aCb2.setState(true);    // QUIT button
        btnQuit = new Button
         (rb.getString("btnQuit"));
        add(btnQuit);    Date d = new Date();
        MessageFormat mf = new MessageFormat
           (rb.getString("today"));
        today = new Label
          (mf.format(new Object [] { d })); 
        add(today);    //
        setSize(400,100);
        setVisible(true);   
        }  public boolean action(Event e, Object o){
        if (e.target == btnQuit) {
           System.exit(0);
           return true;
           }
        else if (e.target instanceof Checkbox) {
           if (e.target == aCb0) localeChoosen = 0;
           else if (e.target == aCb1) localeChoosen = 1;     
           else if (e.target == aCb2) localeChoosen = 2;
           removeAll();
           initLocale();
           initGUI();
           return true;
           }
        return false;
        } 
      }
     
    [resourcesDynamic.java (default resources)] import java.util.*;
    import java.awt.*;
    import java.text.*;public class resourcesDynamic extends ListResourceBundle {
      public Object [][] getContents() {
        return contents;
        }  static final Object[][] contents =   {
        { "title", "Example" },
        { "aCb0" , "United States" } ,
        { "aCb1", "France" } ,
        { "aCb2"  , "Germany"},
        { "aCbdefault"  , "aCb0" },
        { "btnQuit"  , "Quit"},
        { "today"  , " (def) {0,date,long}"},
        };
       }
     
    [resourcesDynamic_en.java (english language resources)] import java.util.*;
    import java.awt.*;
    import java.text.*;public class resourcesDynamic_en extends ListResourceBundle {
      public Object [][] getContents() {
         return contents;
         }   static final Object[][] contents =  {
          { "title", "Example" },
          { "aCb0" , "United States" } ,
          { "aCb1", "France" } ,
          { "aCb2"  , "Germany"},
          { "aCbdefault"  , "aCb0" },
          { "btnQuit"  , "Quit"},
          { "today"  , " (def) {0,date,long}"},
         };
       }
     
    [resourcesDynamic_fr.java (french language resources)] import java.util.*;
    import java.awt.*;
    import java.text.*;public class resourcesDynamic_fr extends ListResourceBundle {
      public Object [][] getContents() {
         return contents;
         }  static final Object[][] contents =   {
         { "title", "Exemple" },
         { "aCb0" , "Etats-Unis" } ,
         { "aCb1", "France" } ,
         { "aCb2", "Allemagne" },
         { "aCbdefault", "aCb1" },   
         { "btnQuit", "Quitter" },
         { "today"  , " (fr) {0,date, dd/MM/yyyy}"},
         };
       }
     
    [resourcesDynamic_de.java (german language resources)] import java.util.*;
    import java.awt.*;
    import java.text.*;public class resourcesDynamic_de extends ListResourceBundle {
      public Object [][] getContents() {
        return contents;
        }
      static final Object[][] contents =  {
         { "title", "Beispiel" },
         { "aCb0" , "Vereinigte Staaten" } ,
         { "aCb1", "Frankreich" } ,
         { "aCb2", "Deutschland" },
         { "aCbdefault", "aCb2" },
         { "btnQuit", "verlassen"},
         { "today"  , " (de) {0,date,dd.MM.yyyy}"},
         };
       }
     
      

  2.   

    Load resources via a resources file
    With the previous JAVA How-to, the resources were stored in classes. The drawback is when there is a modification, we must recompile the class. With a resources file, we simply add or modify the resource in a special file with a regular text editor. JDK1.1 provides a class, PropertyResourceBundle, to use properties file very easily. In fact, from the programmer's point of view, there is no difference if the resources are stored in a class or a file. The ResourceBundle will look first for classes and if not found, it will look for properties files. We don't have to specify filenames, the ResourceBundle will construct the filename using the same mechanism used for classes. The file must have the extension .properties. 
    The properties files are permitted in application or the Appletviewer. With Netscape v4 properties files are not permitted for some unknown security reason.The ResourceBundle try to load the properties file from the current CodeBase() or directory. To specify a differnet directory, use "." instead of "/". For example, let's say that the properties file is in a subdirectory called "subdir" (from the current directory). Then you can load the resourcesDynamic resources file with ResourceBundle.getBundle("subdir.resourcesDynamic", localeCurrent);
     Classes mentionned in the resource file need to be accessible via the Classpath. [internationalDynamic.java] import java.util.*;
    import java.text.*;
    import java.awt.*;
    import java.applet.*;public class internationalDynamic extends Applet {
      myFrame f;    
        
      public static void main(String args[]) {
         internationalDynamicP i =
            new internationalDynamic();
         i.doit();
         }
         
      public void init() {
         internationalDynamic i =
            new internationalDynamic();
         i.doit();
         }  public void doit() {
         f = new myFrame();
         f.initLocale();    
         f.initGUI();
         }  
      }class myFrame extends Frame {
      static Locale[] localesSupported = { 
         Locale.US, Locale.FRANCE,  Locale.GERMANY 
         };
      int localeChoosen = 0;
      Locale localeCurrent;  CheckboxGroup cbg;
      Button btnQuit;
      Checkbox aCb0, aCb1, aCb2;
      Label today;
      ResourceBundle rb;  void myFrame() {  }
      public void initLocale(){
        localeCurrent = localesSupported[localeChoosen];
        this.setLocale(localeCurrent);          
        rb = ResourceBundle.getBundle("resourcesDynamic", localeCurrent);
        }  public void initGUI(){
        setTitle (rb.getString("title"));
        setLayout(new FlowLayout());    // RADIO buttons    
        cbg = new CheckboxGroup();
        aCb0 = new Checkbox
          (rb.getString("aCb0"), cbg, false);
        aCb1 = new Checkbox
          (rb.getString("aCb1"), cbg, false);
        aCb2 = new Checkbox
          (rb.getString("aCb2"), cbg, false);
        add(aCb0);
        add(aCb1);
        add(aCb2);          
        // default RADIO button
        String CbDef =  rb.getString("aCbdefault");
        if (CbDef.equals("aCb0")) 
           aCb0.setState(true);
        else if (CbDef.equals("aCb1"))  
           aCb1.setState(true);
        else
           aCb2.setState(true);
        // QUIT button
        btnQuit = new Button
           (rb.getString("btnQuit"));
        add(btnQuit);    //
        setSize(400,100);
        setVisible(true);   
        }  public boolean action(Event e, Object o){
        if (e.target == btnQuit) {
           System.exit(0);
           return true;
           }
        else if (e.target instanceof Checkbox) {
           if (e.target == aCb0) localeChoosen = 0;
           else if (e.target == aCb1) localeChoosen = 1;        
           else if (e.target == aCb2) localeChoosen = 2;
           removeAll();
           initLocale();
           initGUI();
           return true;
           }
         return false;
         }  
      }
     
    [resourcesDynamic.properties] title=Example
    aCb0=United States
    aCb1=France
    aCb2=Germany
    aCbdefault=aCb0
    btnQuit=Quit
     
    [resourcesDynamic_fr.properties] title=Exemple
    aCb0=Etats-Unis
    aCb1=France
    aCb2=Allemagne
    aCbdefault=aCb1
    btnQuit=Quitte
     
    [resourcesDynamic_de.properties] title=Beispiel
    aCb0=Vereinigte Staaten
    aCb1=Frankreich
    aCb2=Deutschland
    aCbdefault=aCb2
    btnQuit=verlassen