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

📄 ch37.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<H2><A NAME="Chapter27"><FONT SIZE=5 COLOR=#Ff0000>Chapter 27</FONT></A></H2><OL><LI>Java can load only GIF or JPEG files.<LI>The two parameters required by many of the image and soundmethods are the base URL and the relative location of the fileto load.<LI>Java recognizes only AU audio files.<LI>To display an image after it's loaded, you call the <TT>Graphics</TT>object's <TT>drawImage()</TT> method.<LI>No. You can store images and sounds in any directory relativeto the base URL.<LI>To scale an image, simply supply the <TT>drawImage()</TT>method with the width and height with which you want the imagedisplayed.<LI>To determine the normal width and height of an image, callthe <TT>Image</TT> object's <TT>getWidth()</TT> and <TT>getHeight()</TT>methods.<LI>The document base URL is the location of the HTML document,whereas the code base URL is the location of the applet's CLASSfile.<LI>You have more control over sounds with an <TT>AudioClip</TT>object because the <TT>AudioClip</TT> class provides the <TT>play()</TT>,<TT>stop()</TT>, and <TT>loop()</TT> methods, whereas the appletcan only play an audio file from beginning to end.</OL><H2><A NAME="Chapter28"><FONT SIZE=5 COLOR=#Ff0000>Chapter 28</FONT></A></H2><OL><LI>The single argument is a string containing the URL from whichthe <TT>URL</TT> object should be constructed.<LI>An <TT>AppletContext</TT> object represents the applicationcontaining the applet. This application is usually a Web browser.<LI>To obtain an <TT>AppletContext</TT> object, call the applet's<TT>getAppletContext()</TT> method.<LI>To ensure that you have a valid <TT>URL</TT> object, you handlethe exception that may be generated by the <TT>URL</TT> class.<LI>To create an exception handler, you must create <TT>try</TT>and <TT>catch</TT> program blocks.<LI>To connect to an URL, call the <TT>AppletContext</TT> object's<TT>showDocument()</TT> method.<LI>The <TT>URL</TT> class throws a MalformedURLException exceptionif the URL's string is syntactically incorrect.</OL><H2><A NAME="Chapter29"><FONT SIZE=5 COLOR=#Ff0000>Chapter 29</FONT></A></H2><OL><LI>A package is a group of related classes and interfaces.<LI>To tell Java that a class uses a particular package, you usethe <TT>import</TT> keyword followed by the full name of the package.<LI>To add a class or interface to a package, place the <TT>package</TT>keyword at the top of the class's or interface's source code followedby the name of the package to which the class or interface willbe added.<LI>To tell Java that a class implements a particular interface,add the <TT>implements</TT> keyword to the class's declarationline, followed by the name of the interface.<LI>The biggest difference between an interface and a class isthat an interface declares, but never implements, its methods.An interface's methods must be defined in any class that implementsthe interface.<LI>Interfaces and classes are similar in that they are declaredin almost exactly the same way.<LI>The complete name of a package mirrors the folder hierarchyon your hard disk into which the package files must be stored.</OL><H2><A NAME="Chapter30"><FONT SIZE=5 COLOR=#Ff0000>Chapter 30</FONT></A></H2><OL><LI>You use a <TT>try</TT> block to hold the program statementsthat may generate the exceptions you want to handle.<LI>You use a <TT>catch</TT> block to hold the program statementsthat should be executed when a particular exception is thrown.<LI>You don't have to catch all types of exceptions in your applets,because Java has default handlers for many of them.<LI>If you call a method that's declared with a <TT>throw</TT>phrase, you must handle the exception in your program.<LI>You can have as many <TT>catch</TT> blocks as you need inorder to respond to all appropriate exceptions.<LI>To pass an exception on to a calling method, you declare thecalled method with the <TT>throws</TT> phrase. Java will thenpass the exception to the calling function, where it must be handledor thrown again.<LI>Java throws exceptions that must be handled in your program,as well as throws system, runtime exceptions that you may or maynot decide to handle in your program.</OL><H2><A NAME="Chapter31"><FONT SIZE=5 COLOR=#Ff0000>Chapter 31</FONT></A></H2><OL><LI>Threads are like small programs within the main program. Justas a multitasking system can run two or more applications simultaneously,so can an applet or application run more than one thread simultaneously.<LI>All Java threads must implement the <TT>Runnable</TT> interface,which contains the <TT>run()</TT> method needed to start a thread.<LI>To start a thread, you call the thread object's <TT>start()</TT>method.<LI>When you start a thread, Java calls the thread object's <TT>run()</TT>method.<LI>You'll usually stop your applet's threads in either the <TT>stop()</TT>or <TT>destroy()</TT> method.<LI>When you suspend a thread, you put it to sleep so that itremains ready to run when you next need it. When you stop a thread,you kill the thread, meaning that you'll have to create a newthread object when you want to run the thread again.<LI>To ensure that all threads get a chance to run, you shouldcall, in any thread that takes a while to run, the <TT>sleep()</TT>or <TT>yield()</TT> method.<LI>When retaining a thread's state is not an issue, you can bothcreate and start the thread in the applet's <TT>start()</TT> method.<LI>When you want to retain a thread's state while the user switchesto and from the Web page containing your applet, you should createthe threads in <TT>init()</TT>, start or resume the threads in<TT>start()</TT>, suspend the threads in <TT>stop()</TT>, andstop the threads in <TT>destroy()</TT>.<LI>Use the <TT>synchronized</TT> keyword to mark a method ora block of code as a potential area of conflict for threads tryingto access the same resources.<LI>When a thread enters a synchronized area of code, Java givesthe thread the class's monitor object. Until the thread releasesthe monitor object, no other thread can enter the synchronizedarea. In this way, a monitor object is much like a key that unlocksa synchronized method or block of code.</OL><H2><A NAME="Chapter32"><FONT SIZE=5 COLOR=#Ff0000>Chapter 32</FONT></A></H2><OL><LI>The one method that you'll find in every application, butnot in an applet, is <TT>main()</TT>, which is where a Java application'sexecution begins.<LI>To run a Java application, you compile it using javac, andthen run the byte-code .CLASS file with the Java interpreter,java.<LI>The single parameter received by <TT>main()</TT> is the application'scommand-line parameters.<LI>The parameters come into the <TT>main()</TT> method as anarray of strings. Each parameter is in one element of the array,so you can access the elements by indexing the array.<LI>To convert an applet to an application, you must add the <TT>main()</TT>method.<LI>You must instantiate an object from a class before you cancall its methods because a class is just a template for an object,much the same way a blueprint is a template for a manufacturedobject.<LI>To create and display an application's window, you call the<TT>Frame</TT> class's constructor, and then call the <TT>Frame</TT>object's <TT>resize()</TT> and <TT>show()</TT> methods.</OL><H2><A NAME="Chapter33"><FONT SIZE=5 COLOR=#Ff0000>Chapter 33</FONT></A></H2><OL><LI>To run more than one applet at a time with Appletviewer, createan HTML document that loads the applets you want to see. Appletviewerwill load and run each applet in its own window.<LI>Although HotJava is a good example of the type of programsyou can create with Java, it hasn't been kept up to date withthe Java programming language. For this reason, HotJava cannotrun applets created with the latest version of Java. NetscapeNavigator 2.0, on the other hand, can load and run these newerapplets.<LI>A doc-comment block, which is placed at the top of a classor right before a method, provides the information javadoc needsto create useful HTML documentation files.<LI>To start a program that's been loaded into the Java debugger,type run.<LI>To document a method's return value and parameters in a doc-comment,you use the <TT>@return</TT> and <TT>@param</TT> doc tags.<LI>To start a debugging session with appletviewer, type <TT>appletviewer-debug applet.html</TT>, where <TT>applet.html</TT> is the HTMLdocument that loads the applet.<LI>To create hyperlinks in javadoc's HTML documents, add <TT>@see</TT>doc tags where appropriate in your doc-comment blocks.<LI>To start a debugging session with jdb, type <TT>jdb app.class</TT>,where <TT>app.class</TT> is the .CLASS file of the applicationyou want to debug.<LI>The javap tool is a disassembler that converts byte-code .CLASSfiles into a description of the source code.<LI>Native methods are methods written in a language other thanJava.<LI>The javah tool helps you implement native methods by creatingthe header files you need to gain access to data fields in Javaclasses from your C source code.<LI>To set a breakpoint with the debugger, type <TT>stop at class:line</TT>,where <TT>class:line</TT> is the class's name and breakpoint linenumber separated by a colon.</OL><H2><A NAME="Chapter34"><FONT SIZE=5 COLOR=#Ff0000>Chapter 34</FONT></A></H2><OL><LI>You must use the compiler to convert your source-code filesinto byte-code (.CLASS) files that Java's interpreter can readand run.<LI>If you fail to include the .java extension when specifyinga source-code file in the javac command line, the compiler willnot compile the file and will instead generate an illegal argumenterror.<LI>The options for the javac command line go between the <TT>javac</TT>command and the name of the source-code file.<LI>Yes, you can specify multiple options in one command line.Just place them one after the other between the <TT>javac</TT>command and the name of the source-code file.<LI>To set a target directory for the compiler's output files,use the <TT>-d</TT> option followed by the name of the directory.<LI>The <TT>-g</TT> command-line option instructs the compilerto add debugging information to the byte-code files it generates.<LI>The <TT>-nowarn</TT> command-line option instructs the compilerto suppress warning messages as it compiles a file. The compilerwill still generate error messages.<LI>To instruct the compiler to generate status information asit works, specify the <TT>-verbose</TT> command-line option.<LI>Byte-code files are the same format on every computer system.Each type of computer has a Java interpreter specially writtenfor it. The interpreter reads and executes the byte-code files.</OL><H2><A NAME="Chapter35"><FONT SIZE=5 COLOR=#Ff0000>Chapter 35</FONT></A></H2><OL><LI>Everything will run fine if you leave off the file extension.In fact, the interpreter won't accept the file extension and willgenerate an error if you include it.<LI>Byte-code files have the .CLASS file extension. These arethe types of files that the interpreter can load and execute.<LI>You can specify verbose output from the interpreter with the<TT>-verbose</TT> or <TT>-v</TT> command options. The second isjust a short version of the first.<LI>When writing a Java application, you must compile the sourcecode before the interpreter can load and execute the program.<LI>To get a list of options supported by the interpreter, typethe command <TT>javac -help</TT>.<LI>The interpreter is the only tool that can run a Java application.This is because the compiler produces Java byte-code files ratherthan regular executable files like most other compilers.<LI>The <TT>-checksource</TT> (or <TT>-cs</TT>) command-line optioninstructs the interpreter to recompile any source-code files thathave been changed since the last compilation.</OL><H2><A NAME="Chapter36"><FONT SIZE=5 COLOR=#Ff0000>Chapter 36</FONT></A></H2><OL><LI>The six main Java packages are <TT>lang</TT>, <TT>awt</TT>,<TT>applet</TT>, <TT>io</TT>, <TT>util</TT>, and <TT>net</TT>.<LI>The classes needed to write applications and applets thatrun in a windowed environment are found in the <TT>awt</TT> package.<LI>You would use the <TT>Math</TT> class whenever you need toperform sophisticated mathematical calculations that require functionslike sin, cosine, tangent, and so on.<LI>By creating an object of the <TT>String</TT> class, you canuse the class's many methods to manipulate the string.<LI>To join (or concatenate) two strings, call the <TT>String</TT>class's <TT>concat()</TT> method.<LI>No, you do not instantiate an object from the <TT>Math</TT>class. Because the class's methods are all static, you can callthem like this: <TT>Math.Method()</TT>, where <TT>Method</TT>is the name of the method you want to call.<LI>The data-type wrapper classes provide methods for manipulatingJava's primitive data types, such as <TT>int</TT>, <TT>float</TT>,and <TT>boolean</TT>. To use the classes, you can either callstatic methods through the class's name (such as, <TT>Integer.parseInt()</TT>)or create an object of the class and call the methods throughthat object.<LI>The <TT>System</TT> class provides two methods-<TT>getProperty()</TT>and <TT>getProperties()</TT>- that enable you to obtain informationabout the system.<LI>The <TT>io</TT> package features many classes that you canuse to perform various types of I/O operations using input andoutput streams. Usually, you create an object of the appropriateclass and then manipulate the stream through the object's methods.</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 + -