当前位置 类层次 (JDK) 所有类 (JDK) 所有域和方法 (JDK)

类 java.text.MessageFormat

java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.MessageFormat

public class MessageFormat
extends Format

MessageFormat 提供了一种用中性语言方式产生连接信息的方法。 用该类构造显示给最终用户的信息。

MessageFormat 产生一系列对象,格式化它们,然后把格式化的字符串插入模式中恰当的地方。

注意: MessageFormat 与其它 Format 类不同,用对象的构造子创建一个 MessageFormat 对象 ( 而不是用一个 getInstance 风格的工厂方法 )。 不需要这些工厂方法,因为 MessageFormat 不要求为给定的语言环境进行复杂的设置。 实际上,MessageFormat 根本没有实现任何语言环境特定的行为。 它只需要以句子为基础在句子上建立。

下面是一些用法示例:

 Object[] arguments = {
     new Integer(7),
     new Date(System.currentTimeMillis()),
     "a disturbance in the Force"
 };
 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     arguments);
 : At 12:30 PM on Jul 3, 2053, there was a disturbance
           in the Force on planet 7.
 
通常,消息的格式来自于资源,且参数将在运行期间动态的设置。

示例 2:

 Object[] testArgs = {new Long(3), "MyDisk"};
 MessageFormat form = new MessageFormat(
     "The disk \"{1}\" contains {0} file(s).");
 System.out.println(form.format(testArgs));
 // output, with different testArgs
 : The disk "MyDisk" contains 0 file(s).
 : The disk "MyDisk" contains 1 file(s).
 : The disk "MyDisk" contains 1,273 file(s).
 

模式的形式是:

 messageFormatPattern := string ( "{" messageFormatElement "}" string )*
 messageFormatElement := argument { "," elementFormat }
 elementFormat := "time" { "," datetimeStyle }
                | "date" { "," datetimeStyle }
                | "number" { "," numberStyle }
                | "choice" { "," choiceStyle }
 datetimeStyle := "short"
                  | "medium"
                  | "long"
                  | "full"
                  | dateFormatPattern
 numberStyle := "currency"
               | "percent"
               | "integer"
               | numberFormatPattern
 choiceStyle := choiceFormatPattern
 
如果没有 elementFormat,那么参数必须用一个字符串代替。 如果没有 dateTimeStylenumberStyle,就使用缺省格式 ( 例如,NumberFormat.getInstanceDateFormat.getTimeInstanceDateFormat.getInstance)。

必要的话,在字符串中的单引号可用来引起 "{"( 花括号 )。 一个真正的单引号用 '' 表示。 在 messageFormatElement 中,引号被删除。 例如,{1,number,$'#',##} 将产生一个含有被引起的磅符号的数字格式,结果是诸如 "$#31,45" 的形式。

如果使用了一个模式,模式中如果有未括起的花括号,它们必须匹配:即 "ab {0} de" 和 "ab '}' de" 是合法的,但是 "ab {0'}' de" and "ab } de" 是非法的。

参数是 0 到 9 之间的数字,它相应与格式化的数组中的参数。

数组中可以含有未用的参数。 但如果缺少参数或参数不是特定格式要求的正确的类的对象,将抛出一个 ParseException。 首先, format 检验是否用 setFormats 方法为参数指定了一个 Format 对象。 如果指定了,那么 format 使用这个 Format 对象格式化该参数。 否则根据对象的类型格式化参数。 如果参数是一个 Number,那么 formatNumberFormat.getInstance 格式化该参数;如果参数是一个 Date,那么 formatDateFormat.getDateTimeInstance 格式化该参数。 否则它使用 toString 方法。

对于更复杂的模式,可用 ChoiceFormat 输出,如:

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
 double[] filelimits = {0,1,2};
 String[] filepart = {"no files","one file","{0,number} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 form.setFormat(1,fileform); // NOT zero, see below
 Object[] testArgs = {new Long(12373), "MyDisk"};
 System.out.println(form.format(testArgs));
 // output, with different testArgs
 output: The disk "MyDisk" contains no files.
 output: The disk "MyDisk" contains one file.
 output: The disk "MyDisk" contains 1,273 files.
 
可以象上面示例中那样用程序完成格式化,也可以用一个模式 ( 有关的更多信息请参阅 ChoiceFormat) ,如:
 form.applyPattern(
    "There {0,choice,0#are no files|1#is one file|1#are {0,number,integer} files}.");
 

注意: 就象上面看到的那样,由 MessageFormat 中的 ChoiceFormat 产生的字符串将被特别对待;'{' 用于指示子格式并将产生递归。 如果用程序创建了 MessageFormatChoiceFormat ( 而不是用字符串模式 ),那么注意不要产生一个在自身上递归的格式,这将引起无限循环。

注意: 格式按照字符串中变量的顺序编号。这与参数编号不同。 例如:"abc{2}def{3}ghi{0}...",

可以在 applyPattern 之后使用 setLocale ( 可能再调用 setFormat) 以不同的语言环境重新初始化 MessageFormat

参见:
Locale, Format, NumberFormat, DecimalFormat, ChoiceFormat

构造子索引

MessageFormat(String)
用指定的模式构造。

方法索引

applyPattern(String)
设置模式。
clone()
覆盖 Cloneable
equals(Object)
比较两个消息格式对象是否相等。
format(Object, StringBuffer, FieldPosition)
格式化一个对象以产生一个字符串。
format(Object[], StringBuffer, FieldPosition)
返回格式化的对象的模式。
format(String, Object[])
方便的例程。
getFormats()
获得 setFormats 设置的格式。
getLocale()
获得该语言环境。
hashCode()
为消息格式对象生成一个散列码。
parse(String)
分析字符串。
parse(String, ParsePosition)
分析字符串。
parseObject(String, ParsePosition)
分析字符串。
setFormat(int, Format)
分别设置各参数使用的格式。
setFormats(Format[])
设置参数使用的格式。
setLocale(Locale)
用指定的模式和该模式下的参数格式构造。
toPattern()
获得模式。

构造子

MessageFormat
 public MessageFormat(String pattern)
用指定的模式构造。

参见:
applyPattern

方法

setLocale
 public void setLocale(Locale theLocale)
用指定的模式和该模式下的参数格式构造。

参见:
setPattern
getLocale
 public Locale getLocale()
获得该语言环境。 该语言环境用于获取缺省的数值或日期格式信息。

applyPattern
 public void applyPattern(String newPattern)
设置模式。 参见类说明。

toPattern
 public String toPattern()
获得模式。 参见类说明。

setFormats
 public void setFormats(Format newFormats[])
设置参数使用的格式。 关于格式编号请参见类说明。

setFormat
 public void setFormat(int variable,
                       Format newFormat)
分别设置各参数使用的格式。 关于格式编号请参见类说明。

getFormats
 public Format[] getFormats()
获得 setFormats 设置的格式。 关于格式编号请参见类说明。

format
 public final StringBuffer format(Object source[],
                                  StringBuffer result,
                                  FieldPosition ignore)
返回格式化的对象的模式。

参数:
source - 要格式化和替换的对象数组。
result - 文本被添加的位置。
ignore - 不返回可用的状态。
format
 public static String format(String pattern,
                             Object arguments[])
便捷例程。 避免显式地创建 MessageFormat,但不允许将来对它进行优化。

format
 public final StringBuffer format(Object source,
                                  StringBuffer result,
                                  FieldPosition ignore)
格式化一个对象以产生一个字符串。

覆盖:
Format 中的 format
parse
 public Object[] parse(String source,
                       ParsePosition status)
分析字符串。

警告:在许多环境下分列可能失败。 例如:

parse
 public Object[] parse(String source) throws ParseException
分析字符串。 还没有处理递归 ( 被替换的字符串包含 {n} 参量。)

抛出: ParseException
如果字符串不能被分析。
parseObject
 public Object parseObject(String text,
                           ParsePosition status)
分析字符串。 还没有处理递归 ( 被替换的字符串包含 %n 参量。)

覆盖:
Format 中的 parseObject
clone
 public Object clone()
覆盖 Cloneable

覆盖:
Format 中的 clone
equals
 public boolean equals(Object obj)
比较两个消息格式对象是否相等。

覆盖:
Object 中的 equals
hashCode
 public int hashCode()
为消息格式对象生成一个散列码。

覆盖:
Object 中的 hashCode

当前位置 类层次 (JDK) 所有类 (JDK) 所有域和方法 (JDK)