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(0, 0).setAnchor(GBC.EAST)); add(p, new GBC(1, 1)); add(parseButton, new GBC(0, 2).setInsets(2)); add(localeCombo, new GBC(1, 0).setAnchor(GBC.WEST)); add(numberText, new GBC(1, 2).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 GridBagLayoutCopyright (C) 2002 Cay S. Horstmann (http://horstmann.com)This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, 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,顺便学习一下英语。
那些无法讲述的悲伤和苍凉,