📄 ch2.htm
字号:
<TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>To load an HTML document into Netscape Navigator 2.0, select the File, Open File command or press Ctrl+O. Then select the file in the dialog box that appears.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="AppletParameters">Applet Parameters</A></H3><P>As you know, many Java applets are configurable, meaning thatthe applet user can specify certain values that the applet willuse when it starts. A good example is the BarChart applet yousaw earlier in this chapter. When you need to specify parametersfor an applet, you use the <TT><param></TT> tag. The <TT><param></TT>tags, one for each parameter you want to set, are placed afterthe starting <TT><applet></TT> tag and before the ending<TT></applet></TT> tag. For example, Listing 2.5 shows parametersbeing set for the BarChart applet. Figure 2.8 shows the resultantbar chart. As you can see from the listing, each parameter hastwo parts, the parameter name and the value to which it shouldbe set. You'll learn more about applet parameters in <A HREF="ch26.htm" >Chapter 26</A>,"Configurable Applets."<P><A HREF="f2-8.gif"><B> Figure 2.8 : </B><I>This is the bar chart created by the parameters in Listing 2.5.</I></A><P><HR><BLOCKQUOTE><B>Listing 2.5 LST2_5.TXT: Using Parameters with Applets.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><applet code="Chart.class" width=251 height=125><param name=title value="Sales"><param name=orientation value="vertical"><param name=scale value="5"><param name=columns value="3"><param name=c1_style value="solid"><param name=c1 value="10"><param name=c1_color value="blue"><param name=c1_label value="Jan"><param name=c2_style value="solid"><param name=c2 value="12"><param name=c2_color value="green"><param name=c2_label value="Feb"><param name=c3_style value="solid"><param name=c3 value="15"><param name=c3_color value="red"><param name=c3_label value="Mar"></applet></PRE></BLOCKQUOTE><HR><H3><A NAME="NonJavaBrowsers">Non-Java Browsers</A></H3><P>You may wonder what happens when a browser that's not Java-compatiblefinds an applet in an HTML document. In this case, as is standardbehavior for browsers, the non-Java browser simply ignores thetags it doesn't recognize. However, you may want to provide amore user-friendly response to users who are trying to view yourapplets with non-Java browsers. You can do this easily by placingalternate content right before the ending <TT></applet></TT>tag. Listing 2.6, for example, shows the HTML script for runningthe TicTacToe applet with alternate content for browsers thatdon't support Java.<HR><BLOCKQUOTE><B>Listing 2.6 LST2_6.TXT: Supplying Alternate Contentfor TicTacToe.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><applet code=TicTacToe.class width=120 height=120><b>If you had a Java-compatible browser,you'd be playing TicTacToe right now!</b></applet></PRE></BLOCKQUOTE><HR><P>The alternate content you provide can comprise any standard HTMLcommands and is ignored by Java-compatible browsers. That is,the alternate content appears only in non-Java browsers.<H3><A NAME="ExampleAJavaPoweredHomePage">Example: A Java-Powered Home Page</A></H3><P>In the previous section, you saw a sample HTML document that containsan applet. You also saw what this document looks like when loadedinto Netscape Navigator 2.0. Now you'll use what you've learnedto create your own "appletized" home page for NetscapeNavigator. To do this, you must first write your home page's HTMLfile. Then you must set Netscape Navigator's home-page property,which determines the document the browser displays at startup.Just follow the steps below to accomplish these tasks:<OL><LI>Create a folder called HOMEPAGE in your C: drive's root directory.<LI>Type Listing 2.7 and save it as an ASCII file called HOMEPAGE.htmLin your HOMEPAGE directory. (If you don't want to type the listing,just copy it from the CHAP02 directory of this book's CD-ROM.)<LI>Copy the entire TICTACTOE folder (not just the folder's contents)from your JAVA\DEMO folder to your HOMEPAGE folder.<LI>Start up Netscape Navigator 2.0, and click Navigator's Stopbutton to stop loading the currently set home page.<LI>Select Options, General Preferences from Navigator's menubar. The Preferences property sheet appears (Figure 2.9).<BR><A HREF="f2-9.gif"><B> Figure 2.9 : </B><I>You can set your own starting <r>home page in Navigator's Preferences property sheet.</I></A><P><LI>Enter <TT>C:\HOMEPAGE\HOMEPAGE.htmL</TT> into the Start Withtext box.<LI>Click the Home button to load your new TicTacToe home page(Figure 2.10).<BR><A HREF="f2-10.gif"><B> Figure 2.10 : </B><I>Here's your new TicTacToe home page, up and running.</I></A><P></OL><HR><BLOCKQUOTE><B>Listing 2.7 HOMEPAGE.htmL: An HTML Script for aHome Page.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>My Home Page</title><h1>The TicTacToe Home Page</h1>This may be a dumb home page, but it givesyou a chance to play the TicTacToe appletevery time you start Netscape Navigator 2.0!<h><hr><applet codebase=TicTacToe code=TicTacToe.class width=120 height=120 alt="This is the TicTacToe applet." name=TicTacToe></applet><hr></PRE></BLOCKQUOTE><HR><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When running under Netscape Navigator 2.0, the TicTacToe applet seems to respond slowly to mouse clicks at first. If the applet doesn't respond right away, try moving your mouse pointer off the applet. This trick wakes things up most of the time.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>As you learned in this chapter, Java applets are relatively easyto add to your HTML documents. However, folks without Web browsersaren't completely left out of the fun. They can create and viewapplets using the handy Appletviewer application that comes aspart of the JDK. Because HTML was designed long before there wereJava applets, Sun Microsystems had to create an extension to HTMLin order to accommodate applets in Web pages. The extension takesthe form of the <TT><applet></TT> tag, which enables youto not only provide values for an applet's attributes, but alsoto include a list of parameters and even offer alternate contentfor non-Java browsers.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>How can someone without a Java-compatible browser run applets?<LI>What are the three required attributes for the <TT><applet</TT>>tag?<LI>What does the optional <TT>codebase</TT> attribute do?<LI>Name two other optional applet attributes.<LI>Why would you use parameters with an applet?<LI>How can your applet-enhanced HTML documents accommodate non-Javabrowsers?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Use Appletviewer to view the BouncingHeads applet (or anyother demo applet in which you're interested).<LI>Change the size of the BarChart applet in the HTML code fromListing 2.5.<LI>Modify the home page you created earlier in this chapter sothat it displays both the NervousText and TicTacToe demo applets.The code for NervousText is found in the file NervousText.class.In addition to the applet's standard attributes, you'll also needto include one parameter called <TT>text</TT>, whose value isthe text you want displayed.</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 + -