📄 ch20.htm
字号:
list.addItem("Pearl Jam"); list.addItem("Dream Theater"); list.addItem("Joe Satriani"); list.addItem("Oasis"); list.addItem("Alanis Morissette"); list.addItem("Soul Asylum"); list.addItem("The Rembrandts"); list.addItem("Smashing Pumpkins"); list.addItem("Joan Osborne"); list.addItem("Bjork"); add(list); resize(300, 150); } public void paint(Graphics g) { g.drawString("CHOSEN ITEM:", 100, 110); String s = list.getSelectedItem(); if (s == null) s = "None"; g.drawString(s, 100, 130); } public boolean action(Event evt, Object arg) { repaint(); return true; }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>ListApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the list object.<BR> Override the <TT>init()</TT> method.<BR> Create the list object.<BR> Add items to the list object.<BR> Add the list to the applet.<BR> Set the applet's size.<BR> Override the <TT>paint()</TT> method.<BR> Draw a label in the applet's display.<BR> Get the selected item from the list box.<BR> If there is no item selected, set the string to "None."<BR> Display the selected item.<BR> Override the <TT>action()</TT> method.<BR> Force the applet to repaint its display.<BR> Tell Java that the event was handled okay.</BLOCKQUOTE><P>When you run ListApplet with Appletviewer, you see the windowshown in Figure 20.7. When you double-click an item in the list,Java calls the applet's <TT>action()</TT> method in which theapplet calls the <TT>repaint()</TT> method. This forces Java tocall the <TT>paint()</TT> method, where the applet retrieves theselected item and displays it.<P><A HREF="f20-7.gif"><B> Figure 20.7 : </B><I>The scrolling list in this applet lets you choose a single musical artist.</I></A><P><P>Notice the call to <TT>resize()</TT> in the <TT>init()</TT> method.The <TT>resize()</TT> method enables you to set the applet toany size you wish. This size overrides any size setting that'sincluded in the HTML document that ran the applet.<H2><A NAME="TheITextAreaIControl"><FONT SIZE=5 COLOR=#Ff0000>The <I>TextArea</I> Control</FONT></A></H2><P>Throughout this book, you've been using the <TT>TextField</TT>control to retrieve information from the user. In most cases,the <TT>TextField</TT> control works great, but it does have somelimitations, the most serious being the fact that it can displayonly one line of text at a time. There may be situations whereyou'd like to display one or more paragraphs of text in your applet,in a control that enables the user to edit existing text, as wellas enter his or her own text. This is where the <TT>TextArea</TT>control is useful. The <TT>TextArea</TT> control is a text boxthat acts like a simple word processor. When you display a textbox, the user can type and edit multiple lines of text.<P>To create a <TT>TextArea</TT> control, you call the class's constructor,like this:<BLOCKQUOTE><PRE>TextArea textArea = new TextArea(str, rows, cols);</PRE></BLOCKQUOTE><P>This constructor's three arguments are the string to display inthe control, the number of rows in the control, and the numberof columns. As with the other controls, after you create the <TT>TextField</TT>object, you add it to the applet by using the <TT>add()</TT> method.<H3><A NAME="ExampleCreatingaITextAreaIControl">Example: Creating a <I>TextArea</I> Control</A></H3><P>As an example, suppose that you need to create a <TT>TextArea</TT>control that'll start off displaying eight lines of text. Listing20.6 is an applet, called TextAreaApplet, that creates a <TT>TextArea</TT>control that displays eight lines of text. Figure 20.8 shows whatthe applet looks like running under Appletviewer. When you runthe applet, click on the <TT>TextArea</TT> control's box and tryediting the text in the window. As you'll discover, you not onlycan edit the existing text, but also add new text.<P><A HREF="f20-8.gif"><B> Figure 20.8 : </B><I>TextAreaApplet applet run-<r>ning under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 20.6 TEXTAREAAPPLET.JAVA: The TextAreaAppletApplet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class TextAreaApplet extends Applet{ TextArea textArea; public void init() { String s = "This is an example of a\n"; s += "textarea control, which is not\n"; s += "unlike a textfield control.\n"; s += "The big difference is that a\n"; s += "textarea control can hold many\n"; s += "lines of text, whereas a\n"; s += "textfield control deals with\n"; s += "only one line of text at a time.\n"; textArea = new TextArea(s, 9, 30); add(textArea); resize(300, 180); }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>TextAreaApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the <TT>TextArea</TT> object.<BR> Override the <TT>init()</TT> method.<BR> Create the string to display in the control.<BR> Create the <TT>TextArea</TT> object.<BR> Add the control to the applet.<BR> Set the applet's size.<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you look at how the <TT>TextArea</TT> control's display string is created in TextAreaApplet, you'll see that you can store multiple lines of text into a single <TT>String</TT> object. You do this by placing the newline character (<TT>\n</TT>) at the end of each line that you add to the string.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>When you run TextAreaApplet, notice how all the text fits withinthe text box. Because the text is fully displayed, the control'sscroll bars are inactive. However, if you were to edit the textsuch that you added more lines than the control can display, ormade a line longer that the control can display, the control'sscroll bars become active. Figure 20.9 shows TextAreaApplet afterthe user has added text that forces the scroll bars to becomeactive. You can use the scroll bars to view the portions of thetext that are offscreen.<P><A HREF="f20-9.gif"><B> Figure 20.9 : </B><I>When the text contained in the control cannot be fully displayed, a TextArea control activates its scroll bars.</I></A><P><H3><A NAME="MethodsoftheITextAreaIClass">Methods of the <I>TextArea</I> Class</A></H3><P>To enable you to easily manipulate the text, the <TT>TextArea</TT>class features a number of public methods. You can use these methodsto modify the text in the control or to obtain information aboutthe control. Table 20.3 shows the most useful methods and whatthey do.<BR><P><CENTER><B>Table 20.3 Useful Methods of the </B><I>TextArea</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=293><I><B>Method</B></I></TD><TD WIDTH=298><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=293><TT>void appendText(String str)</TT></TD><TD WIDTH=298>Appends text to the control.</TD></TR><TR VALIGN=TOP><TD WIDTH=293><TT>int getColumns()</TT></TD><TD WIDTH=298>Returns the number of columns in the control.</TD></TR><TR VALIGN=TOP><TD WIDTH=293><TT>int getRows()</TT></TD><TD WIDTH=298>Returns the number of rows in the `control.</TD></TR><TR VALIGN=TOP><TD WIDTH=293><TT>void insertText(String str,</TT></TD><TD WIDTH=298>Inserts text at the given position.<TT>int pos)</TT></TD></TR><TR VALIGN=TOP><TD WIDTH=293><TT>void replaceText(String str,</TT></TD><TD WIDTH=298>Replaces text specified by the <TT>int start, int end)</TT>starting and ending points.</TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>Choice menus are a powerful control that enable you to includea pop-up menu of commands for the user of your applet. By usingsuch a menu, the user can more easily control the applet, as wellas set options, without the controls' taking up a lot of screenspace. Scrolling lists are a valuable tool for ensuring that theuser always enters a response from a valid set of responses. Youcan even set up a list to accept multiple selections. Finally,the <TT>TextArea</TT> control provides a simple text editor thatyou can easily add to your applets.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>How many arguments are accepted by the <TT>Choice</TT> class'sconstructor?<LI>How do you add items to a choice menu?<LI>What are the two arguments needed by the <TT>List</TT> class'sconstructor?<LI>How do you add items to a scrolling list?<LI>When would you use a <TT>TextArea</TT> control in place ofa <TT>TextField</TT> control?<LI>How can you determine which menu command the user selected?<LI>How do you create a multiple-selection scrolling list?<LI>How do you retrieve the selected item from a single-selectionscrolling list?<LI>How do you create a single string containing multiple linesof text?<LI>How do you retrieve multiple selections from a scrolling list?<LI>Can you delete items from a scrolling list?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that has a menu containing the commands <TT>On</TT>and <TT>Off</TT>.<LI>Write an applet that displays a single-selection scrollinglist containing the titles of five movies.<LI>Write an applet that displays a <TT>TextArea</TT> control.The control should display five lines of text at startup.<LI>Write an applet that changes the size of the applet basedon five selections in a choice menu.<LI>Revise the applet from exercise 3 such that the user usesa single-selection list to select the applet's size.<LI>Write an applet called TextTransferApplet that includes alist box and a <TT>TextArea</TT> control. The list box shouldcontain 10 words. When the user clicks a word, the word shouldappear in the <TT>TextArea</TT> control on a new line. Figure20.10 shows what the completed applet should look like, and Figure20.11 shows the applet after the user has transferred severalwords to the <TT>TextArea</TT> control. You can find the solutionfor this problem in the CHAP20 folder of this book's CD-ROM.</OL><BR><A HREF="f20-10.gif"><B> Figure 20.10 : </B><I>TextTransferApplet should look like this.</I></A><P><P><A HREF="f20-11.gif"><B> Figure 20.11 : </B><I>Here's the applet after the user has transferred a few words to the text area.</I></A><P><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -