<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    何以解憂?唯有Java

    其實程序員也懂得浪漫!
    posts - 90, comments - 637, trackbacks - 0, articles - 2
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    NetBeans國際化功能(二)

    Posted on 2008-04-18 15:52 guanminglin@gmail.com 閱讀(2401) 評論(2)  編輯  收藏 所屬分類: NetBeans
        Java編程語言是第一個被設(shè)計成為全面支持國際化的語言,從一開始,他就具備進(jìn)行有效的國際化所必須的一個特性:使用Unicode來處理所有字符串。支持Unicode使得在Java編程語言下,編寫程序來操作多種語言的字符串變得非常的方便。如今NetBeans的出現(xiàn)再次幫我們減輕了國際化的工作量真是太棒了!
        好了回歸正題,接著上次的國際化話題,上次只是演示了NetBeans中一些國際化功能,這次講講其它的功能,首先看看下面的幾幅圖







        大家可以看到在不同的DesignLocale里可以有不同的視圖這樣很方便,你可以在自己熟悉的語言環(huán)境下先寫好自己的程序,然后在進(jìn)行國際化,前提是你要先添加自己的locale,或者添加你想要國際化為另一種的語言具體可以看我的上一篇文章NetBeans國際化功能(一)

        還有就是如果你只是想讓自己的應(yīng)用程序只顯示一種語言可以不用改操作系統(tǒng)的語言,,在main的第一句加個Locale.setDefault("這里填寫你想要的語言");以后程序運行的就是你設(shè)置的語言了(本人不建議這樣做)。看下面的截圖,可以有好多的選擇


        國際化,并不是僅僅將界面進(jìn)行翻譯一下就可以了,還要考慮到時間的國際化,以及數(shù)值表的國際化等等。下圖顯示了國際化的一些選項,你可以關(guān)閉或者打開國際化功能。



    今天先講數(shù)值的國際化:
    關(guān)于不同地區(qū)的數(shù)值表示的國際化請看下面的代碼

    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.*;
    import javax.swing.*;

    /**
     *
     * 
    @author Administrator
     
    */
    public class NumberFormatTest {

        
    /**
         * 
    @param args the command line arguments
         
    */
        
    public static void main(String[] args) {
            
    // TODO code application logic here
            JFrame frame = new NumberFormatFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(
    true);
        }
    }

    class NumberFormatFrame extends JFrame {

        
    public NumberFormatFrame() {
            setTitle(
    "NumberFormatTest");
            setLayout(
    new GridBagLayout());

            //定義一個事件監(jiān)聽器
            ActionListener listener 
    = new ActionListener() {

                
    public void actionPerformed(ActionEvent e) {
                    updateDisplay();
                }
            };

            JPanel p 
    = new JPanel();
            addRadioButton(p, numberRadioButton, rbGroup, listener);
            addRadioButton(p, currencyRadioButton, rbGroup, listener);
            addRadioButton(p, percentRadioButton, rbGroup, listener);
            add(
    new JLabel("Local:"), new GBC(00).setAnchor(GBC.EAST));
            add(p, 
    new GBC(11));
            add(parseButton, 
    new GBC(02).setInsets(2));
            add(localeCombo, 
    new GBC(10).setAnchor(GBC.WEST));
            add(numberText, 
    new GBC(12).setFill(GBC.HORIZONTAL));

           //取得可以使用的locale,比顯示它們的名字
            locales 
    = NumberFormat.getAvailableLocales();
            
    for(Locale loc : locales)
                 localeCombo.addItem(loc.getDisplayName());  
           
            localeCombo.setSelectedItem(Locale.getDefault().getDisplayName());
            currentNumber 
    = 123456.78;
            updateDisplay();
            localeCombo.addActionListener(listener);
            parseButton.addActionListener(
    new ActionListener() {

                
    public void actionPerformed(ActionEvent event) {
                    String s 
    = numberText.getText().trim();
                    
    try {
                        Number n 
    = currentNumberFormat.parse(s);
                        
    if (n != null) {
                            currentNumber 
    = n.doubleValue();
                            updateDisplay();
                        } 
    else {
                            numberText.setText(
    "Parse error" + s);
                        }

                    } 
    catch (ParseException e) {
                        numberText.setText(
    "Parse error" + s);
                    }
                }
            });
            pack();

        }
        
        
    private void addRadioButton(Container p,JRadioButton b,ButtonGroup g,ActionListener listener) {
            b.setSelected(g.getButtonCount()
    ==0);
            b.addActionListener(listener);
            g.add(b);
            p.add(b);
        }
      
        //更新視圖
        
    public void updateDisplay(){
            Locale currentLocale 
    = locales[localeCombo.getSelectedIndex()];
            currentNumberFormat 
    =null;
            
    if(numberRadioButton.isSelected())
                currentNumberFormat 
    = NumberFormat.getNumberInstance(currentLocale);
            
    else if(currencyRadioButton.isSelected())
                currentNumberFormat 
    = NumberFormat.getCurrencyInstance(currentLocale);
            
    else if(percentRadioButton.isSelected())
                currentNumberFormat 
    = NumberFormat.getPercentInstance(currentLocale);
            String n 
    = currentNumberFormat.format(currentNumber);
            numberText.setText(n);
        }
        
        
    private Locale[] locales;
        
    private double currentNumber;
        
    private JComboBox localeCombo = new JComboBox();
        
    private JButton parseButton = new JButton("Parse");
        
    private JTextField numberText = new JTextField(30);
        
    private JRadioButton numberRadioButton = new JRadioButton("Number");
        
    private JRadioButton currencyRadioButton = new JRadioButton("Current");
        
    private JRadioButton percentRadioButton = new JRadioButton("Percent");
        
    private ButtonGroup rbGroup = new ButtonGroup();
        
    private NumberFormat currentNumberFormat;

       
    }

    還有個用到的GBC.java代碼

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     
    */

    package testapp.Local;

    /*
    GBC - A convenience class to tame the GridBagLayout

    Copyright (C) 2002 Cay S. Horstmann (
    http://horstmann.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */

    import java.awt.*;

    /**
       This class simplifies the use of the GridBagConstraints
       class.
    */
    public class GBC extends GridBagConstraints
    {
       
    /**
          Constructs a GBC with a given gridx and gridy position and
          all other grid bag constraint values set to the default.
          
    @param gridx the gridx position
          
    @param gridy the gridy position
       
    */
       
    public GBC(int gridx, int gridy)
       {
          
    this.gridx = gridx;
          
    this.gridy = gridy;
       }

       
    /**
          Constructs a GBC with given gridx, gridy, gridwidth, gridheight
          and all other grid bag constraint values set to the default.
          
    @param gridx the gridx position
          
    @param gridy the gridy position
          
    @param gridwidth the cell span in x-direction
          
    @param gridheight the cell span in y-direction
       
    */
       
    public GBC(int gridx, int gridy, int gridwidth, int gridheight)
       {
          
    this.gridx = gridx;
          
    this.gridy = gridy;
          
    this.gridwidth = gridwidth;
          
    this.gridheight = gridheight;
       }

       
    /**
          Sets the anchor.
          
    @param anchor the anchor value
          
    @return this object for further modification
       
    */
       
    public GBC setAnchor(int anchor)
       {
          
    this.anchor = anchor;
          
    return this;
       }
       
       
    /**
          Sets the fill direction.
          
    @param fill the fill direction
          
    @return this object for further modification
       
    */
       
    public GBC setFill(int fill)
       {
          
    this.fill = fill;
          
    return this;
       }

       
    /**
          Sets the cell weights.
          
    @param weightx the cell weight in x-direction
          
    @param weighty the cell weight in y-direction
          
    @return this object for further modification
       
    */
       
    public GBC setWeight(double weightx, double weighty)
       {
          
    this.weightx = weightx;
          
    this.weighty = weighty;
          
    return this;
       }

       
    /**
          Sets the insets of this cell.
          
    @param distance the spacing to use in all directions
          
    @return this object for further modification
       
    */
       
    public GBC setInsets(int distance)
       {
          
    this.insets = new Insets(distance, distance, distance, distance);
          
    return this;
       }

       
    /**
          Sets the insets of this cell.
          
    @param top the spacing to use on top
          
    @param left the spacing to use to the left
          
    @param bottom the spacing to use on the bottom
          
    @param right the spacing to use to the right
          
    @return this object for further modification
       
    */
       
    public GBC setInsets(int top, int left, int bottom, int right)
       {
          
    this.insets = new Insets(top, left, bottom, right);
          
    return this;
       }

       
    /**
          Sets the internal padding
          
    @param ipadx the internal padding in x-direction

          
    @param ipady the internal padding in y-direction
          
    @return this object for further modification
       
    */
       
    public GBC setIpad(int ipadx, int ipady)
       {
          
    this.ipadx = ipadx;
          
    this.ipady = ipady;
          
    return this;
       }
    }

    關(guān)于日期和時間的國際化下次有空再寫了,最近想翻譯SwingApplicationFramework的API,順便學(xué)習(xí)一下英語。

    評論

    # re: NetBeans國際化功能(二)  回復(fù)  更多評論   

    2008-04-18 16:00 by 簡陽
    API重要哦`

    # re: NetBeans國際化功能(二)  回復(fù)  更多評論   

    2008-04-18 16:55 by 王能
    今天SEO大賽關(guān)于西藏的問題,大家說說有沒有什么想法啊?
    關(guān)于西藏的問題,可以參考 http://www.bt285.cn/tibetisxizang 這里
    主站蜘蛛池模板: 亚洲AV无码一区二区乱子伦| 亚洲第一成年网站视频| 免费jjzz在线播放国产| 最近免费中文字幕大全免费| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 国产在线观看免费完整版中文版| 亚洲av无码一区二区三区乱子伦| 久久综合给合久久国产免费| 亚洲精品韩国美女在线| 亚洲日韩精品无码专区网址| 免费看小12萝裸体视频国产| 成人免费在线视频| 最近高清中文字幕免费| 色欲A∨无码蜜臀AV免费播| 久久九九久精品国产免费直播| 国产亚洲综合视频| 亚洲色大成网站www久久九| 亚洲人成电影网站| 亚洲第一二三四区| 亚洲AV日韩AV永久无码绿巨人| 亚洲国产另类久久久精品黑人| 亚洲视频人成在线播放| 亚洲成av人片在线观看天堂无码 | 亚洲黄色片免费看| 亚洲人成网站影音先锋播放| 亚洲精品国产美女久久久| 国产日产亚洲系列最新| 亚洲精品无码久久久久AV麻豆| 国产一级一片免费播放i| 国产免费av一区二区三区| 国产精品免费播放| 免费一看一级毛片人| 亚洲成a人无码av波多野按摩| 亚洲av无码国产精品色在线看不卡 | 天天干在线免费视频| 最近免费中文字幕大全视频| 久久综合AV免费观看| 香蕉视频在线观看免费国产婷婷| 国内免费高清在线观看| 卡一卡二卡三在线入口免费| 日本a级片免费看|