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

📄 applet.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<STRONG>Note:</STRONG>To run an applet written with Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> 2 APIs in a browser, the browser mustbe enabled for the Java 2 Platform.  If your browser is not enabledfor the Java 2 Platform, you have to use appletviewer to run the appletor install <A HREF="http://java.sun.com/products/plugin/index.html">JavaPlug-in</A>. Java Plug-in lets you run applets on web pagesunder the 1.2 version of the Java VM instead of the web browser's default Java VM.<HR></BLOCKQUOTE><A NAME="struct"></A><H3>Applet Structure and Elements</H3>The Java API <CODE>Applet</CODE> class provides what you need to design the appearance and manage the behavior of an applet. This class provides a graphical user interface (GUI) component called a <CODE>Panel</CODE> and a number of methods.To create an applet, you extend (or subclass) the <CODE>Applet</CODE>class and implement the appearance and behavior you want.<P>The applet's appearance is created by drawing onto the <CODE>Panel</CODE>or by attaching other GUI components such as push buttons, scrollbars, or text areas to the <CODE>Panel</CODE>.  The applet'sbehavior is defined by implementing the methods.<H4>Extending a Class</H4><IMG SRC="./Art/hierarchy.gif" WIDTH="93" HEIGHT="255" ALT="" ALIGN=LEFT>Most classes of any complexity extend other classes. To extend anotherclass means to write a new class that can use the fields and methods defined in the class being extended. The class being extended is the parent class, and the class doing the extending is the child class.Another way to say this is the child class inherits the fields andmethods of its parent or chain of parents. Child classes either call or override inherited methods. This is called single inheritance.<P>The <CODE>SimpleApplet</CODE> class extends <CODE>Applet</CODE> class,which extends the <CODE>Panel</CODE> class, which extends the <CODE>Container</CODE> class. The <CODE>Container</CODE> class extends <CODE>Object</CODE>, which is the parent of all Java API classes.  <P>The <CODE>Applet</CODE> class provides the <CODE>init</CODE>,<CODE>start</CODE>, <CODE>stop</CODE>, <CODE>destroy</CODE>,and <CODE>paint</CODE> methods you saw in the example applet.The <CODE>SimpleApplet</CODE> class overrides these methodsto do what the <CODE>SimpleApplet</CODE> class needs them to do.The <CODE>Applet</CODE> class provides no functionality forthese methods.<P>However, the <CODE>Applet</CODE> class does provide functionalityfor the <CODE>setBackground</CODE> method,which is called in the <CODE>init</CODE> method. The call to <CODE>setBackground</CODE> is anexample of calling a method inherited from a parent class in contrastto overriding a method inherited from a parent class.<P>You might wonder why the Java language provides methods withoutimplementations.  It is to provide conventions for everyoneto use for consistency across Java APIs. If everyonewrote their own method to start an applet, for example, butgave it a different name such as <CODE>begin</CODE> or<CODE>go</CODE>, the applet code would not be interoperable withother programs and browsers, or portable across multiple platforms. For example, Netscape and Internet Explorer know howto look for the <CODE>init</CODE> and <CODE>start</CODE>methods. <H4>Behavior</H4>An applet is controlled by the software that runs it.Usually, the underlying software is a browser, but it can alsobe <CODE>appletviewer</CODE> as you saw in the example. Theunderlying software controls the applet by calling themethods the applet inherits from the <CODE>Applet</CODE> class. <P><STRONG><CODE>The init Method:</CODE></STRONG>The <CODE>init</CODE> method is called when the applet is first created and loaded by the underlying software. This method performs one-time operations the applet needs for its operation suchas creating the user interface or setting the font. In the example, the <CODE>init</CODE> method initializes the text string and sets the background color. <P><STRONG><CODE>The start Method:</CODE></STRONG>The <CODE>start</CODE> method is called when the applet is visited such as when the end user goes to a web page with an applet on it. The example prints a string to the console to tell you theapplet is starting. In a more complex applet, the <CODE>start</CODE>method would do things required at the start of the appletsuch as begin animation or play sounds.<P>After the <CODE>start</CODE> method executes, the event threadcalls the <CODE>paint</CODE> method to draw to the applet's <CODE>Panel</CODE>. A thread is a single sequential flowof control within the applet, and every applet can run inmultiple threads.  <CODE>Applet</CODE> drawing methodsare always called from a dedicated drawing and event-handlingthread.  <P><STRONG>The <CODE>stop and destroy Methods:</CODE></STRONG>The <CODE>stop</CODE> method stops the applet whenthe applet is no longer on the screen such as when the end usergoes to another web page. The example prints a string tothe console to tell you the applet is stopping. In a morecomplex applet, this method should do things likestop animation or sounds.<P>The <CODE>destroy</CODE> method is called when the browser exits.Your applet should implement this method to do final cleanupsuch as stop live threads.<H4>Appearance</H4>The <CODE>Panel</CODE> provided in the <CODE>Applet</CODE> class inherits a <CODE>paint</CODE> method from its parent <CODE>Container</CODE> class. To draw something onto the Applet's <CODE>Panel</CODE>, you implement the <CODE>paint</CODE> method to do the drawing. <P>The <CODE>Graphics</CODE> object passed to the <CODE>paint</CODE>method defines a <I>graphics context</I> for drawing on the<CODE>Panel</CODE>. The <CODE>Graphics</CODE> object has methodsfor graphical operations such as setting drawing colors, and drawing graphics, images, and text.<P>The <CODE>paint</CODE> method for the <CODE>SimpleApplet</CODE>draws the <I>I'm a simple applet</I> string in red inside a blue rectangle.</FONT><PRE>   public void paint(Graphics g){        System.out.println(&quot;Paint&quot;);//Set drawing color to blue        g.setColor(Color.blue);//Specify the x, y, width and height for a rectangle        g.drawRect(0, 0,                   getSize().width -1,                   getSize().height -1);//Set drawing color to red        g.setColor(Color.red);//Draw the text string at the (15, 25) x-y location        g.drawString(text, 15, 25);   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="package"></A><H3>Packages</H3>The applet code also has three  <CODE>import</CODE> statements at the top.Applications of any size and all applets use <CODE>import</CODE>statements to access ready-made Java API classes in <I>packages</I>. This is true whether the Java API classes come in the Java platform download,from a third-party, or are classes you write yourself and store ina directory separate from the program. At compile time, a program uses <CODE>import</CODE> statements to locate and referencecompiled Java API classes stored in packages elsewhere on the local or networked system. A compiled class in one package can have the samename as a compiled class in another package. The package name differentiates the two classes.  <P>The examples in Lessons 1 and 2 did not need a package declaration tocall the <CODE>System.out.println</CODE> Java API class because the <CODE>System</CODE> class is in the <CODE>java.lang</CODE>package that is included by default. You never needan <CODE>import java.lang.*</CODE> statement to use the compiled classes in that package.<A NAME="more"></A><H3>More Information</H3>You can find more information on applets in the<A HREF="http://java.sun.com/docs/books/tutorial/applet/">WritingApplets</A> trail in <A HREF="http://java.sun.com/docs/books/tutorial/">The JavaTutorial</A>.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">     <TR>    <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>	    <CENTER>    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">    [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>    <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> |     <A HREF="/developer/index.html">Developer Connection</A> |     <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> |     <A HREF="/developer/support/index.html">Online Support</A><BR>    <A HREF="/developer/community/index.html">Community Discussion</A> |    <A HREF="http://java.sun.com/industry/">Industry News</A> |     <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |     <A HREF="http://java.sun.com/casestudies">Case Studies</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>  <TR>    <TD ALIGN="CENTER">    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -     <A HREF="http://java.sun.com/applets/">Applets</A> -     <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -     <A HREF="http://java.sun.com/jobs/">Employment</A> -     <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> -     <A HREF="http://java.sun.com/javastore/">Java Store</A> -    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>    </FONT>    </TD>  </TR>  <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <a href="/siteinfo/faq.html">FAQ</a> |    <a href="/feedback/index.html">Feedback</a> |     <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |     <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD>    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">      <TR>        <TD WIDTH="50%">        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        For more information on Java technology<BR>        and other software from Sun Microsystems, call:<BR>        </FONT>        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">        (800) 786-7638<BR></FONT>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Outside the U.S. and Canada, dial your country's         <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>        </FONT>        </TD>        <TD ALIGN="RIGHT" WIDTH="50%">        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Copyright &copy; 1995-2000        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>        All Rights Reserved.         <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>.         <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.        </FONT>        </TD>      </TR>    </TABLE>	    </TD>  </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>

⌨️ 快捷键说明

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