📄 ch26.htm
字号:
<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 26</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Configurable Applets</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#TypesofUsers">Types of Users</A><LI><A HREF="#ParametersandApplets">Parameters and Applets</A><UL><LI><A HREF="#ExampleSettingandRetrievingaParametersValue">Example: Setting and Retrieving a Parameter's Value</A><LI><A HREF="#ExampleUsingaParameterinanApplet">Example: Using a Parameter in an Applet</A></UL><LI><A HREF="#MultipleParameters">Multiple Parameters</A><UL><LI><A HREF="#ExampleUsingMultipleParametersinanApplet">Example: Using Multiple Parameters in an Applet</A></UL><LI><A HREF="#DefaultParameterValues">Default Parameter Values</A><UL><LI><A HREF="#ExampleUsingDefaultParametersinanApplet">Example: Using Default Parameters in an Applet</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>All of the applets you've written so far have one thing in common.Outside of the starting size of the applet, none of your appletsare configurable. That is, the user can't configure the appletto fit his needs. In many cases, it doesn't make sense to givethe user configurable options. But, just as often, someone whowants to use your applet in his own home page will want to makeminor changes without having to change and recompile the sourcecode. In fact, the user probably won't even have access to thesource code. In this chapter, you get a look at configurable applets,which enable the applet's user to modify how an applet looks andacts, all without having to change a line of Java code.<H2><A NAME="TypesofUsers"><FONT SIZE=5 COLOR=#Ff0000>Types of Users</FONT></A></H2><P>Before you read further, it might be a good idea to define exactlywhat a user is. When it comes to applets, you could say that thereare two kinds of users. The first kind is a net surfer who logsonto your home page and sees all the cool applets you've spentthe last six months creating. Because this user is not installingyour applets on his own Web pages-he's just a casual observer-hedoesn't need access to the applet's parameters. In fact, if youwant your Web pages to look right for different users, it justdoesn't make sense to enable the surfer to configure an applet.<P>The other kind of user is the guy who found your applet on a serversomewhere and wants to incorporate the applet into his own Webpages. Assuming that you've released your applet into the worldfor others to use, you want this type of user to find your appletto be as flexible as possible. However, you probably don't wantto give this user your source code and expect him to make changesthat require recompiling. Hey, he could end up trashing the appletcompletely, right?<P>So, to make it easy for this user to modify the applet's appearanceand functionality, you must build in support for parameters. Touse these parameters, the user only needs to add a few lines tothe HTML document that loads and runs the applet. For example,you may have written an applet that displays an awesome titleon your home page. Now, you want to release the applet so thatother netfolks can use it in their Web pages. However, these folksare going to want to display their own titles. So, you make thetitle string a parameter.<P>In the sections that follow, you'll not only learn how to supportapplet parameters, but you'll also learn how to make those parametersidiot-proof.<H2><A NAME="ParametersandApplets"><FONT SIZE=5 COLOR=#Ff0000>Parameters and Applets</FONT></A></H2><P>When you want to use an applet that supports parameters, you mustadd the parameters and their values to the HTML document thatloads and runs the applet. You do this using the <TT><PARAM></TT>tag, which has two parts. The <TT>NAME</TT> part of the tag specifiesthe parameter's name, and the <TT>VALUE</TT> part specifies theparameter's value. For example, suppose you want to provide atitle parameter for that title applet you read about in the previoussection. The parameter tag might look like this:<BLOCKQUOTE><PRE><PARAM NAME=title VALUE="Big Al's Home Page"></PRE></BLOCKQUOTE><P>Here, the name of the parameter is <TT>title</TT>. The appletwill use this name to identify the parameter. The value of the<TT>title</TT> parameter in the above line is the text string<TT>Big Al's Home Page</TT>. The applet will retrieve this textstring in order to display the title the user wants. A completeHTML document for the title applet might look something like Listing26.1.<HR><BLOCKQUOTE><B>Listing 26.1 LST26_1.TXT: Using a Parameter in anHTML Document.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="TitleApplet.class" width=250 height=150 name="TitleApplet"> <PARAM NAME=title VALUE="Big Al's Home Page"></applet></PRE></BLOCKQUOTE><HR><P>As you can see, the <TT><PARAM></TT> tag is enclosed betweenthe <TT><applet></TT> and <TT></applet></TT> tags.that is, the parameters are part of the applet's HTML code.<P>How does your applet retrieve the parameter at run time? An excellentquestion, and one for which I fortunately have the answer. Toretrieve a parameter, you call the applet's <TT>getParameter()</TT>method, like this:<BLOCKQUOTE><PRE>String param = getParameter(name);</PRE></BLOCKQUOTE><P>The <TT>getParameter()</TT> method takes a single argument, whichis a string containing the name of the parameter for which youwant the value. The method always returns a string to your applet.This string is, of course, the part of the <TT>PARAM</TT> tagthat follows the <TT>VALUE=</TT>.<H3><A NAME="ExampleSettingandRetrievingaParametersValue">Example: Setting and Retrieving a Parameter's Value</A></H3><P>Suppose that you've written an applet that displays a fancy greetingto the viewer. (How fancy the greeting is displayed depends uponthe code you've written for the applet. Because how the appletactually displays this greeting is not important at this point,just pretend it does something really cool.) The parameter isdefined in the HTML document like this:<BLOCKQUOTE><PRE><PARAM NAME=greeting VALUE="All Web Surfers Welcome!"></PRE></BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -