📄 ch6.htm
字号:
<LI>Type Listing 6.4 and save it in the CLASSES folder as an ASCIIfile. Name the file <TT>Applet3.java</TT>. Note that you can copythe listing from this book's CD-ROM if you don't want to typeit.</OL><HR><BLOCKQUOTE><B>Listing 6.4 Applet3.java: Source Code for the Applet3Applet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class Applet3 extends Applet{ TextField textField; public void init() { textField = new TextField(20); add(textField); } public void paint(Graphics g) { String s = textField.getText(); g.drawString(s, 40, 50); } public boolean action(Event event, 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 program uses classes in the <TT>awt</TT> package.<BR>Tell Java that the program uses classes in the <TT>applet</TT>package.<BR>Derive the <TT>Applet3</TT> class from Java's <TT>Applet</TT>class.<BR> Declare <TT>textField</TT> as an object of the <TT>TextField</TT>class.<BR> Override the <TT>Applet</TT> class's <TT>init()</TT> method.<BR> Create the new <TT>TextField</TT> object.<BR> Add the <TT>TextField</TT> object to the applet's display.<BR> Override the <TT>Applet</TT> class's <TT>paint()</TT> method.<BR> Get the text string from the <TT>TextField</TT> control.<BR> Display the text in the applet's display area.<BR> Override the <TT>Applet</TT> class's <TT>action()</TT> method.<BR> Force the applet to redraw its display area.<BR> Tell Java that the <TT>action()</TT> method finished successfully.</BLOCKQUOTE><OL><LI>Start an MS-DOS session by selecting Start/Programs/MS-DOSPrompt. If the MS-DOS screen takes over the entire screen, pressAlt+Enter to display the MS-DOS session in a window.<LI>Type <TT>cd c:\classes</TT> to move to your CLASSES folder.<LI>Type <TT>javac Applet3.java</TT> to compile the Applet3 applet.You should receive no error messages. If you do receive errormessages, carefully check your typing of Listing 6.4. Also, makesure you typed the command line <TT>javac Applet3.java</TT> properly.<LI>Modify Listing 6.2 by replacing all occurrences of Applet1with Applet3 and save it as an ASCII file to your CLASSES folder.Name the file APPLET3.htmL.<LI>At the MS-DOS prompt, type <TT>appletviewer applet3.html</TT>.When you do, Appletviewer will load and display the applet.</OL><P>When you run Applet3, go ahead and type some text into the <TT>TextField</TT>control. When you press Enter, the text appears in the applet'sdisplay area, right below the control (Figure 6.6).<P><A HREF="f6-6.gif"><B> Figure 6.6 : </B><I>Applet3 can display the text you type into the TextField control.</I></A><P><H3><A NAME="HowAppletWorks2">How Applet3 Works</A></H3><P>If you look at the Applet3 program code, you can see that you'veadded a <TT>paint()</TT> method. This is where the text that theuser types into the <TT>TextField</TT> control gets displayed.First, <TT>paint()</TT> extracts the text from the control, likethis:<BLOCKQUOTE><PRE>String s = textField.getText();</PRE></BLOCKQUOTE><P>This line declares a variable called <TT>s</TT> to be an objectof the <TT>String</TT> class, and then it sets <TT>s</TT> equalto the text string in the control. (The <TT>String</TT> classis another of Java's built-in classes that you'll learn more aboutlater. For now, just know that this class represents text strings.)You can see in this line that you're calling the <TT>textField</TT>object's <TT>getText()</TT> method, because of the object andmethod name separated by the dot. The <TT>getText()</TT> methodsimply returns the text string that's stored in the <TT>textField</TT>control.<P>Displaying the string is as easy as calling on your old friend<TT>drawString()</TT>, as follows:<BLOCKQUOTE><PRE>g.drawString(s, 40, 50);</PRE></BLOCKQUOTE><P>One big difference between Applet2 and Applet3 is the <TT>action()</TT>method. Java calls this method whenever the user performs someaction with controls in the applet. In this case, the action that<TT>action()</TT> responds to is the user's pressing Enter aftertyping text. In Applet3, the <TT>action()</TT> method does nothingmore than call the applet's <TT>repaint()</TT> method, which tellsJava that you want to redraw the applet's display area. (You'lllearn more about using the <TT>action()</TT> method later in thebook.) This causes Java to call the <TT>paint()</TT> method, whichvery neatly displays the control's string.<H2><A NAME="DisplayingNumericalValues"><FONT SIZE=5 COLOR=#Ff0000>Displaying Numerical Values</FONT></A></H2><P>In the previous chapter, you learned to declare and define a numberof different variable types, including <TT>int</TT>, <TT>byte</TT>,<TT>short</TT>, <TT>float</TT>, and other numerical values. Theproblem with numerical values in a computer is that you can'tdisplay them without first converting them to text strings. Luckily,this task is pretty easy to perform. You need only call the <TT>String</TT>class's <TT>valueOf()</TT> method, as shown in Listing 6.5. Figure6.7 shows Appletviewer running Applet4.<P><A HREF="f6-7.gif"><B> Figure 6.7 : </B><I>The Applet4 applet converts and displays an integer value.</I></A><P><HR><BLOCKQUOTE><B>Listing 6.5 Applet4.java: The Source Code for Applet4.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class Applet4 extends Applet{ public void paint(Graphics g) { int x = 10; String s = String.valueOf(x); g.drawString(s, 40, 50); }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the program uses classes in the <TT>awt</TT> package.<BR>Tell Java that the program uses classes in the <TT>applet</TT>package.<BR>Derive the <TT>Applet4</TT> class from Java's <TT>Applet</TT>class.<BR> Override the <TT>Applet</TT> class's <TT>paint()</TT> method.<BR> Declare the integer x and set its value to 10.<BR> Convert x to a string.<BR> Display the string that represents x's value.</BLOCKQUOTE><P>As you can see in Listing 6.5, the <TT>String</TT> class has amethod called <TT>valueOf()</TT> that can convert numerical valuesto strings. The method's single argument is the value you wantto convert, which can be any of Java's numerical data types.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>All computer programs must deal with some form of I/O. At thevery least, a program must be able to display information to theuser, as well as retrieve data from the user. Java has severalways to handle this basic form of I/O, a couple of which you learnedin this chapter. As you dig deeper into the art of writing Javaapplets, you'll see other ways to perform I/O.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What is graphical text?<LI>What's the difference between proportional and non-proportionalfonts?<LI>What are arguments?<LI>Describe the three arguments used with the <TT>drawString()</TT>method.<LI>What does the <TT>paint()</TT> method do?<LI>Describe one way to get user input in your applets.<LI>Describe how the <TT>init()</TT> method works.<LI>Describe how the <TT>action()</TT> method works.<LI>How can you convert a numerical value to a text string?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Modify Applet1 so that it displays the text string "Outto lunch!" at position 30, 50 in the applet's display area.<LI>Modify Applet2 so that it displays a <TT>TextField</TT> controlhalf as wide as the one displayed in the original program.<LI>Write an applet that displays the days of the week in a columndown the center of the applet's display area.<LI>Using Applet3 as a model, write a new applet called NameAppletthat asks the user for her name and age and then prints the answersin the applet's display area. The finished applet should looklike Figure 6.8. (This one's tough, so, if you get stumped, checkout the file NameApplet.java in the CHAP06 folder of this book'sCD-ROM. First try to solve the problem yourself, though.)<BR><A HREF="f6-8.gif"><B> Figure 6.8 : </B><I>This is what the NameApplet applet should look like when it is run with Appletviewer.</I></A><P></OL><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 + -