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

📄 chapter13.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:25:36
Translation Platform:Win32
Number of Output files:27
This File:Chapter13.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>13: Creating windows  and applets</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
    [ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 1st edition</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;1998 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter12.html">Previous Chapter</a> ] 
    [ <a href="SimpleContents.html">Short TOC</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter14.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_15"></A><A NAME="_Toc375545444"></A><A NAME="_Toc407441457"></A><A NAME="_Toc408018677"></A><A NAME="Heading392"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
13: Creating windows <BR>and applets</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>The original design goal
of the graphical user interface (GUI) library in Java
1.0<A NAME="Index1504"></A> was to allow the programmer to build a GUI that
looks good on all platforms.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index1505"></A><A NAME="Index1506"></A><A NAME="Index1507"></A><FONT FACE="Georgia">That
goal was not achieved. Instead, the Java 1.0<A NAME="Index1508"></A>
<A NAME="Index1509"></A><A NAME="Index1510"></A><A NAME="Index1511"></A><I>Abstract
Window Toolkit</I> (AWT) produces a GUI that looks equally mediocre on all
systems. In addition it&#8217;s restrictive: you can use only four fonts and you
cannot access any of the more sophisticated GUI elements that exist in your
operating system (OS). The Java 1.0 AWT programming model is also awkward and
non-object-oriented.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Much of this situation has been
improved with the Java 1.1<A NAME="Index1512"></A> AWT event model, which takes
a much clearer, object-oriented approach, along with the introduction of Java
Beans, a component programming model that is particularly oriented toward the
easy creation of visual programming environments. Java
1.2<A NAME="Index1513"></A> finishes the transformation away from the old Java
1.0 AWT by adding the <A NAME="Index1514"></A><A NAME="Index1515"></A><I>Java
Foundation Classes</I> (JFC), the GUI portion of which is called
&#8220;<A NAME="Index1516"></A>Swing.&#8221; These are a rich set of
easy-to-use, easy-to-understand Java Beans that can be dragged and dropped (as
well as hand programmed) to create a GUI that you can (finally) be satisfied
with. The &#8220;revision 3&#8221; rule of the software industry (a product
isn&#8217;t good until revision 3) seems to hold true with programming languages
as well.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of Java&#8217;s primary 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, they are a powerful tool in supporting client-side
programming, a major issue for the Web.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Programming within an applet is so
restrictive that it&#8217;s often referred to as being &#8220;inside the
sandbox,&#8221; since you always have someone &#8211; the Java run-time security
system &#8211; watching over you. Java 1.1<A NAME="Index1517"></A> offers
digital signing for applets so you can choose to allow trusted applets to have
access to your machine. However, you can also step outside the sandbox and write
regular applications, in which case you can access the other features of your
OS. We&#8217;ve been writing regular applications all along in this book, but
they&#8217;ve been <I>console applications</I> without any graphical components.
The AWT can also be used to build GUI interfaces for regular
applications.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this chapter you&#8217;ll first
learn the use of the original &#8220;old&#8221; AWT, which is still supported
and used by many of the code examples that you will come across. Although
it&#8217;s a bit painful to learn the old AWT, it&#8217;s necessary because you
must read and maintain legacy code that uses the old AWT. Sometimes you&#8217;ll
even need to write old AWT code to support environments that haven&#8217;t
upgraded past Java 1.0<A NAME="Index1518"></A>. In the second part of the
chapter you&#8217;ll learn about the structure of the &#8220;new&#8221; AWT in
Java 1.1<A NAME="Index1519"></A> and see how much better the event model is. (If
you can, you should use the newest tools when you&#8217;re creating new
programs.) Finally, you&#8217;ll learn about the new JFC/Swing components, which
can be added to Java 1.1 as a library &#8211; this means you can use the library
without requiring a full upgrade to Java 1.2<A NAME="Index1520"></A>.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most of the examples will show the
creation of applets, not only because it&#8217;s easier but also because
that&#8217;s where the AWT&#8217;s primary usefulness might reside. In addition
you&#8217;ll see how things are different when you want to create a regular
application using the AWT, 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.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Please be aware that this is not a
comprehensive glossary of all the methods for the described classes. This
chapter will just get you started with the essentials. When you&#8217;re looking
for more sophistication, make sure you go to your information browser to look
for the classes and methods that will solve your problem. (If you&#8217;re using
a development environment your information browser might be built in; if
you&#8217;re using the Sun JDK then you use your Web browser and start in the
java root directory.) Appendix F lists other resources for learning library
details.</FONT><A NAME="_Toc375545445"></A><A NAME="_Toc408018678"></A><BR></P></DIV>
<A NAME="Heading393"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Why use the AWT?</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the problems with the
&#8220;old&#8221; AWT that you&#8217;ll learn about in this chapter is that it
is a poor example of both object-oriented design and GUI development kit design.
It throws us back into the dark ages of programming (some suggest that the
&#8216;A&#8217; in AWT stands for &#8220;awkward,&#8221; &#8220;awful,&#8221;
&#8220;abominable,&#8221; etc.). You must write lines of code to do
<I>everything</I>, including tasks that are accomplished much more easily using
<A NAME="Index1521"></A><I>resources</I> in other environments.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Many of these problems are reduced
or eliminated in <A NAME="Index1522"></A>Java 1.1 because:
<A NAME="Index1523"></A><A NAME="Index1524"></A></FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Georgia">	The new AWT in Java 1.1 is a much better
programming model and a significant step towards a better library. Java Beans is
the framework for that library.</FONT><LI><FONT FACE="Georgia">	&#8220;GUI
builders&#8221; (visual programming environments) will become <I>de rigeur</I>
for all development systems. Java Beans and the new AWT allow the GUI builder to
write code for you as you place components onto forms using graphical tools.
Other component technologies such as ActiveX will be supported in the same
fashion.</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So why learn to
use the old AWT? &#8220;Because it&#8217;s there.&#8221; In this case,
&#8220;there&#8221; has a much more ominous meaning and points to a tenet of
object-oriented library design: <I>Once you publicize a component in your
library, you can never take it out</I>. If you do, you&#8217;ll wreck
somebody&#8217;s existing code. In addition, there are many existing code
examples out there that you&#8217;ll read as you learn about Java and they all
use the old AWT.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The AWT must reach into the GUI
components of the native OS, which means that it performs a task that an applet
cannot otherwise accomplish. An untrusted applet cannot make any direct calls
into an OS because otherwise it could do bad things to the user&#8217;s machine.
The only way an untrusted applet can access important functionality such as
&#8220;draw a window on the screen&#8221; is through calls in the standard Java
library that&#8217;s been specially ported and safety checked for that machine.
The original model that Sun created is that this &#8220;trusted library&#8221;
will be provided only by the trusted vendor of the Java system in your Web
browser, and the vendor will control what goes into that library.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But what if you want to extend the
system by adding a new component that accesses functionality in the OS? Waiting
for Sun to decide that your extension should be incorporated into the standard
Java library isn&#8217;t going to solve your problem. The new model in Java 1.1
is &#8220;trusted code&#8221; or &#8220;signed code&#8221; whereby a special
server verifies that a piece of code that you download is in fact
&#8220;signed&#8221; by the stated author using a public-key encryption system.
This way, you&#8217;ll know for sure where the code comes from, that it&#8217;s
Bob&#8217;s code and not just someone pretending to be Bob. This doesn&#8217;t
prevent Bob from making mistakes or doing something malicious, but it does
prevent Bob from shirking responsibility &#8211; anonymity is what makes
computer viruses possible. A digitally signed applet &#8211; a &#8220;trusted
applet&#8221; &#8211; in Java 1.1 <I>can</I> reach into your machine and
manipulate it directly, just like any other application you get from a
&#8220;trusted&#8221; vendor and install onto your computer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But the point of all this is that
the old AWT is <I>there</I>. There will always be old AWT code floating around
and new Java programmers learning from old books will encounter that code. Also,
the old AWT is worth studying as an example of poor library design. The coverage
of the old AWT given here will be relatively painless since it won&#8217;t go
into depth and enumerate every single method and class, but instead give you an
overview of the old AWT design.
</FONT><A NAME="_Toc375545446"></A><A NAME="_Toc408018679"></A><BR></P></DIV>
<A NAME="Heading394"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The basic applet</H2></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>Vector</B> classes
are examples of these. Other libraries are designed specifically as building
blocks to build other classes. A certain class of library is the
<A NAME="Index1525"></A><A NAME="Index1526"></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&#8217;s default control mechanism will call your overridden methods at
the appropriate time. An application framework is a good example of

⌨️ 快捷键说明

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