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

📄 ch20.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;20</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Choice Menu, Text Area, and ScrollingList Controls</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#ChoiceMenus">Choice Menus</A><UL><LI><A HREF="#ExampleCreatingaChoiceMenu">Example: Creating a Choice Menu</A><LI><A HREF="#ChoiceMenuMethods">Choice Menu Methods</A><LI><A HREF="#ExampleRespondingtoMenuEventsinanApplet">Example: Responding to Menu Events in an Applet</A></UL><LI><A HREF="#ScrollingLists">Scrolling Lists</A><UL><LI><A HREF="#ExampleCreatingaSingleSelectionList">Example: Creating a Single-Selection List</A><LI><A HREF="#ExampleCreatingaMultipleSelectionList">Example: Creating a Multiple-Selection List</A><LI><A HREF="#ExampleCreatingaScrollingList">Example: Creating a Scrolling List</A><LI><A HREF="#MethodsoftheIListIClass">Methods of the <I>List</I> Class</A><LI><A HREF="#ExampleUsingaScrollingListinanApplet">Example: Using a Scrolling List in an Applet</A></UL><LI><A HREF="#TheITextAreaIControl">The <I>TextArea</I> Control</A><UL><LI><A HREF="#ExampleCreatingaITextAreaIControl">Example: Creating a <I>TextArea</I> Control</A><LI><A HREF="#MethodsoftheITextAreaIClass">Methods of the <I>TextArea</I> Class</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>If your applet must handle only a couple of user commands, it'san easy matter to include button controls or checkbox groups inorder to enable the user to select those commands. However, often,an applet's command set is sophisticated enough to warrant completemenus. Java features choice menus for just this eventuality. Javaalso features a scrolling list control, not unlike Windows' listbox control, that enables the user to select items from a list.Finally, the text area control enables you to display paragraphsof text within your applet. You'll learn about all these controlsin this chapter.<H2><A NAME="ChoiceMenus"><FONT SIZE=5 COLOR=#Ff0000>Choice Menus</FONT></A></H2><P>Choice menus are very similar to the drop-down lists. When theuser selects the menu, a list of commands appears from which theuser can choose. After the user makes a choice, the menu disappears.<P>To create a choice menu, you must first create an instance ofthe <TT>Choice</TT> class, like this:<BLOCKQUOTE><PRE>Choice menu = new Choice();</PRE></BLOCKQUOTE><P>As you can see, the <TT>Choice</TT> class's constructor acceptsno arguments.<P>After you have created the <TT>Choice</TT> object, you can additems to the menu by calling the object's <TT>addItem()</TT> method:<BLOCKQUOTE><PRE>menu.addItem(str);</PRE></BLOCKQUOTE><P>Here, <TT>str</TT> is the text that will appear as the commandin the menu. You can call <TT>addItem()</TT> as often as you needto build a complete menu. When you have the complete menu created,you add it to the applet by calling the <TT>add()</TT> method.<H3><A NAME="ExampleCreatingaChoiceMenu">Example: Creating a Choice Menu</A></H3><P>Creating a menu is a fairly straightforward process. For example,to create a choice menu that enables the user to select betweenthree font styles, you might use the code shown in Listing 20.1.<HR><BLOCKQUOTE><B>Listing 20.1&nbsp;&nbsp;LST20_1.TXT: Creating a Choice Menu.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>Choice menu = new Choice();menu.addItem(&quot;Plain&quot;);menu.addItem(&quot;Bold&quot;);menu.addItem(&quot;Italic&quot;);add(menu);</PRE></BLOCKQUOTE><HR><P>When the user runs the applet containing this menu, a menu likethat shown in Figure 20.1 appears. To display the menu, the userclicks on the arrow to the right of the text box displaying thecurrent choice, and the menu appears, as shown in Figure 20.2.To select a choice, the user merely clicks an entry in the menu.<P><A HREF="f20-1.gif"><B> Figure 20.1 : </B><I>Java displays a choice menu as a text box with an arrow.</I></A><P><P><A HREF="f20-2.gif"><B> Figure 20.2 : </B><I>Clicking the arrow displays the menu.</I></A><P><H3><A NAME="ChoiceMenuMethods">Choice Menu Methods</A></H3><P>As you've no doubt guessed, the <TT>Choice</TT> class featuresa number of public methods that enable you to manipulate choicemenus in various ways. Using these methods, you can do everythingfrom determining the number of command entries in a  menu to addingitems to the menu. Table 20.1 lists the most commonly used methodsand their descriptions.<BR><P><CENTER><B>Table 20.1&nbsp;&nbsp;Methods of the </B><I>Choice</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=262><I><B>Method</B></I></TD><TD WIDTH=328><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int countItems()</TT></TD><TD WIDTH=328>Returns the number of command items in the menu.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>String getItem(int index)</TT></TD><TD WIDTH=328>Returns the command text for a given item.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>void addItem(String item)</TT></TD><TD WIDTH=328>Adds an item to the menu.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>String getSelectedItem()</TT></TD><TD WIDTH=328>Returns a string representing the selected item.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getSelectedIndex()</TT></TD><TD WIDTH=328>Returns the index of the selected item.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>void select(int index)</TT></TD><TD WIDTH=328>Selects the item at the given position.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>void select(String str)</TT></TD><TD WIDTH=328>Selects the item with the given text string.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleRespondingtoMenuEventsinanApplet">Example: Responding to Menu Events in an Applet</A></H3><P>A menu isn't much good unless you can determine which item theuser selected so that the applet can carry out the user's command.As with most controls, when the user makes a selection from achoice menu, Java generates an event that you can capture in the<TT>action()</TT> method. In the case of a choice menu, <TT>action()</TT>'sfirst parameter is the menu instance, and the second parameteris the text of the selected menu item. By comparing the text tothe items you added to the menu, you can determine which menuitem the user chose.<P>The ChoiceApplet applet, whose source code is shown in Listing20.2, demonstrates creating and responding to choice menus inan applet. Listing 20.3 is the applet's HTML document. When yourun the applet under Appletviewer, you see the window shown inFigure 20.3. The text in the window is displayed using the currentlyselected color in the choice menu. To change the text color, justselect a new menu item.<P><A HREF="f20-3.gif"><B> Figure 20.3 : </B><I>ChoiceApplet displays text in the color selected from its choice menu.</I></A><P><HR><BLOCKQUOTE><B>Listing 20.2&nbsp;&nbsp;ChoiceApplet.java: Using Menus in anApplet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ChoiceApplet extends Applet{    Choice menu;    Color color;    public void init()    {        Choice menu = new Choice();        menu.addItem(&quot;Black&quot;);        menu.addItem(&quot;Red&quot;);        menu.addItem(&quot;Green&quot;);        menu.addItem(&quot;Blue&quot;);        add(menu);        color = Color.black;    }    public void paint(Graphics g)    {

⌨️ 快捷键说明

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