Java用户界面本地化实例讲解

//: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,是中国大陆地区的资源束
       # 这个文件没有任何资源定义,从默认中国资源束继承

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值