📄 tij0134.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0133.html">Prev</a> | <a href="tij0135.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
The
basic applet
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Libraries
are often grouped according to their functionality. Some libraries, for
example, are used as is, off the shelf. The standard Java library
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes are examples of these. Other libraries are designed specifically as
building blocks to build other classes. A certain class of library is the <A NAME="Index1526"></A><A NAME="Index1527"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>application
framework
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
whose goal is to help you build applications by providing a class or set of
classes that produces the basic behavior that you need in every application of
a particular type. Then, to customize the behavior to your own needs you
inherit from the application class and override the methods of interest. The
application framework’s default control mechanism will call your
overridden methods at the appropriate time. An application framework is a good
example of “separating the things that change from the things that stay
the same,” since it attempts to localize all the unique parts of a
program in the overridden methods.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index1528"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Applets
are built using an application framework. You inherit from class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and override the appropriate methods. Most of the time you’ll be
concerned with only a few important methods that have to do with how the applet
is built and used on a Web page. These methods are:
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="58">
<COL width="279">
</COLGROUP>
<TR VALIGN="TOP">
<TH WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Method</FONT><P></DIV>
</TH>
<TH WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Operation</FONT><P></DIV>
</TH>
</TR>
<A NAME="Index1529"></A><A NAME="Index1530"></A><TR VALIGN="TOP">
<TD WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><P></DIV>
</TD>
<TD WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Called
when the applet is first created to perform first-time initialization of the
applet
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1531"></A><A NAME="Index1532"></A><TR VALIGN="TOP">
<TD WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start( )</B></FONT><P></DIV>
</TD>
<TD WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Called
every time the applet moves into sight on the Web browser to allow the applet
to start up its normal operations (especially those that are shut off by
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Also called after
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1533"></A><A NAME="Index1534"></A><TR VALIGN="TOP">
<TD WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><P></DIV>
</TD>
<TD WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Part
of the base class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(three levels of inheritance up).
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Called
as part of an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>update( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to perform special painting on the canvas of an applet.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1535"></A><A NAME="Index1536"></A><TR VALIGN="TOP">
<TD WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )</B></FONT><P></DIV>
</TD>
<TD WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Called
every time the applet moves out of sight on the Web browser to allow the applet
to shut off expensive operations. Also called right before
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>destroy( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1537"></A><A NAME="Index1538"></A><TR VALIGN="TOP">
<TD WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>destroy( )</B></FONT><P></DIV>
</TD>
<TD WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Called
when the applet is being unloaded from the page to perform final release of
resources when the applet is no longer used
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Consider
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method. This method is called automatically when the <A NAME="Index1539"></A><A NAME="Index1540"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(in this case, the applet) decides that it needs to update itself –
perhaps because it’s being moved back onto the screen or placed on the
screen for the first time, or perhaps some other window had been temporarily
placed over your Web browser. The applet calls its <A NAME="Index1541"></A><A NAME="Index1542"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>update( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method (defined in the base class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">which
goes about restoring everything, and as a part of that restoration calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You don’t have to override
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( ),</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
but it turns out to be an easy way to make a simple applet, so we’ll
start out with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>update( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
it hands it a handle to a <A NAME="Index1543"></A><A NAME="Index1544"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object that represents the surface on which you can paint. This is important
because you’re limited to the surface of that particular component and
thus cannot paint outside that area, which is a good thing or else you’d
be painting outside the lines. In the case of an applet, the surface is the
area inside the applet.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object also has a set of operations you can perform on it. These operations
revolve around painting on the canvas, so most of them have to do with drawing
images, shapes, arcs, etc. (Note that you can look all this up in your online
Java documentation if you’re curious.) There are some methods that allow
you to draw characters, however, and the most commonly used one is <A NAME="Index1545"></A><A NAME="Index1546"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>drawString( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
For this, you must specify the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you want to draw and its starting location on the applet’s drawing
surface. This location is given in pixels, so it will look different on
different machines, but at least it’s portable.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">With
this information you can create a simple applet:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Applet1.java</font>
<font color="#009900">// Very simple applet</font>
<font color="#0000ff">package</font> c13;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Applet1 <font color="#0000ff">extends</font> Applet {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
g.drawString("First applet", 10, 10);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that applets are not required to have a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
That’s all wired in to the application framework; you put any startup
code in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
run this program you must place it inside a Web page and view that page inside
your Java-enabled Web browser. To place an <A NAME="Index1547"></A><A NAME="Index1548"></A>applet
inside a Web page you put a special tag inside the HTML source for that Web page
</FONT><A NAME="fnB54" HREF="#fn54">[54]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to tell the page how to load and run the applet. This is the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
<A NAME="Index1549"></A><A NAME="Index1550"></A>tag,
and it looks like this for Applet1:
</FONT><P></DIV>
<font color="#990000"><PRE><applet
code=Applet1
width=200
height=200>
</applet></PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>code
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">value
gives the name of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">file
where the applet resides. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>width</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>height</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -