📄 joptionpane.html
字号:
<!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:39:01 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class JOptionPane</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> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/JOptionPane.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../javax/swing/JMenuItem.AccessibleJMenuItem.html"><B>PREV CLASS</B></A> <A HREF="../../javax/swing/JOptionPane.AccessibleJOptionPane.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> <A HREF="JOptionPane.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: <A HREF="#inner_class_summary">INNER</A> | <A HREF="#field_summary">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: <A HREF="#field_detail">FIELD</A> | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">javax.swing</FONT><BR>Class JOptionPane</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<A HREF="../../java/awt/Component.html">java.awt.Component</A> | +--<A HREF="../../java/awt/Container.html">java.awt.Container</A> | +--<A HREF="../../javax/swing/JComponent.html">javax.swing.JComponent</A> | +--<B>javax.swing.JOptionPane</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../javax/accessibility/Accessible.html">Accessible</A>, <A HREF="../../java/awt/image/ImageObserver.html">ImageObserver</A>, <A HREF="../../java/awt/MenuContainer.html">MenuContainer</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>JOptionPane</B><DT>extends <A HREF="../../javax/swing/JComponent.html">JComponent</A><DT>implements <A HREF="../../javax/accessibility/Accessible.html">Accessible</A></DL><P><code>JOptionPane</code> makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. For information about using <code>JOptionPane</code>, see <ahref="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>, a section in <em>The Java Tutorial</em>. <p> While the <code>JOptionPane</code> class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static <code>showXxxDialog</code> methods shown below: <blockquote> <table> <tr align=top><td>showConfirmDialog<td>Asks a confirming question, like yes/no/cancel. <tr align=top><td>showInputDialog<td>Prompt for some input. <tr align=top><td>showMessageDialog<td>Tell the user about something that has happened. <tr align=top><td>showOptionDialog<td>The Grand Unification of the above three. </table> </blockquote> Each of these methods also comes in a <code>showInternalXXX</code> flavor, which uses an internal frame to hold the dialog box (see <A HREF="../../javax/swing/JInternalFrame.html"><CODE>JInternalFrame</CODE></A>). Multiple convenience methods have also been defined -- overloaded versions of the basic methods that use different parameter lists. <p> All dialogs are modal. Each <code>showXxxDialog</code> method blocks the current thread until the user's interaction is complete. <p> <table cellspacing=6 cellpadding=4 border=0 align=right> <tr> <td bgcolor=#FFe0d0 rowspan=2> icon <td bgcolor=#FFe0d0> message <tr> <td bgcolor=#FFe0d0> input value <tr> <td bgcolor=#FFe0d0 colspan=2> option buttons </table> The basic appearance of one of these dialog boxes is generally similar to the picture at the right, although the various look-and-feels are ultimatly responsible for the final result. <br clear=all> <p> <b>Parameters:</b><br> The parameters to these methods follow consistent patterns: <blockquote> <dl compact> <dt>parentComponent<dd> Defines the <code>Component</code> that is to be the parent of this dialog box. It is used in two ways: the <code>Frame</code> that contains it is used as the <code>Frame</code> parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be <code>null</code>, in which case a default <code>Frame</code> is used as the parent, and the dialog will be centered on the screen (depending on the L&F). <dt><a name=message>message</a><dd> A descriptive message to be placed in the dialog box. In the most common usage, message is just a <code>String</code> or <code>String</code> constant. However, the type of this parameter is actually <code>Object</code>. Its interpretation depends on its type: <dl compact> <dt>Object[]<dd>An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type. <dt>Component<dd>The <code>Component</code> is displayed in the dialog. <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code> and displayed in the dialog. <dt>others<dd>The object is converted to a <code>String</code> by calling its <code>toString</code> method. The result is wrapped in a <code>JLabel</code> and displayed. </dl> <dt>messageType<dd>Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are: <ul> <li>ERROR_MESSAGE <li>INFORMATION_MESSAGE <li>WARNING_MESSAGE <li>QUESTION_MESSAGE <li>PLAIN_MESSAGE </ul> <dt>optionType<dd>Defines the set of option buttons that appear at the bottom of the dialog box: <ul> <li>DEFAULT_OPTION <li>YES_NO_OPTION <li>YES_NO_CANCEL_OPTION <li>OK_CANCEL_OPTION </ul> You aren't limited to this set of option buttons. You can provide any buttons you want using the options parameter. <dt>options<dd>A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of <code>String</code>s. But the parameter type is an array of <code>Objects</code>. A button is created for each object depending on its type: <dl compact> <dt>Component<dd>The component is added to the button row directly. <dt>Icon<dd>A <code>JButton</code> is created with this as its label. <dt>other<dd>The <code>Object</code> is converted to a string using its <code>toString</code> method and the result is used to label a <code>JButton</code>. </dl> <dt>icon<dd>A decorative icon to be placed in the dialog box. A default value for this is determined by the <code>messageType</code> parameter. <dt>title<dd>The title for the dialog box. <dt>initialValue<dd>The default selection (input value). </dl> </blockquote> <p> When the selection is changed, <code>setValue</code> is invoked, which generates a <code>PropertyChangeEvent</code>. <p> If a <code>JOptionPane</code> has configured to all input <code>setWantsInput</code> the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code> can also be listened to, to determine when the user has input or selected a value. <p> When one of the <code>showXxxDialog</code> methods returns an integer, the possible values are:<pre> YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, or CLOSED_OPTION. </pre> <b>Examples:</b> <dl> <dt>Show an error dialog that displays the message, 'alert': <dd><code> JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); </code><p> <dt>Show an internal information dialog with the message, 'information': <dd><code> JOptionPane.showInternalMessageDialog(frame, "information",<br> <ul><ul>"information", JOptionPane.INFORMATION_MESSAGE);</ul></ul> </code><p> <dt>Show an information panel with the options yes/no and message 'choose one': <dd><code>JOptionPane.showConfirmDialog(null, <ul><ul>"choose one", "choose one", JOptionPane.YES_NO_OPTION);</ul></ul> </code><p> <dt>Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and title information: <dd><code>JOptionPane.showInternalConfirmDialog(frame, <ul><ul>"please choose one", "information",</ul></ul> <ul><ul>JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);</ul></ul> </code><p> <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue': <dd><code> Object[] options = { "OK", "CANCEL" };<br> JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", <ul><ul>JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,</ul></ul> <ul><ul>null, options, options[0]);</ul></ul> </code><p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -