📄 ch24.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter 24</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Dialog Boxes</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTNETS</FONT></B></CENTER><UL><LI><A HREF="#UsingaDialogBox">Using a Dialog Box</A><UL><LI><A HREF="#CreatingtheDialogBox">Creating the Dialog Box</A><LI><A HREF="#CreatingtheDialogBoxsLayout">Creating the Dialog Box's Layout</A><LI><A HREF="#DisplayingtheDialogBox">Displaying the Dialog Box</A><LI><A HREF="#RemovingtheDialogBox">Removing the Dialog Box</A><LI><A HREF="#MethodsoftheDialogClass">Methods of the Dialog Class</A><LI><A HREF="#ExampleADialogBoxforTextInput">Example: A Dialog Box for Text Input</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>In most cases, you'll add controls to your applet's display inorder to present information to the user or to obtain informationfrom the user. However, there may be times when you prefer tocreate a dialog box. For example, when the applet encounters somesort of error, a pop-up dialog box not only supplies the userwith important information, but also immediately draws his attentionto that information. Although Java supports dialog boxes, theyunfortunately can only be associated with a frame window. Thisrequirement limits their usefulness, but you still may want touse a dialog box at one time or another. In this chapter, you'llsee how.<H2><A NAME="UsingaDialogBox"><FONT SIZE=5 COLOR=#Ff0000>Using a Dialog Box</FONT></A></H2><P>To create, display, and handle a dialog box, you must performthe following steps: <OL><LI>Create the dialog box object.<LI>Create and set a layout manager for the dialog box.<LI>Create controls and add them to the dialog box.<LI>Call the dialog's <TT>show()</TT> method to display the dialogbox.<LI>When the user clicks the OK or Cancel button, call the dialog's<TT>hide()</TT> method to remove the dialog box from the screen.<LI>Extract and process the data, if any, entered into the dialogbox's controls.</OL><P>The following section discuss the above steps in greater detail.<H3><A NAME="CreatingtheDialogBox">Creating the Dialog Box</A></H3><P>Java's dialog boxes are objects of the <TT>Dialog</TT> class.So, to create a dialog box object, you first call the <TT>Dialog</TT>class's constructor, like this:<BLOCKQUOTE><PRE>Dialog dialog = new Dialog(frame, title, modal);</PRE></BLOCKQUOTE><P>The constructor's three arguments refer to a frame window, thedialog box's title, and <TT>boolean</TT> value indicating whetherthe dialog box is modal (<TT>true</TT>) or modeless (<TT>false</TT>).A modal dialog box, which is the most common of the two types,retains the focus until the user dismisses it. This forces theuser to respond to the dialog box before continuing with the program.A modeless dialog box can lose the focus to another window, whichmeans that the user can switch to another window even while thedialog box is still on the screen.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Although Java claims to support both modal and modeless dialog boxes, the constructor's argument doesn't seem to make any difference. In my experience, every Java dialog box is modeless. Maybe this inconsistency will be corrected by the time you read this book.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="CreatingtheDialogBoxsLayout">Creating the Dialog Box's Layout</A></H3><P>Once you have the dialog box object created, you must give ita layout manager. If you fail to do this, any components you tryto place in the dialog box will not appear. You perform this stepexactly as you would for any other type of window or applet, bycreating and setting the layout object:<BLOCKQUOTE><PRE>FlowLayout layout = new FlowLayout();dialog.setLayout(layout);</PRE></BLOCKQUOTE><P>The next step is to create and add whatever controls you wantto appear in the dialog box. You'll always have at least an OKbutton, with which the user can dismiss the dialog box:<BLOCKQUOTE><PRE>Button button = new Button("OK");dialog.add(button);</PRE></BLOCKQUOTE><H3><A NAME="DisplayingtheDialogBox">Displaying the Dialog Box</A></H3><P>Just like a frame window, a dialog box doesn't appear on the screenuntil you call its <TT>show()</TT> method, like this:<BLOCKQUOTE><PRE>dialog.show();</PRE></BLOCKQUOTE><P>Once the dialog box is on the screen, the user can manipulateits controls in order to enter information into the dialog box'sfields or to dismiss the dialog box from the screen.<H3><A NAME="RemovingtheDialogBox">Removing the Dialog Box</A></H3><P>When the user clicks a dialog box's OK or Cancel buttons, that'syour applet's signal to remove the dialog box from the screen,which you do by calling its <TT>hide()</TT> method:<BLOCKQUOTE><PRE>dialog.hide();</PRE></BLOCKQUOTE><P>The <TT>hide()</TT> method removes the dialog box from the screen,but the dialog box and its controls remain in memory so that youcan access them in order to extract whatever information the usermay have entered into the dialog box.<P>After you've removed the dialog box from the screen, you can usea control's methods to extract whatever information the user mayhave entered into the dialog's controls. For example, to get theentry from a text field control, you'd call the control's <TT>getText()</TT>method.<H3><A NAME="MethodsoftheDialogClass">Methods of the Dialog Class</A></H3><P>Like any class, <TT>Dialog</TT> provides a set of public methodsthat you can use to control the dialog box. <TT>Dialog</TT> alsoinherits many methods from its superclasses, <TT>Window</TT> and<TT>Container</TT>. Table 24.1 lists the most useful methods ofthe <TT>Dialog</TT> class, including those methods inherited fromthe <TT>Window</TT> and <TT>Container</TT> classes.<BR><P><CENTER><B>Table 24.1 Useful Methods of the </B><I>Dialog</I><B>Class (Including Inherited).</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=295><I><B>Method</B></I></TD><TD WIDTH=295><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Component add(Component comp)</TT></TD><TD WIDTH=295>Adds a component to the dialog box.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void dispose()</TT></TD><TD WIDTH=295>Removes the dialog box from memory.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>LayoutManager getLayout()</TT></TD><TD WIDTH=295>Returns the dialog's layout manager.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>String getTitle()</TT></TD><TD WIDTH=295>Returns the dialog box's title.</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -