如何使java程序用户界面地方化?首先我们的程序需要查找特定locale对象关联的资源包,所以应该定义一个local对象,来获取本地默认的地区!然后可以调用resourcebundle的getbundle方法,并将locale对象作为参数传入。
清单一:
locale locale = locale.getdefault(); //获取地区:默认
//获取资源束。如未发现则会抛出missingresourceexception异常
resourcebundle bundle = resourcebundle.getbundle("properties.dorian",locale);
清单一中的”properties.dorian”代表properties包下以dorian命名的默认资源文件。
这样就可以使用资源文件了!让我们来看看资源文件是如何定义的。
清单二:
# dorian.properties是默认的"dorian"资源束文件。
# 作为中国人,我用自己的地区作为默认
title=/u4e2d/u56fd
red.label=/u7ea2/u8272
green.label=/u7eff/u8272
blue.label=/u84dd/u8272
清单三:
# 文件dorian_en_us.properties,是美国地区的资源束
# 它覆盖了默认资源束
title=america
red.label=red
green.label=green
blue.label=blue
清单一和二定义了一个默认资源文件,和美国地区的资源文件。其中等号左边的字符串表示主键,它们是唯一的。为了获得主键对应的值,你可以调用resourcebundle类的getstring方法,并将主键作为参数。此外,文件中以“#”号开头的行表示注释行。需要注意的是清单二中的“/u4e2d/u56fd”,它是字符“中国”的unicode字符码。是使用java自带的native2ascii工具转换的(native2ascii in.properties out.properties),这是为了不在程序界面中产生乱码。
清单四:
cmdred.settext(bundle.getstring("red.label"));
cmdblue.settext (bundle.getstring("blue.label"));
cmdgreen.settext (bundle.getstring("green.label"));
清单二中的cmdred、cmdblue、cmdgreen 为按钮。bundle.getstring("red.label")为得到资源文件中主键是red.label的值。
好了到此为止java程序用户界面的本地化就是这么简单^_^不过,要提醒你的是在为用户界面事件编写事件监听器代码时,要格外小心。请看下面这段代码。
清单五:
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(“cancel”);
docancel();
else ……
}
}
如果你对清单五的代码不进行本地化,她就可能会运行的很好。但当你的按钮被本地化为中文时,“cancel”变为了“取消”。这时就…………下面有三个方法可以消除这个潜在的问题!
1> 使用内部类而不使用独立的actionperformed程序。
2> 使用引号而不使用标签来标识组件。
3> 使用name属性来标识组件
本例稍后的代码就是采用第一种方法来消除这个问题的。
清单六:完整的代码
//:mynative.java
/**
copyright (c) 2003 dorian. all rights reserved
@(#)mynative.java 2003-12-21
@author dorian
@version 1.0.0
visit http://www.dorian.com/java/
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
这是一个将java程序界面地方化的例子
本例采用读取属性文件来达到目的
@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();//获取地区:默认
//获取资源束。如未发现则会抛出missingresourceexception异常
//"properties.dorian"为在properties下以dorian为文件名的默认属性文件
resourcebundle bundle = resourcebundle.getbundle(
"properties.dorian",locale);
settitle(bundle.getstring("title"));//通过getstring()的返回值来设置title
setsize(width,height); // set the window size.
panel=new mynativepanel();
container contentpane=getcontentpane();
contentpane.add(panel);
//通过获取资源束中*.label的值对三个按钮设置其label
panel.setcmdred(bundle.getstring("red.label"));
panel.setcmdblue(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();
setlayout(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.addactionlistener(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(color.blue);
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 setcmdred(string s){
cmdred.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;
}
//~
资源文件:
# dorian.properties是默认的"dorian"资源束文件。
# 作为中国人,我用自己的地区作为默认
title=/u4e2d/u56fd
red.label=/u7ea2/u8272
green.label=/u7eff/u8272
blue.label=/u84dd/u8272
# 文件dorian_en_us.properties,是美国地区的资源束
# 它覆盖了默认资源束
title=america
red.label=red
green.label=green
blue.label=blue
# 文件dorian_zh_cn.properties,是中国大陆地区的资源束
# 这个文件没有任何资源定义,从默认中国资源束继承
以下是这个程序远行后的截屏!

闽公网安备 35060202000074号