⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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&nbsp;19</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Checkbox and TextField Controls</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTNETS</FONT></B></CENTER><UL><LI><A HREF="#Checkboxes">Checkboxes</A><UL><LI><A HREF="#ExampleCreatingNonexclusiveCheckboxes">Example: Creating Nonexclusive Checkboxes</A><LI><A HREF="#CheckboxGroups">Checkbox Groups</A><LI><A HREF="#ICheckboxIMethods"><I>Checkbox</I> Methods</A><LI><A HREF="#ExampleHandlingCheckboxesinanApplet">Example: Handling Checkboxes in an Applet</A><LI><A HREF="#RespondingtoaCheckboxEvent">Responding to a Checkbox Event</A><LI><A HREF="#ExampleHandlingCheckboxEventsinanApplet">Example: Handling Checkbox Events in an Applet</A></UL><LI><A HREF="#TextFields">TextFields</A><UL><LI><A HREF="#ITextFieldIMethods"><I>TextField</I> Methods</A><LI><A HREF="#ExampleUsingEchoCharacters">Example: Using Echo Characters</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>As you learned in the previous chapter, label and button controlsgive you a couple of ways of presenting and retrieving informationfrom the user. However, Java provides many other types of controls,each of which is adept at a certain type of interactivity withthe user. Checkboxes, for example, are a lot like buttons, exceptthat they enable the user to select from a list of options, whereastextfield controls enable the user to type information that yourapplet needs from the user. In this chapter, you learn about theseimportant controls.<H2><A NAME="Checkboxes"><FONT SIZE=5 COLOR=#Ff0000>Checkboxes</FONT></A></H2><P>Many applications (and applets) require that the user select froma list of options. Sometimes, the user can choose as many optionsas he or she likes (such as when combining various text attributeslike bold and italic), and other times the user can select onlyone option in a list (such as when selecting a color). One wayto provide these kinds of choices to your applet's users is tocreate and display checkbox controls.<P>To create a checkbox, you call the <TT>Checkbox</TT> class's constructor,like this:<BLOCKQUOTE><PRE>Checkbox checkBox = new Checkbox(str, group, check);</PRE></BLOCKQUOTE><P>Here, <TT>str</TT> is a text string for the checkbox's label,<TT>group</TT> is a reference to a <TT>CheckboxGroup</TT> object(used only for exclusive checkboxes), and a <TT>boolean</TT> valueindicating whether the checkbox is selected (<TT>true</TT>) ornot selected (<TT>false</TT>). After you create the checkbox,add it to the applet by calling the <TT>add()</TT> method, likethis:<BLOCKQUOTE><PRE>add(checkbox);<BR></PRE></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When the user can select many options from a list of checkboxes, the checkboxes are being used <I>nonexclusively</I>. When only one checkbox in a group can be selected simultaneously, the checkboxes are being used <I>exclusively</I>. Java's <TT>Checkbox</TT> class enables you to include both types of checkboxes in your applets.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleCreatingNonexclusiveCheckboxes">Example: Creating Nonexclusive Checkboxes</A></H3><P>Suppose that you're writing an applet that requires the user toselect from a list of books. Because you want the user to be ableto select any, all, or none of the books, you want to set up checkboxesin nonexclusive mode. First, you create the checkboxes, as shownin Listing 19.1<HR><BLOCKQUOTE><B>Listing 19.1&nbsp;&nbsp;LST19_1.TXT: Creating NonexclusiveCheckboxes.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>checkbox1 =    new Checkbox(&quot;The Adventures of Javaman&quot;, null, false);checkbox2 =    new Checkbox(&quot;Java by Example&quot;, null, false);checkbox3 =    new Checkbox(&quot;Java and the Single Guy&quot;, null, false);</PRE></BLOCKQUOTE><HR><P>As you know, the Checkbox constructor takes three agruments, whichare the box's label, a reference to the checkbox's group, anda boolean value indicating whether the box should be displayedas checked. After creating the checkboxes, you add them to theapplet:<BLOCKQUOTE><PRE>add(checkbox1);add(checkbox2);add(checkbox3);</PRE></BLOCKQUOTE><P>Now, when you run your applet, the user sees a list of checkboxes,like those shown in Figure 19.1. In the figure, none of the checkboxeshas been selected. To select a checkbox, the user needs only toclick the checkbox with the mouse. Because these are nonexclusivecheckboxes, the user can select as many options as desired, asshown in Figure 19.2.<P><A HREF="f19-1.gif"><B> Figure 19.1 : </B><I>Checkboxes enable the user to select from a list of options.</I></A><P><P><A HREF="f19-2.gif"><B> Figure 19.2 : </B><I>Nonexclusive checkboxes enable the user to select as many options as desired.</I></A><P><H3><A NAME="CheckboxGroups">Checkbox Groups</A></H3><P>In order to create a list of exclusive checkboxes, you must firstassociate the checkboxes in the list with a <TT>CheckboxGroup</TT>object. The first step is to create the <TT>CheckboxGroup</TT>,like this:<BLOCKQUOTE><PRE>CheckboxGroup group = new CheckboxGroup();</PRE></BLOCKQUOTE><P>The <TT>CheckboxGroup</TT> constructor takes no arguments. Afteryou create the <TT>CheckboxGroup</TT> object, you create the checkboxesthemselves, giving a reference to the <TT>CheckboxGroup</TT> objectas the constructor's second argument, as shown in Listing 19.2.<HR><BLOCKQUOTE><B>Listing 19.2&nbsp;&nbsp;LST19_2.TXT: Creating Exclusive Checkboxes.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>checkbox1 =    new Checkbox(&quot;The Adventures of Javaman&quot;, group, true);checkbox2 =    new Checkbox(&quot;Java by Example&quot;, group, false);checkbox3 =    new Checkbox(&quot;Java and the Single Guy&quot;, group, false);</PRE></BLOCKQUOTE><HR><P>In Listing 19.2, notice that the <TT>CheckboxGroup</TT> object,<TT>group</TT>, is given as the second argument of the <TT>Checkbox</TT>class's constructor for each of the checkboxes in the list. Thistells Java that the three checkboxes should all be placed intothe same group and that they should be treated as exclusive checkboxes,meaning only one can be selected at a time. Notice also that thethird argument for the first checkbox is <TT>true</TT>. This valuetells Java that you want the first checkbox to be selected whenJava displays the list.<P>As always, after creating the checkboxes, you must add them tothe applet, by calling the <TT>add()</TT> method for each checkboxin the group:<BLOCKQUOTE><PRE>

⌨️ 快捷键说明

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