⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch2.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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&nbsp;2</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Running Java Applets</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#TheSampleJavaApplets">The Sample Java Applets</A><UL><LI><A HREF="#TheAppletviewerTool">The Appletviewer Tool</A><LI><A HREF="#ExampleRunningTicTacToe">Example: Running TicTacToe</A><LI><A HREF="#TheAnimatorApplet">The Animator Applet</A><LI><A HREF="#TheBarChartApplet">The BarChart Applet</A><LI><A HREF="#OtherDemoApplets">Other Demo Applets</A></UL><LI><A HREF="#AddingAppletstoanHTMLDocument">Adding Applets to an HTML Document</A><UL><LI><A HREF="#OptionalAttributesforApplets">Optional Attributes for Applets</A><LI><A HREF="#AppletParameters">Applet Parameters</A><LI><A HREF="#NonJavaBrowsers">Non-Java Browsers</A><LI><A HREF="#ExampleAJavaPoweredHomePage">Example: A Java-Powered Home Page</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>Because you're interested in writing Java applets (you boughtthis book, after all), you're probably already pretty familiarwith using HTML (Hypertext Markup Language) to create Web pages.If not, you should pick up a book on HTML and get some idea ofhow that markup language works. Even if you're an HTML expert,though, you may not have seen the HTML extension that Sun Microsystemscreated to support Java applets in Web pages. In this chapter,then, you not only get a chance to see Java applets up and running,but you also learn how to add them to your Web pages.<H2><A NAME="TheSampleJavaApplets"><FONT SIZE=5 COLOR=#Ff0000>The Sample Java Applets</FONT></A></H2><P>As you learned in <A HREF="ch1.htm" >Chapter 1</A> &quot;Java Overview,&quot; the JavaDeveloper's Kit (JDK) includes many sample applets that you cantest in your Web pages. (The HotJava browser, too, comes witha few of these sample applets.) If you installed the JDK as describedin <A HREF="ch1.htm" >Chapter 1</A> you're ready to start experimenting with Java applets.In this section, you will use the Appletviewer tool-which comeswith the JDK-to get a quick look at some applets. A followingsection, &quot;Adding Applets to an HTML Document,&quot; willshow you how to add an applet to a Web page.<H3><A NAME="TheAppletviewerTool">The Appletviewer Tool</A></H3><P>The truth is that you can write and run applets without even havinga Java-compatible browser. This is thanks to the Appletviewertool that comes as part of the JDK. Appletviewer is a Windowsapplication (unless you're using a non-Windows version of theJDK) that you run from a DOS command line. Part of the commandline is the applet that you want to run. When Appletviewer appears,the applet appears in the viewer's main window.<P>To run the Appletviewer application, first bring up an MS-DOSwindow by selecting the MS-DOS Prompt command from Programs onthe Start menu. Then, switch to the folder containing the appletyou want to run and type the command line<TT> C:\JAVA\BIN\APPLETVIEWERDOC.htmL</TT>.<P>In the preceding command line, DOC.htmL is the name of an HTMLdocument that contains the tag for the applet you want to see.<H3><A NAME="ExampleRunningTicTacToe">Example: Running TicTacToe</A></H3><P>Suppose you want to run the TicTacToe demo applet that comes withthe JDK. To do this, just follow these steps:<OL><LI>Select the Start/Programs/MS-DOS Prompt command. The DOS windowappears, as shown in Figure 2.1.<BR><A HREF="f2-1.gif"><B> Figure 2.1 : </B><I>The first step <r>in running Appletviewer is <r>to bring up the MS-DOS window.</I></A><P><LI>Change to the directory containing the TicTacToe applet (Figure2.2).<BR><A HREF="f2-2.gif"><B> Figure 2.2 : </B><I>The second step is to change to the applet's directory.</I></A><P><LI>Type the command line <TT>C:\JAVA\BIN\APPLETVIEWER EXAMPLE1.htmL.</TT>TheAppletviewer runs, loading and displaying the TicTacToe applet(Figure 2.3).<BR><A HREF="f2-3.gif"><B> Figure 2.3 : </B><I>The requested applet appears in Appletviewer's main window.</I></A><P></OL><P>Now that you have the applet started, try a few games of TicTacToeagainst the computer. To place an X, click the square you want.You'll quickly discover that the computer player is as dumb asyogurt. Let's just say that you don't have to be a rocket scientistto win (Figure 2.4).<P><A HREF="f2-4.gif"><B> Figure 2.4 : </B><I>Beating this ver-sion of TicTacToe doesn't require a degree in computer science.</I></A><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you want to avoid typing the full path name for Appletviewer every time you run it, type the command <TT>PATH=C:\JAVA\BIN </TT>at the MS-DOS prompt to add Appletviewer's directory to your path. (Of course, if you've installed the JDK somewhere else on your hard drive, you'll have to use a different path in the command.) After you type this command, MS-DOS will be able to find Appletviewer without your having to type the full path. For example, you'll be able to run TicTacToe by switching to the TicTacToe directory and simply typing <TT>APPLETVIEWER EXAMPLE1.htmL</TT>. You can also add Appletviewer's path to the PATH statement in your AUTOEXEC.BAT file and thus avoid having to type it in by hand every time you start your system and want to use Appletviewer.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="TheAnimatorApplet">The Animator Applet</A></H3><P>Another applet that demonstrates some interesting facets of Javaprogramming is the Animator applet, which not only displays variousanimation sequences, but also plays sound effects simultaneously.To run the Animator applet, switch to the C:\JAVA\DEMO\ANIMATOR

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -