📄 tij0134.html
字号:
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">files
on the Internet (<A NAME="Index1551"></A><A NAME="Index1552"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>codebase</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
alignment information (<A NAME="Index1553"></A><A NAME="Index1554"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>align</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
a special identifier that makes it possible for applets to communicate with
each other (<A NAME="Index1555"></A><A NAME="Index1556"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>name</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),</FONT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
</FONT></TT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
applet <A NAME="Index1557"></A><A NAME="Index1558"></A>parameters
to provide information that the applet can retrieve. Parameters are in the form
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black"><param
name=identifier value = "information">
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
there can be as many as you want.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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><P></DIV>
<A NAME="Heading396"></A><H3 ALIGN=LEFT>
Testing
applets
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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’s
JDK also contains a tool called the <A NAME="Index1559"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>appletviewer</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that picks the <APPLET> tags out of the HTML file and runs the applets
without displaying the surrounding HTML text.
</FONT><A NAME="fnB55" HREF="#fn55">[55]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
As the HTML file is loaded, the browser will discover the applet tag and go
hunt for 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 specified by 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. Of course, it looks at the CLASSPATH to find out where to hunt, and if
your
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file isn’t in the CLASSPATH then it will give an error message on the
status line of the browser to the effect that it couldn’t find that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you want to try this out on your Web site things are a little more complicated.
First of all, you must
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>have</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a Web site, which for most people means a third-party <A NAME="Index1560"></A><A NAME="Index1561"></A>Internet
Service Provider (ISP) at a remote location. Then you must have a way to move
the HTML files and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files from your site to the correct directory (your WWW directory) on the ISP
machine. This is typically done with a <A NAME="Index1562"></A><A NAME="Index1563"></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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here’s
where you can get fooled. If the browser cannot locate 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 on the server, it will hunt through the <A NAME="Index1564"></A><A NAME="Index1565"></A>CLASSPATH
on your
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>local</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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’t find it. So when
you’re testing, make sure you erase the relevant
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files on your machine to be safe.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
of the most insidious places where this happened to me is when I innocently
placed an applet inside a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement was the culprit. In general, you’ll want to leave the <A NAME="Index1566"></A><A NAME="Index1567"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement out of an applet.
</FONT><a name="_Toc408018681"></a><P></DIV>
<A NAME="Heading397"></A><H3 ALIGN=LEFT>
A
more graphical example
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
example above isn’t too thrilling, so let’s try adding a slightly
more interesting graphic component:
</FONT><P></DIV>
<font color="#990000"><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.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Applet2 <font color="#0000ff">extends</font> Applet {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
g.drawString("Second applet", 10, 15);
g.draw3DRect(0, 0, 100, 20, <font color="#0000ff">true</font>);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
puts a box around the string. Of course, all the numbers are hard-coded and are
based on pixels, so on some machines the box will fit nicely around the string
and on others it will probably be off, because fonts will be different on
different machines.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are other interesting things you can find in the documentation for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphic
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class.
Any sort of graphics activity is usually entertaining, so further experiments
of this sort are left to the reader.
</FONT><a name="_Toc408018682"></a><P></DIV>
<A NAME="Heading398"></A><H3 ALIGN=LEFT>
Demonstrating
<P>the
framework methods
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
interesting to see some of the framework methods in action. (This example will
look only at <A NAME="Index1568"></A><A NAME="Index1569"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index1570"></A><A NAME="Index1571"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start( ),</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index1572"></A><A NAME="Index1573"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">because
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>destroy( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">are
self-evident and not so easily traceable.) The following applet keeps track of
the number of times these methods are called and displays them using
</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>
<font color="#990000"><PRE><font color="#009900">//: Applet3.java</font>
<font color="#009900">// Shows init(), start() and stop() activities</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Applet3 <font color="#0000ff">extends</font> Applet {
String s;
<font color="#0000ff">int</font> inits = 0;
<font color="#0000ff">int</font> starts = 0;
<font color="#0000ff">int</font> stops = 0;
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() { inits++; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> start() { starts++; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> stop() { stops++; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
s = "inits: " + inits +
", starts: " + starts +
", stops: " + stops;
g.drawString(s, 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">Normally
when you override a method you’ll want to look to see whether you need to
call the base-class version of that method, in case it does something
important. For example, with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you might need to call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>super.init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
documentation specifically states that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
do nothing, so it’s not necessary to call them here.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you experiment with this applet you’ll discover that if you minimize the
Web browser or cover it up with another window you might not get calls to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start( )</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">(This
behavior seems to vary among implementations; you might wish to contrast the
behavior of Web browsers with that of applet viewers.) The only time the calls
will occur is when you move to a different Web page and then come back to the
one containing the applet.
</FONT><a name="_Toc375545447"></a><a name="_Toc408018683"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn54" HREF="#fnB54">[54]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
It is assumed that the reader is familiar with the basics of HTML. It’s
not too hard to figure out, and there are lots of books and resources.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="fn55" HREF="#fnB55">[55]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
Because the appletviewer ignores everything but APPLET tags, you can put those
tags in the Java source file as comments:
</FONT><P><TT><FONT FACE="Courier" SIZE=2 COLOR="Black">//
<applet code=MyApplet.class width=200 height=100></applet>
</FONT></TT><P><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">This
way, you can run "
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><B>appletviewer
MyApplet.java
</B></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">"
and you don’t need to create tiny HTML files to run tests.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0133.html">Prev</a> | <a href="tij0135.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -