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

📄 tij316.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!--
This document was converted from RTF source: 
By r2net 5.8 r2netcmd Windows 
See http://www.logictran.com
-->
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thinking in Java, 3rd ed. Revision 4.0: 14: Creating Windows  &amp; Applets</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"></head>

<body >
   <CENTER>     <a href="http://www.MindView.net">     <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>     <Font FACE="Verdana, Tahoma, Arial, Helvetica, Sans">     <h2>Thinking in Java, 3<sup>rd</sup> ed. Revision 4.0</h2>     <FONT size = "-1"><br>     [ <a href="README.txt">Viewing Hints</a> ]     [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]     [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>     [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]     [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]     [ <a href="http://www.mindview.net/Services">Consulting</a> ] <br><br>     </FONT></FONT>   </CENTER> 
<font face="Georgia"><div align="CENTER"><a href="TIJ315.htm" target="RightFrame"><img src="./prev.gif" alt="Previous " border="0"></a>
<a href="TIJ317.htm" target="RightFrame"><img src="./next.gif" alt="Next " border="0"></a>

<a href="TIJ3_t.htm"><img src="./first.gif" alt="Title Page " border="0"></a>
<a href="TIJ3_i.htm"><img src="./index.gif" alt="Index " border="0"></a>
<a href="TIJ3_c.htm"><img src="./contents.gif" alt="Contents " border="0"></a>
</div>
<hr>

<h1>
<a name="_Toc24272653"></a><a name="_Toc24775862"></a><a name="Heading18463"></a>14:
Creating Windows <br>&amp; Applets</h1>
<p class="Intro">A fundamental design guideline is &#147;make simple things easy, and difficult things possible.&#148;<sup> <a name="fnB74" href="#fn74">[74]</a></sup><br></p>
<p>The original design goal of the graphical user interface (GUI) library in Java 1.0 was to allow the programmer to build a GUI that looks good on all platforms. That goal was not achieved. Instead, the Java 1.0 <a name="Index1645"></a><a name="Index1646"></a><a name="Index1647"></a><a name="Index1648"></a><a name="Index1649"></a><i>Abstract Window Toolkit</i> (AWT) produced a GUI that looked equally mediocre on all systems. In addition, it was restrictive; you could use only four fonts and you couldn&#146;t access any of the more sophisticated GUI elements that exist in your operating system. The Java 1.0 AWT programming model is also awkward and non-object-oriented. A student in one of my seminars (who had been at Sun during the creation of Java) explained why: The original AWT had been conceptualized, designed, and implemented in a month. Certainly a marvel of productivity, and also an object lesson in why design is important. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1898" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The situation improved with the Java 1.1 AWT event model, which takes a much clearer, object-oriented approach, along with the addition of JavaBeans, a component programming model that is oriented toward the easy creation of visual programming environments. Java 2 (JDK 1.2) finished the transformation away from the old Java 1.0 AWT by essentially replacing everything with the <a name="Index1650"></a><a name="Index1651"></a><i>Java Foundation Classes</i> (JFC), the GUI portion of which is called &#147;Swing.&#148; These are a rich set of easy-to-use, easy-to-understand JavaBeans that can be dragged and dropped (as well as hand programmed) to create a GUI that you can (finally) be satisfied with. The &#147;revision 3&#148; rule of the software industry (a product isn&#146;t good until revision 3) seems to hold true with programming languages as well. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1899" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1652"></a>This chapter does not cover anything but the modern Java 2 Swing library and makes the reasonable assumption that Swing is the final destination GUI library for Java.<sup><a name="fnB75" href="#fn75">[75]</a></sup> If for some reason you need to use the original &#147;old&#148; AWT (because you&#146;re supporting old code or you have browser limitations), you can find that introduction in the first edition of this book, downloadable at <i>www.BruceEckel.com</i> (also included on the CD ROM bound with this book) Note that some AWT components remain in Java, and in some situations you must use them. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1900" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Early in this chapter, you&#146;ll see how things are different when you want to create an applet versus a regular application using Swing, and how to create programs that are both applets and applications so they can be run either inside a browser or from the command line. Almost all the GUI examples in this book will be executable as both applets and applications. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1901" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Please be aware that this is not a comprehensive glossary of either all the Swing components or all the methods for the described classes. What you see here is intended to be simple. The Swing library is vast, and the goal of this chapter is only to get you started with the essentials and comfortable with the concepts. If you need to do more, then Swing can probably give you what you want if you&#146;re willing to do the research. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1902" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>I assume here that you have downloaded and installed the JDK library documents in HTML format from <i>java.sun.com</i> and will browse the <b>javax.swing</b> classes in that documentation to see the full details and methods of the Swing library. Because of the simplicity of the Swing design, this will often be enough information to solve your problem. There are numerous (rather thick) books dedicated solely to Swing, and you&#146;ll want to go to those if you need more depth, or if you want to modify the default Swing behavior. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1903" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As you learn about Swing, you&#146;ll discover:  <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1904" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li><a name="Index1653"></a><a name="Index1654"></a>Swing is a much better
programming model than you&#146;ve probably seen in other languages and
development environments. JavaBeans (which will be introduced toward the end of
this chapter) is the framework for that library. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1905" title="Send BackTalk
Comment">Feedback</a></font></li>
<li>&#147;GUI builders&#148; (visual programming environments) are a <i>de
rigueur</i> aspect of a complete Java development environment. JavaBeans and
Swing allow the GUI builder to write code for you as you place components onto
forms using graphical tools. This not only rapidly speeds development during GUI
building, but it allows for greater experimentation and thus the ability to try
out more designs and presumably come up with a better one. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1906" title="Send BackTalk
Comment">Feedback</a></font></li>
<li>The simplicity and well-designed nature of Swing means that even if you do
use a GUI builder rather than coding by hand, the resulting code will still be
comprehensible; this solves a big problem with GUI builders from the past, which
could easily generate unreadable code. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1907" title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>Swing contains all the components that you expect to see in a modern UI: everything from buttons that contain pictures to trees and tables. It&#146;s a big library, but it&#146;s designed to have appropriate complexity for the task at hand; if something is simple, you don&#146;t have to write much code, but as you try to do more complex things, your code becomes proportionally more complex. This means an easy entry point, but you&#146;ve got the power if you need it. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1908" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Much of what you&#146;ll like about Swing could be called &#147;orthogonality of use.&#148; That is, once you pick up the general ideas about the library, you can apply them everywhere. Primarily because of the standard naming conventions, much of the time that I was writing these examples I could guess at the method names and get it right the first time without looking anything up. This is certainly the hallmark of a good library design. In addition, you can generally plug components into other components and things will work correctly. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1909" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>For speed, all the components are &#147;lightweight,&#148; and Swing is written entirely in Java for portability. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1910" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1655"></a><a name="Index1656"></a><a name="Index1657"></a>Keyboard navigation is automatic; you can run a Swing application without using the mouse, and this doesn&#146;t require any extra programming. Scrolling support is effortless; you simply wrap your component in a <b>JScrollPane</b> as you add it to your form. Features such as tool tips typically require a single line of code to use. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1911" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Swing also supports a rather radical feature called &#147;pluggable look and feel,&#148; 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&#146;s even possible (albeit difficult) to invent your own look and feel. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1912" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545446"></a><a name="_Toc24775863"></a><a name="Heading18482"></a>The
basic applet<br></h2>
<p><a name="Index1658"></a>Java has the ability 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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1913" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545464"></a><a name="_Toc24775864"></a><a name="Heading18484"></a>Applet
restrictions<br></h3>
<p><a name="Index1659"></a><a name="Index1660"></a><a name="Index1661"></a>Programming within an applet is so restrictive that it&#146;s often referred to as being &#147;inside the sandbox,&#148; since you always have someone&#151;that is, the Java run-time security system&#151;watching over you. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1914" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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&#146;ve been writing regular applications all along in this book, but they&#146;ve been <i>console applications</i> without any graphical components. Swing can be used to build GUI interfaces for regular applications. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1915" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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&#146;ll notice are probably:  <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1916" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li><a name="Index1662"></a><i></i><i>An applet can&#146;t touch the local
disk</i>. This means writing <i>or</i> reading, since you wouldn&#146;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>signed applets</i>
(those signed by a trusted source) to have access to your machine. You&#146;ll
see an example later in this chapter, as well as an example of <i>Java Web
Start</i>, a way to safely send applications to a client over the Internet.
<font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1917"
title="Send BackTalk Comment">Feedback</a></font></li>
<li><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. &#147;Digital signing&#148; is available for each
individual entry in the JAR file. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1918" title="Send BackTalk
Comment">Feedback</a></font></li></ol><h3>
<a name="_Toc375545465"></a><a name="_Toc24775865"></a><a name="Heading18490"></a>Applet
advantages</h3>
<p>If you can live within the restrictions, applets have definite advantages, especially when building client/server or other networked applications:  <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1919" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li><a name="Index1663"></a><a name="Index1664"></a><a name="Index1665"></a><i></i><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&#146;t
need to make any changes in your code for different platforms, nor does anyone
have to perform any installation &#147;tweaking.&#148; In fact, installation
is automatic every time the user loads a 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. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1920" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><i>You don&#146;t have to worry about bad code causing damage to
someone&#146;s system,</i> because of the security built into the core Java
language and applet structure. This, along with the previous point, makes Java
useful for so-called <i>intranet</i> client/server applications that live only
within a company or restricted arena of operation where the user environment

⌨️ 快捷键说明

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