📄 chap13.htm
字号:
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I14>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Swing also supports a rather radical
feature called “pluggable look and feel,” which means that the
appearance of the UI can be dynamically changed to suit the expectations of
users working under different platforms and operating systems. It’s even
possible (albeit difficult) to invent your own look and feel.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I15>
</FONT><A NAME="_Toc375545446"></A><A NAME="_Toc481064782"></A><BR></P></DIV>
<A NAME="Heading415"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The basic applet<BR><A NAME="Index1559"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of Java’s design goals is to
create <I>applets</I>, which are little programs that run inside a Web browser.
Because they must be safe, applets are limited in what they can accomplish.
However, applets are a powerful tool that support client-side programming, a
major issue for the Web.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I15'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I16>
</FONT><A NAME="_Toc375545464"></A><A NAME="_Toc481064783"></A><BR></P></DIV>
<A NAME="Heading416"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Applet
restrictions<BR><A NAME="Index1560"></A><A NAME="Index1561"></A><A NAME="Index1562"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Programming within an applet is so
restrictive that it’s often referred to as being “inside the
sandbox,” since you always have someone—that is, the Java run-time
security system—watching over you.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I16'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, you can also step outside the
sandbox and write regular applications rather than applets, in which case you
can access the other features of your OS. We’ve been writing regular
applications all along in this book, but they’ve been <I>console
applications</I> without any graphical components. Swing can also be used to
build GUI interfaces for regular applications.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I17'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I18>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can generally answer the question of
what an applet is able to do by looking at what it is <I>supposed</I> to do:
extend the functionality of a Web page in a browser. Since, as a Net surfer, you
never really know if a Web page is from a friendly place or not, you want any
code that it runs to be safe. So the biggest restrictions you’ll notice
are probably: <A NAME="Index1563"></A>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I18'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I19>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"><I> </I></FONT><FONT FACE="Georgia"><I>An applet
can’t touch the local disk</I>. This means writing <I>or</I> reading,
since you wouldn’t want an applet to read and transmit private information
over the Internet without your permission. Writing is prevented, of course,
since that would be an open invitation to a virus. Java offers <I>digital
signing</I> for applets. Many applet restrictions are relaxed when you choose to
allow <I>trusted applets</I> (those signed by a trusted source) to have access
to your machine.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I19'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I20>
</FONT><LI><FONT FACE="Verdana"><I> </I></FONT><FONT FACE="Georgia"><I>Applets
can take longer to display, </I>since you must download the whole thing every
time, including a separate server hit for each different class. Your browser can
cache the applet, but there are no guarantees. Because of this, you should
always package your applets in a JAR (Java ARchive) file that combines all the
applet components (including other <B>.class</B> files as well as images and
sounds) together into a single compressed file that can be downloaded in a
single server transaction. “Digital signing” is available for each
individual entry in the JAR file.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I20'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I21>
</FONT><A NAME="_Toc375545465"></A><A NAME="_Toc481064784"></A></OL><A NAME="Heading417"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Applet advantages</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you can live within the restrictions,
applets have definite advantages, especially when building
<A NAME="Index1564"></A>client/server or other networked applications:
<A NAME="Index1565"></A><A NAME="Index1566"></A>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I21'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I22>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"><I> </I></FONT><FONT FACE="Georgia"><I>There is no
installation issue.</I> An applet has true platform independence (including the
ability to easily play audio files, etc.) so you don’t need to make any
changes in your code for different platforms nor does anyone have to perform any
“tweaking” on installation. In fact, installation is automatic every
time the user loads the Web page that contains applets, so updates happen
silently and automatically. In traditional client/server systems, building and
installing a new version of the client software is often a nightmare.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I22'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I23>
</FONT><LI><FONT FACE="Verdana"><I> </I></FONT><FONT FACE="Georgia"><I>You
don’t have to worry about bad code causing damage to someone’s
system,</I> because of the security built into the core Java language and applet
structure. This, along with the previous point, makes Java popular for so-called
<I>intranet</I> client/server applications that live only within a company or
restricted arena of operation where the user environment (Web browser and
add-ins) can be specified and/or controlled.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I23'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I24>
</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because
applets are automatically integrated with HTML, you have a built-in
platform-independent documentation system to support the applet. It’s an
interesting twist, since we’re used to having the documentation part of
the program rather than vice versa.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I24'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I25>
</FONT><A NAME="_Toc481064785"></A><BR></P></DIV>
<A NAME="Heading418"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Application frameworks</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Libraries are often grouped according to
their functionality. Some libraries, for example, are used as is, off the shelf.
The standard Java library <B>String</B> and <B>ArrayList</B> classes are
examples of these. Other libraries are designed specifically as building blocks
to create other classes. A certain category of library is the
<A NAME="Index1567"></A><A NAME="Index1568"></A><I>application framework</I>,
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><A NAME="fnB62" HREF="#fn62">[62]</A><FONT FACE="Georgia">.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I25'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Applets are built using an application
framework. You inherit from class <B>JApplet</B> and override the appropriate
methods. There are a few methods that control the creation and execution of an
applet on a Web page: </FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=72 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Method</B></FONT><BR></P></DIV>
</TH>
<TH WIDTH=288 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Operation</B></FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>init( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Automatically called to perform
first-time initialization of the applet, including component layout.
You’ll always override this method.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>start( )</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( )</B>). Also called after
<B>init( )</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>stop( )</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( )</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>destroy( )</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">With this information you are ready to
create a simple applet:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Applet1.java</font>
<font color=#009900>// Very simple 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> Applet1 <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">Note that applets are not required to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -