📄 ch26.htm
字号:
<PARAM NAME=typesize VALUE=18><PARAM NAME=xpos VALUE=60><PARAM NAME=ypos VALUE=150></PRE></BLOCKQUOTE><HR><H2><A NAME="DefaultParameterValues"><FONT SIZE=5 COLOR=#Ff0000>Default Parameter Values</FONT></A></H2><P>You might have noticed by now that there's a big problem withthe ConfigApplet and ConfigApplet2 applets. Neither applet checksto ensure that the parameters it tries to retrieve exist. Forexample, what happens when the user forgets to include the <TT>text</TT>parameter?<P>Relying on other people to provide your applet with the data itneeds is a dangerous practice. Your applet should always checkthe validity of values returned from the <TT>getParameter()</TT>method. At the very least, you should be sure that the returnedvalue is not <TT>null</TT>, which is the value <TT>getParameter()</TT>returns when a particular parameter doesn't exist (that is, theuser forget to define it in the HTML document or deliberatelyleft it out assuming that the applet will automatically use adefault value for the missing one).<P>To ensure that your applet is in a runnable state after retrievingparameters, you must always check the parameter's values and supplydefault values for those parameters that are missing or invalid.For example, to make sure that your applet has a text string todisplay, you might use lines like this:<BLOCKQUOTE><PRE>str = getParameter("text");if (str == null) str = "Default Text";<BR></PRE></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you decide to release your applets so other people can use them in their Web pages, be sure that you include a separate documentation file that describes the applet's parameters and shows how to use them.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingDefaultParametersinanApplet">Example: Using Default Parameters in an Applet</A></H3><P>You can now extend the ConfigApplet2 so that it provides defaultvalues for each parameter. When you've done this, the applet canrun without generating errors no matter what parameters the userchooses to include or ignore. Listing 26.7 is the new version,called ConfigApplet3.<P>Notice that although the program now checks for missing parameters,it doesn't limit the values to any ranges or otherwise check theirvalidity. Because the <TT>text</TT> parameter will always be astring, there's really nothing you need to check for (except <TT>null</TT>).However, you may want to limit the font size or make sure thatthe display location is inside the applet's window. Listing 26.8is the HTML document used to load and run the applet as it's displayedin Figure 26.4.<P><A HREF="f26-4.gif"><B> Figure 26.4 : </B><I>This is ConfigApplet3 running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 26.7 ConfigApplet3.java: This Applet ProvidesDefault Values for All Parameters.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ConfigApplet3 extends Applet{ String str; Point position; public void init() { HandleTextParam(); HandleTypeSizeParam(); HandlePositionParam(); } public void paint(Graphics g) { g.drawString(str, position.x, position.y); } protected void HandleTextParam() { str = getParameter("text"); if (str == null) str = "Default Text"; } protected void HandleTypeSizeParam() { String s = getParameter("typesize"); if (s == null) s = "24"; int typeSize = Integer.parseInt(s); Font font = new Font("TimesRoman", Font.BOLD, typeSize); setFont(font); } protected void HandlePositionParam() { String s = getParameter("xpos"); if (s == null) s = "20"; int xpos = Integer.parseInt(s); s = getParameter("ypos"); if (s == null) s = "50"; int ypos = Integer.parseInt(s); position = new Point(xpos, ypos); }}</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>ConfigApplet3</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the class's data fields.<BR> Override the <TT>init()</TT> method.<BR> Call the methods that retrieve and validate the parameters.<BR> Override the <TT>paint()</TT> method.<BR> Display the given string.<BR> Define the <TT>HandleTextParam()</TT> method.<BR> Retrieve the text parameter.<BR> If the parameter is null, set str to the default text.<BR> Define the <TT>HandleTypeSizeParam()</TT> method.<BR> Retrieve the typesize parameter.<BR> If the parameter is null, set the parameter string to"24."<BR> Convert the parameter string to an integer.<BR> Create and set the font.<BR> Define the <TT>HandlePositionParam()</TT> method.<BR> Retrieve the xpos parameter.<BR> if xpos is null, set the parameter string to "20."<BR> Convert the parameter string to an integer.<BR> Retrieve the ypos parameter.<BR> if ypos is null, set the parameter string to "50."<BR> Convert the parameter string to an integer.<BR> Create the Point object with the position values.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 26.8 CONFIGAPPLET3.htmL: The HTML Documentfor ConfigApplet3.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="ConfigApplet3.class" width=640 height=200 name="ConfigApplet3"> <PARAM NAME=text VALUE="Hi there!"> <PARAM NAME=typesize VALUE=144> <PARAM NAME=xpos VALUE=40> <PARAM NAME=ypos VALUE=140></applet></PRE></BLOCKQUOTE><HR><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>By supporting parameters, your applets are more flexible, whichmakes it easier for other people to incorporate them into theirWeb-page designs. Even if you don't plan to release your applets,using parameters can make your applets more powerful and yourown Web pages easier to fine tune. Using the <TT><PARAM></TT>tag is more sensible than having to reprogram and recompile anapplet every time you want it to do something slightly different.Keep in mind, though, that all parameters must have default valuesbuilt into the applet's source code. Otherwise, you could endup with an error-ridden applet, something that won't do much foryour reputation as a Java guru.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What kind of applet user is likely to appreciate parameterizedapplets?<LI>What are the two parts of the <TT><PARAM></TT> tag?<LI>How can your applet retrieve the value of a parameter?<LI>Where do you specify the values for an applet's parameters?<LI>Do you need to recompile an applet in order to take advantageof new parameters?<LI>How many parameters can you have in a single applet?<LI>Why do you need to convert the values of some parameters?<LI>Why is it important to supply default values for all parameters?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that displays a rectangle on the screen. Therectangle's size should be specified using parameters.<LI>Write an applet that uses a parameter to display a red, green,or blue background.<LI>Modify ConfigApplet2 so that it not only checks for null parameters,but also checks for the parameters' validity. The type size shouldalways be in the range of 10 to 72 points and the text shouldalways be positioned so that it's never printed completely outof view. (You can find the solution to this exercise, called ConfigApplet4,in the CHAP26 folder of this book's CD-ROM.)</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 + -