📄 ch15.htm
字号:
name="MyApplet"></applet></PRE></BLOCKQUOTE><HR><H2><A NAME="TheFiveStagesofanAppletsLifeCycle"><FONT SIZE=5 COLOR=#Ff0000>The Five Stages of an Applet's Life Cycle</FONT></A></H2><P>Every Java applet you create inherits a set of default behaviorsfrom the <TT>Applet</TT> class. In most cases, these default behaviorsdo nothing, unless you override some of <TT>Applet</TT>'s methodsin order to extend the applet's basic functionality. However,although a simple applet like <TT>MyApplet</TT> in Listing 15.1doesn't seem to do much, a lot is going on in the background.Some of this activity is important to your understanding of applets,and some of it can stay out of sight and out of mind.<P>Part of what goes on in a simple applet is the execution of theapplet's life cycle. There are five parts to this cycle, eachof which has a matching method that you can override to gain accessto that cycle of the applet's life. The five stages of an applet'slife cycle are listed here:<UL><LI><I>Initialization stage</I>. This is the part of an applet'slife cycle in which the applet object is created and loaded. Atthis point, it's appropriate to create objects needed by the applet,as well as initialize values that must be valid when the appletruns. The initialization stage occurs only once in the applet'slife cycle. You can tap into the initialization stage by overridingthe <TT>Applet</TT> class's <TT>init()</TT> method.<LI><I>Start stage</I>. This stage occurs when the system startsrunning the applet. The start stage can occur right after theinitialization stage or when an applet is restarted. This usuallyhappens when the user switches back to the applet's page afterviewing a different page in his or her Web browser. Unlike theinitialization stage, the start stage can occur several timesover the life of the applet. To provide your own start code, overridethe <TT>Applet</TT> class's <TT>start()</TT> method.<LI><I>Paint stage</I>. The paint stage occurs whenever the applet'sdisplay must be drawn on the screen. This happens right afterthe applet's start stage, as well as whenever the applet's displaymust be restored or changed. This can happen when the applet isexposed from underneath another window or when the program changesthe applet's display in some way and explicitly repaints the applet.Almost every applet you write will have a <TT>paint()</TT> method,which is the method you override to provide your applet with itsdisplay.<LI><I>Stop stage</I>. As you may have guessed, the stop stageis the counterpart to the start stage. Java executes this stageof the applet's life cycle when the applet is no longer visibleon the screen, such as when the user switches to a different Webpage. The default behavior for this cycle, however, is to keepthe applet running in the background. If you want to handle thestop cycle differently, you should override the <TT>Applet</TT>class's <TT>stop()</TT> method.<LI><I>Destroy stage</I>. This is the counterpart to the initializationstage and occurs when the system is about to remove the appletfrom memory. Like the initialization cycle, the destroy cycleoccurs only once. If your applet has resources that need to becleaned up before the applet exits, this is the place to do it.You tap into this cycle by overriding the <TT>Applet</TT> class's<TT>destroy()</TT> method.</UL><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>To be entirely accurate, the paint stage isn't considered an actual applet life cycle, but because an applet without a display is likely useless (not always, though), I thought I'd include the paint cycle. Truth is, the <TT>paint()</TT> method isn't even defined in the <TT>Applet</TT> class. Rather, <TT>Applet</TT> inherits <TT>paint()</TT> from the <TT>Component</TT> class, a superclass in <TT>Applet</TT>'s long chain of inheritance, which goes from <TT>Applet</TT> to <TT>Panel</TT> to <TT>Container</TT> and finally to <TT>Component</TT>.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="ExampleOverridingtheLifeCycleMethods"><FONT SIZE=5 COLOR=#Ff0000>Example: Overriding the Life Cycle Methods</FONT></A></H2><P>All this talk about life cycles and overriding methods may haveleft you a little confused as to how all this actually appliesto the applets you want to create. In previous chapters, you managedto create applets without dealing with most of this stuff becausethe <TT>Applet</TT> class, from which you derived your own appletclasses, handled the life-cycle methods in the default mannerproscribed by the Java system. If you look at Listing 15.3, you'llsee a small applet that overrides all the methods needed to providecustom behaviors for all the applet's life-cycle stages.<HR><BLOCKQUOTE><B>Listing 15.3 MyApplet2.java: Overriding the AppletLife-Cycle Methods.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.applet.*;import java.awt.*;public class MyApplet2 extends Applet{ public void init() { // Place initialization cycle code here. } public void start() { // Place start cycle code here. } public void paint(Graphics g) { // Place paint cycle code here. } public void stop() { // Place stop cycle code here. }public void destroy() { // Place destroy cycle code here. }}</PRE></BLOCKQUOTE><HR><P>Notice that in order to override the <TT>paint()</TT> method,you must import the <TT>java.awt.*</TT> libraries, which containinformation about the <TT>Graphics</TT> class. As you learnedwhen writing previous applets in this book, the <TT>Graphics</TT>class enables you to display information and graphics in an applet'sdisplay area (or canvas, as the display area is sometimes called).<P>If you look for the previous methods in Java's source code, you'lldiscover that the default implementations of <TT>init()</TT>,<TT>start()</TT>, <TT>paint()</TT>, <TT>stop()</TT>, and <TT>destroy()</TT>all do nothing at all. If you want your applet to do somethingin response to these cycles, you have to provide the code yourselfby overriding the appropriate method.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>In this chapter, you got a quick look at the basic applet andthe methods you can call at various stages during the applet'slife cycle. Over the rest of the chapters in this book, you'lluse this knowledge to develop applets that can do anything fromdisplay text in various fonts to execute animation sequences withsound.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What is the superclass for all applets?<LI>Why do applet classes need to be declared as <TT>public</TT>?<LI>What are the five life-cycle stages of an applet?<LI>How is the paint cycle different from the rest of the life-cyclestages?<LI>What's the difference between the initialize and start life-cyclestages?<LI>What do the life-cycle methods in the <TT>Applet</TT> superclassdo?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write a simple do-nothing applet called <TT>TestApplet</TT>.<LI>Override the <TT>paint()</TT> method in <TT>TestApplet</TT>so that the applet displays the text string "Hello there!."The final applet should look like Figure 15.1. You can find thesolution for these exercises in the CHAP15 folder of this book'sCD-ROM.<BR><A HREF="f15-1.gif"><B> Figure 15.1 : </B><I>This is TestApplet running under Appletviewer.</I></A><P></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 + -