精品推荐
Java用户界面本地化实例探索
日期:2007年5月2日 作者: 查看:[大字体
中字体 小字体 ]
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; } //~ 资源文件:
复制本页网址和标题,发送给你QQ/Msn的好友一起分享
上一篇:Java图形用户界面设计
下一篇:JBoss技术支持文档(一)
Java用户界面本地化实例探索 相关文章:
Java用户界面本地化实例探索 相关软件: