📄 dialog.html
字号:
JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");</pre></blockquote>The rest of this section covers the following topics:<ul><li> <a href="#overview">An Overview of Dialogs</a><li> <a href="#dialogdemo">The DialogDemo Example</a><li> <a href="#features">JOptionPane Features</a><li> <a href="#create">Creating and Showing Simple Dialogs</a><li> <a href="#button">Customizing Button Text</a><li> <a href="#input">Getting the User's Input from a Dialog</a><li> <a href="#stayup">Stopping Automatic Dialog Closing</a><li> <a href="#api">The Dialog API</a><li> <a href="#eg">Examples that Use Dialogs</a></ul></blockquote><h3><a name="overview">An Overview of Dialogs</a></h3><blockquote>Every dialog is dependent on a frame.When that frame is destroyed,so are its dependent dialogs.When the frame is iconified,its dependent dialogs disappear from the screen.When the frame is deiconified,its dependent dialogs return to the screen.The AWT automatically provides this behavior.<p>A dialog can be <em>modal</em>.When a modal dialog is visible,it blocks user input to all other windowsin the program.The <code>JDialog</code>s that <code>JOptionPane</code> creates are modal.To create a non-modal dialog, you must use the <code>JDialog</code> class directly.<p>The <code>JDialog</code> class is a subclass of the AWT<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Dialog.html"><code>java.awt.Dialog</code></a> class.It adds to <code>Dialog</code> a<a href="rootpane.html">root pane</a>and support for a default close operation.These are the same features that <code>JFrame</code> has,and using <code>JDialog</code> directlyis very similar to using <code>JFrame</code>.If you're going to use <code>JDialog</code> directly,then you should understand the material in<a href="toplevel.html">Using Top-Level Containers</a>and <a href="frame.html">How to Make Frames</a>, especially<a href="frame.html#windowevents">Responding to Window-Closing Events</a>.<p>Even when you use <code>JOptionPane</code> to implement a dialog,you're still using a <code>JDialog</code> behind the scenes.The reason is that <code>JOptionPane</code> is simply a containerthat can automatically create a <code>JDialog</code>and add itself to the <code>JDialog</code>'s content pane.</blockquote><h3><a name="dialogdemo">The DialogDemo Example</a></h3><blockquote><p>Here's a picture of an applicationthat displays dialogs.<p><center><IMG SRC="../../figures/uiswing/components/DialogDemoMetal.png" WIDTH="359" HEIGHT="285" ALIGN="BOTTOM" ALT="DialogDemo lets you bring up many kinds of dialogs"></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/DialogDemo.jnlp">Run DialogDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#DialogDemo">example index</a>.<li> Click the Show it! button. <br> A modal dialog will appear. Until you close it, the application will be unresponsive, although it will repaint itself if necessary. You can close the dialog either by clicking a button in the dialog or explicitly, such as by using the dialog's window decorations.<li> In the More Dialogs pane, click the bottom radio button and then the Show it! button. <br> A non-modal dialog will appear. Note that the DialogDemo window remains fully functional while the non-modal dialog is up.<li> While the non-modal dialog is showing, iconify the DialogDemo window. <br> The dialog will disappear from the screen until you deiconify the DialogDemo window.</ol><hr></blockquote></blockquote><h3><a name="features">JOptionPane Features</a></h3><blockquote>Using <code>JOptionPane</code>, you can create and customize several different kinds of dialogs.<code>JOptionPane</code> provides supportfor laying out standard dialogs,providing icons,specifying the dialog's title and text,and customizing the button text.Other features allow you to customizethe components the dialog displaysand specify where the dialog should appear onscreen.You can even specify that an option pane put itself into an <a href="internalframe.html">internal frame</a>(<code>JInternalFrame</code>) instead of a <code>JDialog</code>.<p>When you create a <code>JOptionPane</code>,look-and-feel-specific code adds componentsto the <code>JOptionPane</code>and determines the layout of those components.<p><code>JOptionPane</code>'s icon supportlets you easily specifywhich icon the dialog displays.You can use a custom icon, no icon at all,or any one of four standard <code>JOptionPane</code> icons(question, information, warning, and error).Each look and feel has its own versions of the four standard icons.The following figure shows the icons usedin the Java look and feel.<blockquote><table summary="layout"><caption><strong>Icons used by JOptionPane<br>(Java look and feel)</strong></caption><tr><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/metal-question.png" WIDTH="32" HEIGHT="32" ALT="The Java look and feel icon for dialogs that ask questions"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/metal-info.png" WIDTH="32" HEIGHT="32" ALT="The Java look and feel icon for informational dialogs"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/metal-warning.png" WIDTH="32" HEIGHT="32" ALT="The Java look and feel icon for warning dialogs"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/metal-error.png" WIDTH="32" HEIGHT="32" ALT="The Java look and feel icon for error dialogs"></td></tr><tr><td align="center">question</td><td align="center">information</td><td align="center">warning</td><td align="center">error</td></tr></table><p><table><caption><strong>(Windows look and feel)</strong></caption><tr><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/windows-question.png" WIDTH="32" HEIGHT="32" ALT="The Windows look and feel icon for dialogs that ask questions"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/windows-info.png" WIDTH="32" HEIGHT="32" ALT="The Windows look and feel icon for informational dialogs"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/windows-warning.png" WIDTH="32" HEIGHT="32" ALT="The Windows look and feel icon for warning dialogs"></td><td align="center" width="75"><IMG SRC="../../figures/uiswing/components/windows-error.png" WIDTH="32" HEIGHT="32" ALT="The Windows look and feel icon for error dialogs"></td></tr><tr><td align="center">question</td><td align="center">information</td><td align="center">warning</td><td align="center">error</td></tr></table></blockquote><p></blockquote><h3><a name="create">Creating and Showing Simple Dialogs</a></h3><blockquote>For most simple modal dialogs,you create and show the dialog using one of <code>JOptionPane</code>'s<code>show<em>Xxx</em>Dialog</code> methods.If your dialog should be an <a href="internalframe.html">internal frame</a>,then add <code>Internal</code> after<code>show</code>— for example, <code>showMessageDialog</code>changes to <code>showInternalMessageDialog</code>.If you need to control the dialog's window-closing behavioror if the dialog isn't modal, then you should directly instantiate <code>JOptionPane</code>and add it to a <code>JDialog</code> instance.Then invoke <code>setVisible(true)</code> on the <code>JDialog</code>to make it appear.<p>The two most useful <code>show<em>Xxx</em>Dialog</code> methodsare <code>showMessageDialog</code> and<code>showOptionDialog</code>.The <code>showMessageDialog</code> method displays a simple, one-button dialog.The <code>showOptionDialog</code> methoddisplays a customized dialog— it can display a variety of buttons with customized button text,and can contain a standard text message or a collection of components.<p>The other two <code>show<em>Xxx</em>Dialog</code> methodsare used less often.The <code>showConfirmDialog</code> methodasks the user to confirm something,but has the disadvantage of having standard button text(Yes/No or the localized equivalent, for example)rather than button text customized to the user's situation(Start/Cancel, for example).A fourth method, <code>showInputDialog</code>,is designed to display a modal dialog that gets a string from the user,using either a text field or an uneditable combo box.<p>Here are some examples,taken from <a class="SourceLink" target="_blank" href="examples/DialogDemo.java"><code>DialogDemo.java</code></a>,of using <code>showMessageDialog</code>,<code>showOptionDialog</code>,and the <code>JOptionPane</code> constructor.For more example code, see<a class="SourceLink" target="_blank" href="examples/DialogDemo.java"><code>DialogDemo.java</code></a>and the other programs listed in<a href="#eg">Examples that Use Dialogs</a>.<dl><dt> <strong><code>showMessageDialog</code></strong><dd> Displays a modal dialog with one button, which is labeled "OK" (or the localized equivalent). You can easily specify the message, icon, and title that the dialog displays. Here are some examples of using <code>showMessageDialog</code>:<p><table summary="layout"><tr><td valign=top><IMG SRC="../../figures/uiswing/components/DialogIcon1Metal.png" WIDTH="268" HEIGHT="122" ALT="Informational dialog with default title and icon"></td><td valign=top><pre>//default title and iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");</pre></td></tr><tr><td valign=top><IMG SRC="../../figures/uiswing/components/DialogIcon2Metal.png" WIDTH="268" HEIGHT="122" ALT="Informational dialog with custom title, warning icon"></td><td valign=top><pre>//custom title, warning iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);</pre></td></tr><tr><td valign=top><IMG SRC="../../figures/uiswing/components/DialogIcon3Metal.png" WIDTH="268" HEIGHT="122" ALT="Informational dialog with custom title, error icon"></td><td valign=top><pre>//custom title, error iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane error", JOptionPane.ERROR_MESSAGE);</td></tr><tr><td valign=top><IMG SRC="../../figures/uiswing/components/DialogIcon4Metal.png" WIDTH="268" HEIGHT="122" ALT="Informational dialog with custom title, no icon"></td><td valign=top><pre>//custom title, no iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "A plain message", JOptionPane.PLAIN_MESSAGE);</pre></td></tr><tr><td valign=top><IMG SRC="../../figures/uiswing/components/DialogIcon5Metal.png" WIDTH="268" HEIGHT="122" ALT="Informational dialog with custom title, custom icon"></td><td valign=top><pre>//custom title, custom iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);</pre></td></tr></table><dt> <strong><code>showOptionDialog</code></strong><dd> Displays a modal dialog with the specified buttons, icons, message, title, and so on. With this method, you can change the text that appears on the buttons of standard dialogs. You can also perform many other kinds of customization.<p><table summary="layout"><tr><td valign=top><IMG SRC="../../figures/uiswing/components/OptionDialogMetal.png" WIDTH="374" HEIGHT="122" ALT="Yes/No/Cancel (in different words); showOptionDialog"></td></tr><tr><td valign=top><pre>//Custom button textObject[] options = {"Yes, please", "No, thanks", "No eggs, no ham!"};int n = JOptionPane.showOptionDialog(frame, "Would you like some green eggs to go " + "with that ham?", "A Silly Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -