正在阅读:实战Java用户界面本地化实战Java用户界面本地化

2004-02-14 09:34 出处:PConline 作者:sxhv998/CSDN 责任编辑:linjixiong

  清单一和二定义了一个默认资源文件,和美国地区的资源文件。其中等号左边的字符串表示主键,它们是唯一的。为了获得主键对应的值,你可以调用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 ……

  }

  }
 

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品