📄 ch2.htm
字号:
folder and type the command line <TT>APPLETVIEWER EXAMPLE1.htmL</TT>.(The previous command line assumes that you've set your path tothe JAVA\BIN directory.) When you do, you see the display shownin Figure 2.5. (Yep, it's the ubiquitous Duke, waving at you fromhis very own applet.)<P><A HREF="f2-5.gif"><B> Figure 2.5 : </B><I>The Animator applet includes several animation and sound examples, including this one featuring Duke, the Java mascot.</I></A><P><P>Animator is an example of a configurable applet. That is, by modifyingthe HTML tag that loads and runs the applet, the user can displayhis or her own custom animation sequence and sound effects. You'lllearn about configurable applets in <A HREF="ch26.htm" >Chapter 26</A>, "ConfigurableApplets." For now, though, it's enough for you to know thatJava is capable of adding both animation sequences and sound effectsto your Web pages.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you'd like to see what the applet's HTML tag looks like, select Appletviewer's Applet,Tag command.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="TheBarChartApplet">The BarChart Applet</A></H3><P>BarChart, another configurable applet, is especially useful whenyou need to graphically display data values in a Web page. Tocheck out BarChart, switch to the JAVA\DEMO\BARCHART folder andtype the command line <TT>APPLETVIEWER EXAMPLE1.htmL</TT>. Whenyou do, you see the window shown in Figure 2.6.<P><A HREF="f2-6.gif"><B> Figure 2.6 : </B><I>The BarChart applet enables you to create graphs in your Web pages.</I></A><P><P>Because BarChart is configurable, you can create all sorts ofdifferent bar charts in your Web pages just by specifying differentparameters in the applet's HTML tag. As you can see, applets canbe powerful tools for creating dynamic and useful Web pages. (Tryout Appletviewer's Applet,Tag command to see the code that specifieshow the bar chart appears.)<H3><A NAME="OtherDemoApplets">Other Demo Applets</A></H3><P>The DEMO folder contains many sample applets that you can experimentwith using Appletviewer. All of the demo applets are run fromHTML documents with names such as EXAMPLE1.htmL, EXAMPLE2.htmL,and so on. All demo applets have at least the EXAMPLE1.htmL document,while others have additional examples. To run any demo applet,change to the applet's folder and type <TT>APPLETVIEWER EXAMPLE1.htmL</TT>(assuming that you've set your path to the Appletviewer application).Use the <TT>DIR </TT>command to display the contents of an applet'sdirectory in order to discover whether the applet features additionalexample HTML files.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Remember that the HotJava browser cannot load and run newer applets like those that come with the latest version of the JDK.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="AddingAppletstoanHTMLDocument"><FONT SIZE=5 COLOR=#Ff0000>Adding Applets to an HTML Document</FONT></A></H2><P>If you've created Web pages before, you know that you use HTMLto create a template for the page. The commands in the templatetell a Web browser how to display the Web page. When Sun Microsystemsdeveloped Java, they also had to come up with an extension toHTML that would enable Web pages to contain Java applets. Thatextension is the <TT><applet></TT> tag, which Sun Microsystemsdefines as shown in Listing 2.1.<HR><BLOCKQUOTE><B>Listing 2.1 LST2_1.TXT: The </B><I><applet></I><B>Tag Definition.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><applet attributes>parametersalternate-content</applet></PRE></BLOCKQUOTE><HR><P>In the preceding tag, the text in normal characters is typed literally;the text shown in italics is replaced by whatever is appropriatefor the applet you're including in the document. As you can see,the <TT><applet></TT> tag is similar to other HTML tagswith which you may be familiar. For example, the tag starts with<TT><applet attributes></TT> and ends with <TT></applet></TT>,which is not unlike the format of other HTML tags. The first andlast lines are required. Other lines in the tag are optional.<P>The attributes section of the <TT><applet></TT> tag containsimportant information about the tag, including the associated.CLASS file and the applet's width and height. The last line tellsthe browser that it has reached the end of the tag. You can loadand run the TicTacToe applet, for example, with the <TT><applet></TT>tag shown in Listing 2.2.<HR><BLOCKQUOTE><B>Listing 2.2 LST2_2.TXT: A Tag for Loading and RunningTicTacToe.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><applet code=TicTacToe.class width=120 height=120></applet></PRE></BLOCKQUOTE><HR><P>In the preceding example, the <TT>code</TT> attribute is the nameof the .CLASS file for the applet. If you remember, the .CLASSfile holds the applet's byte-code representation, which can berun by the Java interpreter. The <TT>width</TT> and <TT>height</TT>attributes control the size of the applet.<P>The TicTacToe tag above is the simplest <TT><applet></TT>tag you can write. That is, the <TT>code</TT>, <TT>width</TT>,and <TT>height</TT> attributes are all required, as is the final<TT></applet></TT> line.<H3><A NAME="OptionalAttributesforApplets">Optional Attributes for Applets</A></H3><P>There are several optional attributes you can use with the <TT><applet></TT>tag. The first is <TT>codebase</TT>, which specifies the applet'sbase folder or URL (Uniform Resource Locator). This folder orURL is used in combination with the file specified in the <TT>code</TT>attribute to find the applet's code. In the case of a folder,the <TT>codebase</TT> attribute is relative to the location ofthe HTML document containing the applet's tag. In Listing 2.2,because the <TT>codebase</TT> attribute is missing, the Web browserwill look for the applet's files in the same folder as the HTMLdocument. The <TT><applet></TT> tag in Listing 2.2 lookslike Listing 2.3 when using the <TT>codebase</TT> attribute. (Don'ttry to use run this HTML code, as it's only an example of theattribute's format and may not run.)<HR><BLOCKQUOTE><B>Listing 2.3 LST2_3.TXT: Using the </B><I>codebase</I><B>Attribute.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><applet codebase=tictactoe code=TicTacToe.class width=120 height=120></applet></PRE></BLOCKQUOTE><HR><P>The preceding tag tells the browser that the TicTacToe.class fileis located in a folder called TICTACTOE. This folder must be onthe same level in the directory tree as the HTML file. That is,if the HTML file is in the folder JAVA\DEMO, then the path forthe .CLASS file should be JAVA\DEMO\TICTACTOE\TicTacToe.class.You can also use an URL, such as <TT><A TARGET="resource window" HREF="http://www.provider.com/my_pages/tictactoe">http://www.provider.com/my_pages/tictactoe</A></TT>,for the <TT>codebase</TT> attribute. This causes the applet tobe loaded from the specified site.<P>Other optional attributes you can use with the <TT><applet></TT>tag are <TT>alt</TT>, <TT>align</TT>, <TT>name</TT>, <TT>hspace</TT>,and <TT>vspace</TT>. The <TT>alt</TT> attribute enables you tospecify text that will be displayed by text-only browsers, whereasthe <TT>name</TT> attribute gives the applet a symbolic name that'sused to reference the applet (used when you need to communicatebetween applets).<P>The <TT>align</TT>, <TT>hspace</TT>, and <TT>vspace</TT> attributesall work together to position the applet within the text flowof the HTML document. These attributes work exactly as they dowith the <TT><img></TT> tag that's used to display imagesin Web pages. The <TT>align</TT> attribute can be one of thesevalues: <TT>left</TT>, <TT>right</TT>, <TT>middle</TT>, <TT>absmiddle</TT>,<TT>bottom</TT>, <TT>absbottom</TT>, <TT>baseline</TT>, <TT>top</TT>,or <TT>texttop</TT>. The <TT>hspace</TT> and <TT>vspace</TT> attributescontrol the amount of white space around the applet when <TT>align</TT>is set to <TT>left</TT> or <TT>right</TT>.<P>Listing 2.4 shows the script for a simple Web page using the <TT><applet></TT>tag. Figure 2.7 shows Netscape Navigator 2.0 displaying the page.<P><A HREF="f2-7.gif"><B> Figure 2.7 : </B><I>This is the Web page created by Listing 2.4.</I></A><P><HR><BLOCKQUOTE><B>Listing 2.4 LST2_4.TXT: A Simple HTML Document Usingthe </B><I><applet><B> </B></I><B>Tag.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>TicTacToe</title><hr>This is a bunch of text whose sole purpose is to demonstratethe placement<applet codebase=TicTacToe code=TicTacToe.class width=120 height=120 alt="This is the TicTacToe applet." name=TicTacToe align=middle></applet> of the TicTacToe applet within the text flow of an HTML document.<hr></PRE></BLOCKQUOTE><HR><P><CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -