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