📄 chap13.htm
字号:
have a <B>main( )</B>. That’s all wired into the application
framework; you put any startup code in <B>init( )</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I26'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this program, the only activity is
putting a text label on the applet, via the
<A NAME="Index1569"></A><B>JLabel</B> class (the old AWT appropriated the name
<B>Label</B> as well as other names of components, so you will often see a
leading “<B>J</B>” used with Swing components). The constructor for
this class takes a <B>String</B> and uses it to create the label. In the above
program this label is placed on the form.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I27'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I28>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index1570"></A><FONT FACE="Georgia">The
<B>init( )</B> method is responsible for putting all the components on the
form using the <B>add( )</B> method. You might think that you ought to be
able to simply call <B>add( )</B> by itself, and in fact that’s the
way it used to be in the old AWT. However, Swing requires you to add all
components to the “content pane” of a form, and so you must call
<B>getContentPane( )</B> as part of the <B>add( )</B> process.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I28'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I29>
</FONT><A NAME="_Toc481064786"></A><BR></P></DIV>
<A NAME="Heading419"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Running applets inside a Web browser</H3></FONT>
<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="Index1571"></A><A NAME="Index1572"></A>applet inside a Web
page you put a special tag inside the HTML source for that Web
page</FONT><A NAME="fnB63" HREF="#fn63">[63]</A><FONT FACE="Georgia"> to tell
the page how to load and run the applet.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I29'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I30>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This process used to be very simple, when
Java itself was simple and everyone was on the same bandwagon and incorporated
the same Java support inside their Web browsers. Then you might have been able
to get away with a very simple bit of HTML inside your Web page, like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><applet code=Applet1 width=100 height=50>
</applet></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Then along came the browser and language
wars, and we (programmers and end users alike) lost. After awhile, JavaSoft
realized that we could no longer expect browsers to support the correct flavor
of Java, and the only solution was to provide some kind of add-on that would
conform to a browser’s extension mechanism. By using the extension
mechanism (which a browser vendor cannot disable—in an attempt to gain
competitive advantage—without breaking all the third-party extensions),
JavaSoft guarantees that Java cannot be shut out of the Web browser by an
antagonistic vendor.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I30'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I31>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With Internet Explorer, the extension
mechanism is the ActiveX control, and with Netscape it is the plug-in. In your
HTML code, you must provide tags to support both. Here’s what the simplest
resulting HTML page looks like for
<B>Applet1</B>:</FONT><A NAME="fnB64" HREF="#fn64">[64]</A><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>//:! c13:Applet1.html
<html><head><title>Applet1</title></head><hr>
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="100" height="50" align="baseline" codebase="http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0">
<PARAM NAME="code" VALUE="Applet1.class">
<PARAM NAME="codebase" VALUE=".">
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2">
<COMMENT>
<EMBED type=
"application/x-java-applet;version=1.2.2"
width="200" height="200" align="baseline"
code="Applet1.class" codebase="."
pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html">
<NOEMBED>
</COMMENT>
No Java 2 support for APPLET!!
</NOEMBED>
</EMBED>
</OBJECT>
<hr></body></html>
///:~</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some of these lines were too long and had
to be wrapped to fit on the page. The code in this book’s source code (on
the book’s CD ROM, and downloadable from <I>www.BruceEckel.com</I>) will
work without having to worry about correcting line wraps.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I31'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I32>
</FONT><BR></P></DIV>
<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="Index1573"></A><A NAME="Index1574"></A><B>codebase</B>), alignment
information (<A NAME="Index1575"></A><A NAME="Index1576"></A><B>align</B>), a
special identifier that makes it possible for applets to communicate with each
other (<A NAME="Index1577"></A><A NAME="Index1578"></A><B>name</B>), and applet
<A NAME="Index1579"></A><A NAME="Index1580"></A>parameters to provide
information that the applet can retrieve. Parameters are in the
form:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><param name=<font color=#004488>"identifier"</font> value = <font color=#004488>"information"</font>></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and there can be as many as you want.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I32'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I33>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The source code package for this book
provides an HTML page for each of the applets in this book, and thus many
examples of the applet tag. You can find a full and current description of the
details of placing applets in Web pages at <I>java.sun.com</I>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I33'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I34>
</FONT><A NAME="_Toc481064787"></A><BR></P></DIV>
<A NAME="Heading420"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using Appletviewer</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Sun’s JDK (freely downloadable from
<I>java.sun.com</I>) contains a tool called the
<A NAME="Index1581"></A><I>Appletviewer</I> that picks the <B><applet></B>
tags out of the HTML file and runs the applets without displaying the
surrounding HTML text. Because the Appletviewer ignores everything but APPLET
tags, you can put those tags in the Java source file as
comments:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>// <applet code=MyApplet width=200 height=100></font>
<font color=#009900>// </applet></font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This way, you can run
“<B>appletviewer MyApplet.java</B>” and you don’t need to
create tiny HTML files to run tests. For example, you can add the commented HTML
tags to <B>Applet1.java</B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Applet1b.java</font>
<font color=#009900>// Embedding the applet tag for Appletviewer.</font>
<font color=#009900>// <applet code=Applet1b width=100 height=50></font>
<font color=#009900>// </applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Applet1b <font color=#0000ff>extends</font> JApplet {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
getContentPane().add(<font color=#0000ff>new</font> JLabel(<font color=#004488>"Applet!"</font>));
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now you can invoke the applet with the
command</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>appletviewer Applet1b.java</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this book, this form will be used for
easy testing of applets. Shortly, you’ll see another coding approach which
will allow you to execute applets from the command line without the
Appletviewer.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I34'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I35>
</FONT><A NAME="_Toc481064788"></A><BR></P></DIV>
<A NAME="Heading421"></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. 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’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 <B>.class</B> file.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I35'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I36>
</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="Index1582"></A><A NAME="Index1583"></A>Internet Service Provider (ISP)
at a remote location. Since the applet is just a file or set of files, the ISP
does not have to provide any special support for Java. You must also have a way
to move the HTML files and the <B>.class</B> files from your site to the correct
directory on the ISP machine. This is typically done with a
<A NAME="Index1584"></A><A NAME="Index1585"></A>File Transfer Protocol (FTP)
program, of which there are many different types available for free or as
shareware. 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?
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I36'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I37>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here’s where you can get fooled. If
the browser on the client machine cannot locate the <B>.class</B> file on the
server, it will hunt through the
<A NAME="Index1586"></A><A NAME="Index1587"></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 during your testing process because the browser finds it on
your machine. When someone else connects, however, his or her browser
can’t find it. So when you’re testing, make sure you erase the
relevant <B>.class</B> files (or <B>.jar </B>file) on your local machine to
verify that they exist in the proper location on the server.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I37'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I38>
</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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -