之前說到要想看看Swing。不知道爲啥我第一個想到的問題是界面語言顯示的國際化問題。管他呢,既然隨心所遇學,想到了就實現來看看。
我們所要做的就是在切換語言的時候,從對應的資源文件中讀取值信息,顯示出來。
- /**
- * 觀察者中心,用於統一通知發送變更信
- *
- * @author lihzh
- * @date 2012-3-17 下午9:54:15
- */
- public class ObserveCenter extends Observable {
- private static ObserveCenter observeCenter;
- private ObserveCenter(){}
- public static ObserveCenter getInstance() {
- if (observeCenter == null) {
- observeCenter = new ObserveCenter();
- }
- return observeCenter;
- }
- /**
- * 發送變更消息, 相當於{@link #notifyChange(null)}
- *
- * @author lihzh
- * @date 2012-3-17 下午9:58:19
- */
- public void notifyChange() {
- notifyChange(null);
- }
- /**
- * 發送變更信息
- *
- * @param obj
- * @author lihzh
- * @date 2012-3-17 下午10:01:50
- */
- public void notifyChange(Object obj) {
- setChanged();
- notifyObservers(obj);
- }
- }
- /**
- * 當前位置(國際化信息所用)管理類
- *
- * @author lihzh
- * @date 2012-3-17 下午4:26:43
- */
- public class LocaleHandler {
- private static final Logger logger = LoggerFactory.getLogger(LocaleHandler.class);
- public static final Locale DEFAULT_LOCAL = Locale.CHINA;
- private static Locale locale = DEFAULT_LOCAL;
- static {
- ObserveCenter.getInstance().addObserver(new I18nUIHandler());
- ObserveCenter.getInstance().addObserver(new ResourceBundleLoader());
- }
- /**
- * 獲取國際化區域信息
- *
- * @return
- * @author lihzh
- * @date 2012-3-17 下午9:32:13
- */
- public static Locale getLocale() {
- return locale;
- }
- /**
- * 設置國際化區域信息
- *
- * @param locale
- * @author lihzh
- * @date 2012-3-17 下午9:31:47
- */
- public static void setLocale(Locale locale) {
- LocaleHandler.locale = locale == null ? DEFAULT_LOCAL : locale;
- logger.info("Current locale is: [{}]", LocaleHandler.locale.toString());
- refresh();
- }
- /**
- * 設置國際化區域信息,相當於{@link #setLocale(DEFAULT_LOCAL)}
- *
- * @author lihzh
- * @date 2012-3-17 下午9:35:00
- */
- public static void setLocale() {
- setLocale(DEFAULT_LOCAL);
- }
- /**
- * 刷新國際化信息
- *
- * @author lihzh
- * @date 2012-3-17 下午9:31:58
- */
- private static void refresh() {
- ObserveCenter.getInstance().notifyChange();
- }
- }
- /**
- * 國際化文件讀取類,提供靜態方法和靜態引用
- *
- * @author lihzh
- * @date 2012-3-17 下午4:07:24
- */
- public class ResourceBundleLoader implements Observer {
- private static final String RESOURCE_CLASSLOADER_PATH_PREFIX = "i18n.i18n_";
- private static String RESOURCE_CLASSLOADER_PATH_FULL;
- private static ResourceBundle resourceBundle;
- /**
- * 讀取國際化文件中定義的值
- *
- * @param key
- * @return
- * @author lihzh
- * @date 2012-3-17 下午4:33:32
- */
- public static String getValue(String key) {
- return resourceBundle.getString(key);
- }
- /*
- * 當Locale環境變量改變時更改resourceBundle實例加載的國際化文件
- */
- @Override
- public void update(Observable o, Object arg) {
- refreshBundle();
- }
- /**
- * 刷新讀取的Bundle信息
- *
- * @author lihzh
- * @date 2012-3-17 下午10:13:26
- */
- private static void refreshBundle() {
- RESOURCE_CLASSLOADER_PATH_FULL = RESOURCE_CLASSLOADER_PATH_PREFIX
- + getLocale().toString();
- resourceBundle = ResourceBundle.getBundle(
- RESOURCE_CLASSLOADER_PATH_FULL, getLocale());
- }
- }
- /**
- * UI控件池,用於保存當前所有的UI控件
- *
- * @author lihzh
- * @date 2012-3-18 下午10:21:14
- */
- public class ComponentPool {
- // 按鈕池
- private static final Map<String, GenericButton> buttonPool = new HashMap<String, GenericButton>();
- /**
- * 註冊按鈕
- *
- * @param button
- * @author lihzh
- * @date 2012-3-18 下午10:26:35
- */
- public static void addButton(GenericButton button) {
- if (button == null) {
- throw new IllegalArgumentException(
- "The registing button can not be null.");
- }
- if (button.getName() == null) {
- throw new IllegalArgumentException(
- "The registing button's name can not be null.");
- }
- buttonPool.put(button.getName(), button);
- }
- /**
- * 獲取按鈕
- *
- * @param buttonName
- * @return
- * @author lihzh
- * @date 2012-3-18 下午10:30:31
- */
- public static GenericButton getButton(String buttonName) {
- return buttonPool.get(buttonName);
- }
- /**
- * 獲取所有的Button
- *
- * @return
- * @author lihzh
- * @date 2012-3-18 下午10:52:50
- */
- public static Map<String, GenericButton> getAllButtons() {
- return buttonPool;
- }
- }
- /**
- * 所有按鈕的基類,可統一處理國際化通用事件, 統一將name設置爲國際化文件中的key值
- *
- * @author lihzh
- * @date 2012-3-18 下午5:15:01
- */
- public class GenericButton extends JButton {
- private static final long serialVersionUID = -5971509297727392232L;
- public GenericButton(String name) {
- super();
- this.setName(name);
- ComponentPool.addButton(this);
- }
- /*
- * 設置名字,同時設置顯示值
- * @see java.awt.Component#setName(java.lang.String)
- *
- * @author lihzh
- * @date 2012-3-19 下午12:34:28
- */
- @Override
- public void setName(String name) {
- super.setName(name);
- setText(ResourceBundleLoader.getValue(name));
- }
- /**
- * 在國際化位置發生改變時執行
- *
- * @author lihzh
- * @date 2012-3-18 下午10:35:54
- */
- public void onLocaleChange() {
- this.setText(ResourceBundleLoader.getValue(this.getName()));
- afterTextChange();
- }
- /**
- * 在國際化位置發生改變時執行,用於子類複寫其特有操作。在文本刷新前執行
- *
- * @author lihzh
- * @date 2012-3-18 下午10:57:06
- */
- protected void beforeTextChange() {
- }
- /**
- * 在國際化位置發生改變時執行,用於子類複寫其特有操作。在文本刷新後執行
- *
- * @author lihzh
- * @date 2012-3-18 下午10:57:06
- */
- protected void afterTextChange() {
- }
- /**
- * UI界面國際化管理類
- *
- * @author lihzh
- * @date 2012-3-18 下午10:09:29
- */
- public class I18nUIHandler implements Observer {
- /*
- * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
- *
- * @author lihzh
- * @date 2012-3-19 下午12:33:27
- */
- @Override
- public void update(Observable o, Object arg) {
- // 刷新按鈕
- refreshButton();
- }
- /**
- * 刷新按鈕
- *
- * @author lihzh
- * @date 2012-3-18 下午10:53:02
- */
- private void refreshButton() {
- Map<String, GenericButton> buttons = ComponentPool.getAllButtons();
- Iterator<Entry<String, GenericButton>> butEntryIt = buttons.entrySet().iterator();
- while (butEntryIt.hasNext()) {
- Entry<String, GenericButton> entry = butEntryIt.next();
- GenericButton button = entry.getValue();
- button.onLocaleChange();
- }
- }
- }
- /**
- * 中文按鈕
- *
- * @author lihzh
- * @date 2012-3-18 下午11:00:46
- */
- public class CNConfirmButton extends GenericButton {
- private static final long serialVersionUID = 1481761153745275594L;
- public CNConfirmButton(String name) {
- super(name);
- setBounds(100, 80, 80, 30);
- addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- LocaleHandler.setLocale(Locale.CHINA);
- }
- });
- }
- }
- /**
- * @author lihzh
- * @date 2012-3-15 下午10:57:16
- */
- public class MainContainer extends JFrame {
- private static final long serialVersionUID = -4379994966214808467L;
- private static final Logger logger = LoggerFactory
- .getLogger(MainContainer.class);
- public MainContainer() {
- logger.info("Begin to initialize the UI container.");
- this.setSize(300, 200);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setResizable(false);
- this.setTitle(getValue(TITLE_MAIN));
- this.getContentPane().setLayout(null);
- JButton usButton = new USConfirmButton(COMBO_LANGUAGE_US);
- JButton cnButton = new CNConfirmButton(COMBO_LANGUAGE_ZH);
- this.add(usButton, null);
- this.add(cnButton, null);
- }
- }