1回顶部 如何使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 green.label=Green blue.label=Blue 2回顶部 清单一和二定义了一个默认资源文件,和美国地区的资源文件。其中等号左边的字符串表示主键,它们是唯一的。为了获得主键对应的值,你可以调用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 …… } } 3回顶部 如果你对清单五的代码不进行本地化,她就可能会运行的很好。但当你的按钮被本地化为中文时,“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; */ 4回顶部 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")); } 5回顶部 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); } }); 6回顶部 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); } 7回顶部 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 # 它覆盖了默认资源束 Title=America red.label=Red green.label=Green blue.label=Blue # 文件Dorian_zh_CN.properties,是中国大陆地区的资源束 # 这个文件没有任何资源定义,从默认中国资源束继承 以下是这个程序远行后的截屏!
|
正在阅读:实战Java用户界面本地化实战Java用户界面本地化
2004-02-14 09:34
出处:PConline
责任编辑:linjixiong