messageformat.html

来自「API資料大全」· HTML 代码 · 共 797 行 · 第 1/3 页

HTML
797
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:36:39 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class  MessageFormat</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/MessageFormat.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup>&nbsp;2&nbsp;Platform<br>Std.&nbsp;Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="../../java/text/Format.html"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../java/text/NumberFormat.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="MessageFormat.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: &nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.text</FONT><BR>Class  MessageFormat</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A>  |  +--<A HREF="../../java/text/Format.html">java.text.Format</A>        |        +--<B>java.text.MessageFormat</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Cloneable.html">Cloneable</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>MessageFormat</B><DT>extends <A HREF="../../java/text/Format.html">Format</A></DL><P><code>MessageFormat</code> provides a means to produce concatenated messages in language-neutral way. Use this to construct messages displayed for end users. <p> <code>MessageFormat</code> takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places. <p> <strong>Note:</strong> <code>MessageFormat</code> differs from the other <code>Format</code> classes in that you create a <code>MessageFormat</code> object with one of its constructors (not with a <code>getInstance</code> style factory method). The factory methods aren't necessary because <code>MessageFormat</code> doesn't require any complex setup for a given locale. In fact, <code>MessageFormat</code> doesn't implement any locale specific behavior at all. It just needs to be set up on a sentence by sentence basis. <p> Here are some examples of usage: <blockquote> <pre> 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); <em>output</em>: At 12:30 PM on Jul 3, 2053, there was a disturbance           in the Force on planet 7. </pre> </blockquote> Typically, the message format will come from resources, and the arguments will be dynamically set at runtime. <p> Example 2: <blockquote> <pre> 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 <em>output</em>: The disk "MyDisk" contains 0 file(s). <em>output</em>: The disk "MyDisk" contains 1 file(s). <em>output</em>: The disk "MyDisk" contains 1,273 file(s). </pre> </blockquote> <p> The pattern is of the form: <blockquote> <pre> 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 </pre> </blockquote> If there is no <code>elementFormat</code>, then the argument must be a string, which is substituted. If there is no <code>dateTimeStyle</code> or <code>numberStyle</code>, then the default format is used (for example, <code>NumberFormat.getInstance</code>, <code>DateFormat.getTimeInstance</code>, or <code>DateFormat.getInstance</code>). <p> In strings, single quotes can be used to quote the "{" (curly brace) if necessary. A real single quote is represented by ''. Inside a <code>messageFormatElement</code>, quotes are <strong>not</strong> removed. For example, {1,number,$'#',##} will produce a number format with the pound-sign quoted, with a result such as: "$#31,45". <p> If a pattern is used, then unquoted braces in the pattern, if any, must match: that is, "ab {0} de" and "ab '}' de" are ok, but "ab {0'}' de" and "ab } de" are not. <p> The argument is a number from 0 to 9, which corresponds to the arguments presented in an array to be formatted. <p> It is ok to have unused arguments in the array. With missing arguments or arguments that are not of the right class for the specified format, a <code>ParseException</code> is thrown. First, <code>format</code> checks to see if a <code>Format</code> object has been specified for the argument with the <code>setFormats</code> method. If so, then <code>format</code> uses that <code>Format</code> object to format the argument. Otherwise, the argument is formatted based on the object's type. If the argument is a <code>Number</code>, then <code>format</code> uses <code>NumberFormat.getInstance</code> to format the argument; if the argument is a <code>Date</code>, then <code>format</code> uses <code>DateFormat.getDateTimeInstance</code> to format the argument. Otherwise, it uses the <code>toString</code> method. <p> For more sophisticated patterns, you can use a <code>ChoiceFormat</code> to get output such as: <blockquote> <pre> 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. </pre> </blockquote> You can either do this programmatically, as in the above example, or by using a pattern (see <A HREF="../../java/text/ChoiceFormat.html"><CODE>ChoiceFormat</CODE></A> for more information) as in: <blockquote> <pre> form.applyPattern(    "There {0,choice,0#are no files|1#is one file|1#are {0,number,integer} files}."); </pre> </blockquote> <p> <strong>Note:</strong> As we see above, the string produced by a <code>ChoiceFormat</code> in <code>MessageFormat</code> is treated specially; occurances of '{' are used to indicated subformats, and cause recursion. If you create both a <code>MessageFormat</code> and <code>ChoiceFormat</code> programmatically (instead of using the string patterns), then be careful not to produce a format that recurses on itself, which will cause an infinite loop. <p> <strong>Note:</strong> formats are numbered by order of variable in the string. This is <strong>not</strong> the same as the argument numbering! For example: with "abc{2}def{3}ghi{0}...", <ul> <li>format0 affects the first variable {2} <li>format1 affects the second variable {3} <li>format2 affects the second variable {0} <li>and so on. </ul> <p> When a single argument is parsed more than once in the string, the last match will be the final result of the parsing.  For example, <pre> MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}"); Object[] objs = {new Double(3.1415)}; String result = mf.format( objs ); // result now equals "3.14, 3.1" objs = null; objs = mf.parse(result, new ParsePosition(0)); // objs now equals {new Double(3.1)} </pre> <p> Likewise, parsing with a MessageFormat object using patterns containing multiple occurances of the same argument would return the last match.  For example, <pre> MessageFormat mf = new MessageFormat("{0}, {0}, {0}"); String forParsing = "x, y, z"; Object[] objs = mf.parse(forParsing, new ParsePosition(0)); // result now equals {new String("z")}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?