📄 ch32.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 32</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Writing Java Applications</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#AboutJavaApplications">About Java Applications</A><LI><A HREF="#TheSimplestJavaApplication">The Simplest Java Application</A><UL><LI><A HREF="#ExampleBuildinganApplication">Example: Building an Application</A><LI><A HREF="#ExampleGettinganApplicationsArguments">Example: Getting an Application's Arguments</A></UL><LI><A HREF="#WindowedApplications">Windowed Applications</A><UL><LI><A HREF="#ExampleChanginganApplettoanApplication">Example: Changing an Applet to an Application</A><LI><A HREF="#UnderstandingtheFaceAppApplication">Understanding the FaceApp Application</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>The bulk of this book is dedicated to using Java to create appletsfor the Internet. However, Java is a full-fledged computer languagethat enables you to write complete, stand-alone applications.Although most Java users are interested in creating only applets(there are other, more powerful languages for creating applications),no introductory Java book would be complete without at least dabblinga little with Java applications. In this chapter, then, you learnthe basics of creating standalone applications with Java.<H2><A NAME="AboutJavaApplications"><FONT SIZE=5 COLOR=#Ff0000>About Java Applications</FONT></A></H2><P>If you've run the HotJava browser, you've already had experiencewith Java applications. The HotJava browser was programmed entirelyin Java and, although the browser is way out of date at the timeof this writing, it demonstrates how much you can do with Java,even when dealing with sophisticated telecommunications applications.<P>Much of what you've learned about applets can be applied towardwriting applications. After all, the language doesn't change,just the way you use it does. Of course, conversely, some of whatyou learned about applets doesn't apply to the writing of applications.For example, because Java applications aren't run "on theWeb," they don't have to deal with all the security issuesthat come up when running applets. A Java application can accessany files it needs to access, for example.<P>If you've ever programmed in C or C++, you'll discover that writingapplications in Java is similar. If you haven't programmed inthose languages, rest assured that, by this point in the book,you already have 95% of the knowledge you need in order to writestandalone Java applications.<H2><A NAME="TheSimplestJavaApplication"><FONT SIZE=5 COLOR=#Ff0000>The Simplest Java Application</FONT></A></H2><P>You can create a runnable Java application in only a few linesof code. In fact, an application requires only one method called<TT>main()</TT>.C and C++ programmers will recognize <TT>main()</TT>as being the place where applications begin their execution. Thesame is true for Java applications. Listing 32.1 shows the simplestJava application.<HR><BLOCKQUOTE><B>Listing 32.1 SimpleApp.java: The Simplest Java Application.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>class SimpleApp{ public static void main(String args[]) { System.out.println("My first Java app!"); }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Declare the <TT>SimpleApp</TT> class.<BR> Declare the app's <TT>main()</TT> method.<BR> Print a line of text on the screen.</BLOCKQUOTE><P>If you look a the first line of Listing 32.1, you'll see thateven a Java standalone application starts off as a class. In thiscase, the class has no superclass-that is, the <TT>SimpleApp</TT>class is not derived from another class (although it could havebeen). The first line of the body of the class begins the <TT>main()</TT>method, which is where all Java applications begin execution.This method's single parameter is a <TT>String</TT> array containingthe command line sent to the application when it was started.<P>Finally, the single line in <TT>main()</TT> prints a message onyour computer's screen. Because this is not a Windows application,the class contains no <TT>paint()</TT> method. Instead, you displaytext by using the <TT>println()</TT> method of the <TT>System.out</TT>package.<P>To run the Java application, you must first compile it and thenrun the byte-code file using Java's interpreter. You compile theprogram exactly as you would an applet, using javac. The followingexample describes the entire process.<H3><A NAME="ExampleBuildinganApplication">Example: Building an Application</A></H3><P>Building a Java application isn't any more difficult that buildingan applet, although the steps are slightly different. Follow thesteps below to compile and run the SimpleApp application.<OL><LI>Type Listing 32.1, and save it in your C:\CLASSES folder,under the file name SimpleApp.java. (If you don't want to type,you can copy the listing from the CHAP32 folder of this book'sCD-ROM.)<LI>Select the MS-DOS Prompt command from the Start menu's Programmenu. The DOS window appears on your screen (Figure 32.1).<BR><A HREF="f32-1.gif"><B> Figure 32.1 : </B><I>You must run SimpleApp from a DOS window.</I></A><P><LI>Type <TT>cd c:\classes</TT> to switch to your CLASSES folder.<LI>Type <TT>javac SimpleApp.java</TT> to compile the application'ssource code. After compilation, you'll have the SimpleApp.classfile in your CLASSES folder.<LI>Type <TT>java SimpleApp</TT> to run the application. The message"My first Java app!" appears on the screen (Figure 32.2).</OL><P><A HREF="f32-2.gif"><B> Figure 32.2 : </B><I>The SimpleApp applications prints a single line of text on the screen.</I></A><P><H3><A NAME="ExampleGettinganApplicationsArguments">Example: Getting an Application's Arguments</A></H3><P>You know that when you start a DOS program, you can sometimesappend parameters to the command line in order to give the programinformation it needs to start. You can do the same thing withJava applications. For example, here's how you would start SimpleAppwith two parameters:<BLOCKQUOTE><PRE>java SimpleApp param1 param2</PRE></BLOCKQUOTE><P>Of course, because SimpleApp ignores its parameters, the parametersin the command line don't mean anything. Suppose, however, youwanted to print a user-specified message a user-specified numberof times. You might, then, write the Java application shown inListing 32.2. When you run this application, add two parametersto the command line: the message to print (in quotes if the messageis more than one word) and the number of times to print the message.You'll get output like that shown in Figure 32.3.<P><A HREF="f32-3.gif"><B> Figure 32.3 : </B><I>The ArgApp application prints a message the number of times given in the command-line arguments.</I></A><P><HR><BLOCKQUOTE><B>Listing 32.2 ArgApp.java: Using Command-Line Arguments.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>class ArgApp{ String message; int count; void GetArgs(String args[]) { String s = args[1]; count = Integer.parseInt(s); message = args[0]; } void PrintMessage() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -