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

📄 tij316.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
(Web browser and add-ins) can be specified and/or controlled. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1921" title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>Because applets are automatically integrated with HTML, you have a built-in platform-independent documentation system to support the applet. It&#146;s an interesting twist, since we&#146;re used to having the documentation part of the program rather than vice versa. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1922" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775866"></a><a name="Heading18495"></a>Application
frameworks</h3>
<p>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="Index1666"></a><a name="Index1667"></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&#146;s default control mechanism will call your overridden methods at the appropriate time. An application framework is a good example of &#147;separating the things that change from the things that stay the same,&#148; since it attempts to localize all the unique parts of a program in the overridden methods.<sup><a name="fnB76" href="#fn76">[76]</a></sup> <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1923" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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: <br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Method</b><br></p>
</th>
<th width="383.999904" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Operation</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>init(&#160;)</b><br></p>
</td>
<td width="383.999904" colspan="1" rowspan="1" valign="top">
<p class="Table">Automatically called to perform first-time initialization of the applet, including component layout. You&#146;ll always override this method.<br></p>
</td>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>start(&#160;)</b><br></p>
</td>
<td width="383.999904" colspan="1" rowspan="1" valign="top">
<p class="Table">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>.<br></p>
</td>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>stop(&#160;)</b><br></p>
</td>
<td width="383.999904" colspan="1" rowspan="1" valign="top">
<p class="Table">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>.<br></p>
</td>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>destroy(&#160;)</b><br></p>
</td>
<td width="383.999904" colspan="1" rowspan="1" valign="top">
<p class="Table">Called when the applet is being unloaded from the page to perform final release of resources when the applet is no longer used<br></p>
</td>
</tr>
</table></div>
<p class="TableFollower">With this information you are ready to create a simple applet:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14: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><p><br></p>
<p>Note that applets are not required to have a <b>main(&#160;)</b>. That&#146;s all wired into the application framework; you put any startup code in <b>init(&#160;)</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1924" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In this program, the only activity is putting a text label on the applet, via the <a name="Index1668"></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 &#147;<b>J</b>&#148; used with Swing components). The constructor for this class takes a <b>String</b> and uses it to create the label. In the preceding program this label is placed on the form. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1925" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1669"></a>The <b>init(&#160;)</b> method is responsible for putting all the components on the form using the <b>add(&#160;)</b> method. You might think that you ought to be able to simply call <b>add(&#160;)</b> by itself, and in fact that&#146;s the way it used to be in the old AWT. However, Swing requires that you add all components to the &#147;content pane&#148; of a form, so you must call <b>getContentPane(&#160;)</b> as part of the <b>add(&#160;)</b> process. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1926" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775867"></a><a name="Heading18524"></a>Running applets inside a
Web browser</h3>
<p>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 applet inside a Web page, you put a special tag inside the HTML source for that Web page<a name="Index1670"></a><a name="Index1671"></a><sup><a name="fnB77" href="#fn77">[77]</a></sup> to tell the page how to load and run the applet. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1927" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>&lt;applet code=Applet1 width=100 height=50&gt;
&lt;/applet&gt;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Then along came the browser and language wars, and we (programmers and end users alike) lost. After awhile, Sun 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&#146;s extension mechanism. By using the extension mechanism (which a browser vendor cannot disable&#151;in an attempt to gain competitive advantage&#151;without breaking all the third-party extensions), Sun guarantees that Java cannot be shut out of the Web browser by an antagonistic vendor. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1928" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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, but you can automatically generate the necessary tags with the <b>HTMLconverter</b> tool that comes with the JDK download. Here&#146;s what the simplest resulting HTML page looks like for <b>Applet1</b> after running <b>HTMLconverter</b> on the preceding applet tag:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>&lt;!--<font color=#004488>"CONVERTED_APPLET"</font>--&gt;
&lt;!-- HTML CONVERTER --&gt;
&lt;OBJECT 
    classid = <font color=#004488>"clsid:CAFEEFAC-0014-0001-0000-ABCDEFFEDCBA"</font>
    codebase = <font color=#004488>"http:</font><font color=#004488>/</font><font color=#004488>/java.sun.com</font><font color=#004488>/products</font><font color=#004488>/plugin</font><font color=#004488>/autodl</font><font color=#004488>/jinstall-1_4_1-windows-i586.cab#Version=1,4,1,0"</font>
    WIDTH = 100 HEIGHT = 50 &gt;
    &lt;PARAM NAME = CODE VALUE = Applet1 &gt;
    &lt;PARAM NAME = <font color=#004488>"type"</font> VALUE = <font color=#004488>"application</font><font color=#004488>/x-java-applet;jpi-version=1.4.1"</font>&gt;
    &lt;PARAM NAME = <font color=#004488>"scriptable"</font> VALUE = <font color=#004488>"false"</font>&gt;
    &lt;COMMENT&gt;
      &lt;EMBED 
          type = <font color=#004488>"application</font><font color=#004488>/x-java-applet;jpi-version=1.4.1"</font> 
          CODE = Applet1
          WIDTH = 100
          HEIGHT = 50  
          scriptable = <font color=#0000ff>false</font> 
          pluginspage = <font color=#004488>"http:</font><font color=#004488>/</font><font color=#004488>/java.sun.com</font><font color=#004488>/products</font><font color=#004488>/plugin</font><font color=#004488>/index.html#download"</font>&gt;
          &lt;NOEMBED&gt;
          &lt;/NOEMBED&gt;
      &lt;/EMBED&gt;
    &lt;/COMMENT&gt;
&lt;/OBJECT&gt;
&lt;!--
&lt;APPLET CODE = Applet1 WIDTH = 100 HEIGHT = 50&gt;
&lt;/APPLET&gt;
--&gt;
&lt;!--<font color=#004488>"END_CONVERTED_APPLET"</font>--&gt;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Some of these lines were too long and had to be wrapped to fit on the page. The code in this book&#146;s source code (downloadable from <i>www.BruceEckel.com</i>) will work without having to worry about correcting line wraps. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1929" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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="Index1672"></a><a name="Index1673"></a><b>codebase</b>), alignment information (<a name="Index1674"></a><a name="Index1675"></a><b>align</b>), a special identifier that makes it possible for applets to communicate with each other (<a name="Index1676"></a><a name="Index1677"></a><b>name</b>), and applet parameters to provide information that the applet can retrieve. Parameters are in the form:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>&lt;param name=<font color=#004488>"identifier"</font> value = <font color=#004488>"information"</font>&gt;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>and there can be as many as you want. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1930" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The source code package for this book (freely downloadable at <i>www.BruceEckel.com</i>)<i> </i>provides an HTML page for each of the applets in this book, and thus many examples of the applet tag, all driven from the <b>index.html</b> file corresponding to this chapter&#146;s source code. You can find a full and current description of the details of placing applets in Web pages at <i>java.sun.com</i>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1931" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>

⌨️ 快捷键说明

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