swing 国际化

查阅了很多资料,最简单的需求是当用户登录的时候选择语言,这个就不说了。

比较变态的需求是,当用户已经登录到某个界面的时候突然想到要修改语言了(的确够变态 哈哈), 查阅了很多资料,找到一个不错的方法。

该方法使用了观察着模式,就是给每一个面板添加监听,当用户修改语言的时候,触发所有已存在的面板的监听程序,将对应面板上的语言全部修改成新的语言版本。

简单步骤如下:
1,定义监听接口

/**
 * 
 */
package com.rodrigue.nepo.multilanguage;

/**
 * @author LEO.ZHOU
 * DATE Nov 29, 2009
 */
public interface LangChangeListener {
         public void updateResource();
}

 

2,写一个用于管理的类

/**
 * 
 */
package com.rodrigue.nepo.multilanguage;

import java.util.LinkedList;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author LEO.ZHOU
 * DATE Nov 29, 2009
 */
public class ResourceManager {
        
        private LinkedList<LangChangeListener> listeners = new LinkedList<LangChangeListener>();
        private ResourceBundle bundle;
        private Locale locale;
        private boolean changed = false;
        
        // ResourceBundle file
        private static final String RESOURCE_NAME = "nepo";

        // singleTon
        private static ResourceManager manager;

        public static ResourceManager getInstance() {
                if(manager == null) {
                        manager = new ResourceManager();
                }
                return manager;
        }

        public void changeLocal(Locale newLocal) {
                if(newLocal != null) {
                        locale = newLocal;
                        changed = true;
                }
        }
        
        // get value by key
        @SuppressWarnings("finally")
        public String getString(String key) {
                String value = "";
//                try {
                        value = getBundle().getString(key);
//                } catch (MissingResourceException e) {
//                        value = key;
//                        throw e;
//                } finally {
                        return value;  
//                }
        }
        
        /**
         * get bundle
         * 
         * @return
         */
        private ResourceBundle getBundle() {
                if (locale == null) {
                        locale = new Locale("en", "US");
                        changed = true;
                }
                if (bundle == null) {
                        bundle = ResourceBundle.getBundle(RESOURCE_NAME, locale);
                } else {
                        if(changed) {
                                bundle = ResourceBundle.getBundle(RESOURCE_NAME, locale);
                                changed = false;
                        }
                }
                return bundle;
        }
        
        public void addListener(LangChangeListener lsn) {
                listeners.add(lsn);
        } 
        
        //Invoke listener
        public void languageChangedEvent(Locale locale) {
                changeLocal(locale); 
                for(LangChangeListener listener : listeners) {
                        if(listener != null) {
                                listener.updateResource();
                        }
                }
        }
        

}

 

3,定义一个实现了语言监听接口的抽象类,该类继承jpanel类(以后所有的自定义 类都要继承该抽象类)

package com.rodrigue.nepo.multilanguage;

import java.awt.LayoutManager;

import javax.swing.JPanel;

public abstract class JPanelMultiLanguageNepo extends JPanel implements LangChangeListener{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        /**
     * Creates a new JPanel with the specified layout manager and buffering
     * strategy.
     *
     * @param layout  the LayoutManager to use
     * @param isDoubleBuffered  a boolean, true for double-buffering, which
     *        uses additional memory space to achieve fast, flicker-free 
     *        updates
     */
    public JPanelMultiLanguageNepo(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        customConstruct(layout, isDoubleBuffered);
        
    }

    /**
     * Create a new buffered JPanel with the specified layout manager
     *
     * @param layout  the LayoutManager to use
     */
    public JPanelMultiLanguageNepo(LayoutManager layout) {
            super(layout);
            customConstruct(layout);
    }

    /**
     * Creates a new <code>JPanel</code> with <code>FlowLayout</code>
     * and the specified buffering strategy.
     * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code>
     * will use a double buffer.
     *
     * @param isDoubleBuffered  a boolean, true for double-buffering, which
     *        uses additional memory space to achieve fast, flicker-free 
     *        updates
     */
    public JPanelMultiLanguageNepo(boolean isDoubleBuffered) {
            super(isDoubleBuffered);
            customConstruct(isDoubleBuffered);
    }

    /**
     * Creates a new <code>JPanel</code> with a double buffer
     * and a flow layout.
     */
    public JPanelMultiLanguageNepo() {
            super();
            customConstruct();
    }
    
    public JPanelMultiLanguageNepo(Object... objs) {
            super();
            customConstruct(objs);
    }
    
    //-----------------------------------MULTI LANGUAGE-------------------START
    private void customConstruct(Object... objs) {
            ResourceManager.getInstance().addListener(this);
            iniCurrentComponents(objs);
            updateResource();
    }
    
    protected String getResource(String key) {
            return ResourceManager.getInstance().getString(key);
        }  
    
    public abstract void iniCurrentComponents(Object... objs);
    public abstract void updateResource();
    //-----------------------------------MULTI LANGUAGE-------------------END

}

 

4,每当你新建一个面板的时候都需要继承上面的抽象类

package com.rodrigue.nepo.ui.template.component;
/**
 * @author LEO.ZHOU
 * DATE Nov 20, 2009
 */
public class JPopupPanel extends JPanelMultiLanguageNepo {
        
        private String titleKey;
        private JLabel titleLabel;

        public JPopupPanel(String titleKey) {
                super(titleKey);
        }
        
        //这里用来定义组件
        @Override
        public void iniCurrentComponents(Object... objs) {
                this.titleKey = (String) objs[0];
                iniComponents();
                iniLayout();
                addActions();
        }
        
        //所有需要国际化的text,tooltip text等在这里设置
        @Override
        public void updateResource() {
                titleLabel.setText(getResource(titleKey));
                
        }
}

 

5,当用户修改某个语言的时候调用一下方法

ResourceManager.getInstance().languageChangedEvent(new Locale("zh", "CN"));
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值