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

📄 chapter13.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
“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><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index1527"></A><FONT FACE="Georgia">Applets are
built using an application framework. You inherit from class <B>Applet</B> and
override the appropriate methods. Most of the time you&#8217;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:
<A NAME="Index1528"></A><A NAME="Index1529"></A><A NAME="Index1530"></A><A NAME="Index1531"></A><A NAME="Index1532"></A><A NAME="Index1533"></A><A NAME="Index1534"></A><A NAME="Index1535"></A><A NAME="Index1536"></A><A NAME="Index1537"></A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=58 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Method</FONT><BR></P></DIV>
</TH>
<TH WIDTH=279 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Operation</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>init(&#160;)</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Called when the applet is first
created to perform first-time initialization of the applet</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>start(&#160;)</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 <B>stop(&#160;)</B>). Also
called after <B>init(&#160;)</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>paint(&#160;)</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Part of the base class
<B>Component</B> (three levels of inheritance up).<B> </B>Called as part of an
<B>update(&#160;)</B> to perform special painting on the canvas of an
applet.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>stop(&#160;)</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 <B>destroy(&#160;)</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>destroy(&#160;)</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Called when the applet is being
unloaded from the page to perform final release of resources when the applet is
no longer used</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consider the <B>paint(&#160;)</B>
method. This method is called automatically when the
<A NAME="Index1538"></A><A NAME="Index1539"></A><B>Component</B> (in this case,
the applet) decides that it needs to update itself &#8211; perhaps because
it&#8217;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="Index1540"></A><A NAME="Index1541"></A><B>update(&#160;)</B> method
(defined in the base class <B>Component</B>),<B> </B>which goes about restoring
everything, and as a part of that restoration calls <B>paint(&#160;)</B>. You
don&#8217;t have to override <B>paint(&#160;),</B> but it turns out to be an
easy way to make a simple applet, so we&#8217;ll start out with
<B>paint(&#160;)</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When <B>update(&#160;)</B> calls
<B>paint(&#160;)</B> it hands it a handle to a
<A NAME="Index1542"></A><A NAME="Index1543"></A><B>Graphics</B> object that
represents the surface on which you can paint. This is important because
you&#8217;re limited to the surface of that particular component and thus cannot
paint outside that area, which is a good thing or else you&#8217;d be painting
outside the lines. In the case of an applet, the surface is the area inside the
applet.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>Graphics</B> 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&#8217;re curious.) There are some methods that allow you to draw
characters, however, and the most commonly used one is
<A NAME="Index1544"></A><A NAME="Index1545"></A><B>drawString(&#160;)</B>. For
this, you must specify the <B>String</B> you want to draw and its starting
location on the applet&#8217;s drawing surface. This location is given in
pixels, so it will look different on different machines, but at least it&#8217;s
portable.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With this information you can
create a simple applet:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><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(<font color=#004488>"First applet"</font>, 10, 10);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that applets are not required
to have a <B>main(&#160;)</B>. That&#8217;s all wired in to the application
framework; you put any startup code in <B>init(&#160;)</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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="Index1546"></A><A NAME="Index1547"></A>applet inside a Web
page you put a special tag inside the HTML source for that Web
page</FONT><A NAME="fnB51" HREF="#fn51">[51]</A><FONT FACE="Georgia"> to
tell the page how to load and run the applet. This is the <B>applet</B>
<A NAME="Index1548"></A><A NAME="Index1549"></A>tag, and it looks like this for
Applet1:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>&lt;applet
code=Applet1
width=200
height=200&gt;
&lt;/applet&gt;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>code </B>value gives the
name of the <B>.class </B>file where the applet resides. The <B>width</B> and
<B>height</B> specify the initial size of the applet (in pixels, as before).
There are other items you can place within the applet tag: a place to find other
<B>.class </B>files on the Internet
(<A NAME="Index1550"></A><A NAME="Index1551"></A><B>codebase</B>), alignment
information (<A NAME="Index1552"></A><A NAME="Index1553"></A><B>align</B>), a
special identifier that makes it possible for applets to communicate with each
other
(<A NAME="Index1554"></A><A NAME="Index1555"></A><B>name</B>),</FONT><TT><FONT FACE="Courier New">
</FONT></TT><FONT FACE="Georgia">and applet
<A NAME="Index1556"></A><A NAME="Index1557"></A>parameters to provide
information that the applet can retrieve. Parameters are in the
form</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>&lt;param name=identifier value = <font color=#004488>"information"</font>&gt;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and there can be as many as you
want.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For simple applets all you need to
do is place an applet tag in the above form inside your Web page and that will
load and run the applet.</FONT><A NAME="_Toc408018680"></A><BR></P></DIV>
<A NAME="Heading395"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Testing applets</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can perform a simple test
without any network connection by starting up your Web browser and opening the
HTML file containing the applet tag. (Sun&#8217;s JDK also contains a tool
called the <A NAME="Index1558"></A><I>appletviewer</I> that picks the
&lt;APPLET&gt; tags out of the HTML file and runs the applets without displaying
the surrounding HTML
text.</FONT><A NAME="fnB52" HREF="#fn52">[52]</A><FONT FACE="Georgia">) As
the HTML file is loaded, the browser will discover the applet tag and go hunt
for the <B>.class</B> file specified by the <B>code</B> value. Of course, it
looks at the CLASSPATH to find out where to hunt, and if your <B>.class</B> file
isn&#8217;t in the CLASSPATH then it will give an error message on the status
line of the browser to the effect that it couldn&#8217;t find that <B>.class</B>
file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you want to try this out on
your Web site things are a little more complicated. First of all, you must
<I>have</I> a Web site, which for most people means a third-party
<A NAME="Index1559"></A><A NAME="Index1560"></A>Internet Service Provider (ISP)
at a remote location. Then you must have a way to move the HTML files and the
<B>.class</B> files from your site to the correct directory (your WWW directory)
on the ISP machine. This is typically done with a
<A NAME="Index1561"></A><A NAME="Index1562"></A>File Transfer Protocol (FTP)
program, of which there are many different types freely available. So it would
seem that all you need to do is move the files to the ISP machine with FTP, then
connect to the site and HTML file using your browser; if the applet comes up and
works, then everything checks out, right?</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s where you can get
fooled. If the browser cannot locate the <B>.class</B> file on the server, it
will hunt through the <A NAME="Index1563"></A><A NAME="Index1564"></A>CLASSPATH
on your <I>local</I> machine. Thus, the applet might not be loading properly
from the server, but to you it looks fine because the browser finds it on your
machine. When someone else logs in, however, his or her browser can&#8217;t find
it. So when you&#8217;re testing, make sure you erase the relevant <B>.class</B>
files on your machine to be safe.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the most insidious places
where this happened to me is when I innocently placed an applet inside a
<B>package</B>. After uploading the HTML file and applet, it turned out that the
server path to the applet was confused because of the package name. However, my
browser found it in the local CLASSPATH. So I was the only one who could
properly load the applet. It took some time to discover that the <B>package</B>
statement was the culprit. In general, you&#8217;ll want to leave the
<A NAME="Index1565"></A><A NAME="Index1566"></A><B>package</B> statement out of
an applet.</FONT><A NAME="_Toc408018681"></A><BR></P></DIV>
<A NAME="Heading396"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
A more graphical example</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The example above isn&#8217;t too
thrilling, so let&#8217;s try adding a slightly more interesting graphic
component:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Applet2.java</font>
<font color=#009900>// Easy graphics</font>
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> java.applet.*;

⌨️ 快捷键说明

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