何以解憂?唯有Java

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

          NetBeans國際化功能(二)

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







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

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


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



          今天先講數值的國際化:
          關于不同地區的數值表示的國際化請看下面的代碼

          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());

                  //定義一個事件監聽器
                  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;
             }
          }

          關于日期和時間的國際化下次有空再寫了,最近想翻譯SwingApplicationFramework的API,順便學習一下英語。

          評論

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

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

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

          2008-04-18 16:55 by 王能
          今天SEO大賽關于西藏的問題,大家說說有沒有什么想法啊?
          關于西藏的問題,可以參考 http://www.bt285.cn/tibetisxizang 這里
          主站蜘蛛池模板: 道孚县| 青田县| 靖江市| 莱州市| 子长县| 望谟县| 清涧县| 青铜峡市| 斗六市| 克拉玛依市| 那曲县| 惠东县| 广昌县| 依兰县| 外汇| 宜章县| 北京市| 天长市| 墨竹工卡县| 荥阳市| 温州市| 娄底市| 通海县| 阿勒泰市| 金坛市| 临武县| 德格县| 永德县| 镇巴县| 阿合奇县| 霍邱县| 嘉鱼县| 婺源县| 寻乌县| 响水县| 盐山县| 永善县| 济源市| 江华| 蒲江县| 盘山县|