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 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,順便學習一下英語。