当前位置: 技术问答>java相关
高手请进!!!十分感谢!
来源: 互联网 发布时间:2015-02-23
本文导语: 请问java.util.ResourceBundle类的getBundle(String baseName)方法中baseName字符串是什么意思,在什么地方定义相关的内容? | Load resources dynamically [internationalDynamic.java (main program)] import java.util.*; imp...
请问java.util.ResourceBundle类的getBundle(String baseName)方法中baseName字符串是什么意思,在什么地方定义相关的内容?
|
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}"},
};
}
[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}"},
};
}
|
随便写,只要和你要用的资源文件的名字相同就可以...
|
资源文件:
I。图像、声音文件。
II。带有信息字串和按钮标签的文本文件。
III。带有二进制数据的文件。
getBundle(String baseName);返回由baseName指定的ResourceBundle对象。
I。图像、声音文件。
II。带有信息字串和按钮标签的文本文件。
III。带有二进制数据的文件。
getBundle(String baseName);返回由baseName指定的ResourceBundle对象。