📄 chap13.htm
字号:
<!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:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:22
Translation Platform:Win32
Number of Output files:23
This File:Chap13.htm
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 & Applets</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README.txt">Viewing Hints</a> ]
[ <a href="RevHist.htm">Revision History</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> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 2nd edition, Revision 12</FONT></H2>
<H3><FONT FACE="Verdana">©2000 by Bruce Eckel</FONT></H3>
<FONT FACE="Verdana" size = "-1">
[ <a href="Chap12.htm">Previous Chapter</a> ]
[ <a href="SimpCont.htm">Short TOC</a> ]
[ <a href="Contents.htm">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Chap14.htm">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><A NAME="Chapter_14"></A><A NAME="Chapter_15"></A><A NAME="_Toc375545444"></A><A NAME="_Toc477690733"></A><A NAME="_Toc481064781"></A><A NAME="Heading414"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
13: Creating Windows <BR>& Applets</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER13_I0>
A
fundamental design guideline is “make simple things easy, and difficult
things possible.”</FONT><SUP><FONT FACE="Georgia" SIZE=2>
</FONT></SUP><A NAME="fnB61" HREF="#fn61">[61]</A><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
<A NAME="Index1546"></A><A NAME="Index1547"></A><A NAME="Index1548"></A>That
goal was not achieved. Instead, the Java 1.0<A NAME="Index1549"></A>
<A NAME="Index1550"></A><I>Abstract Window Toolkit</I> (AWT) produces a GUI that
looks equally mediocre on all systems. In addition, it’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. 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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I0'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 finishes the
transformation away from the old Java 1.0 AWT by essentially replacing
everything with the <A NAME="Index1551"></A><A NAME="Index1552"></A><I>Java
Foundation Classes</I> (JFC), the GUI portion of which is called
“<A NAME="Index1553"></A>Swing.” 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 “revision 3” rule of the software industry (a product
isn’t good until revision 3) seems to hold true with programming languages
as well.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I1'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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. If for some reason you need to
use the original “old” AWT (because you’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).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I2'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Early in this chapter, you’ll see
how things are different when you want to create an applet vs. 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 either
applets or applications.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I3'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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’re willing to do the
research.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I4'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">I assume here that you have downloaded
and installed the (free) Java 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’ll want to go to those if you need more depth, or if you
want to modify the default Swing behavior.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I5'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I6>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As you learn about Swing you’ll
discover: <A NAME="Index1554"></A><A NAME="Index1555"></A>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I6'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I7>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Swing is a much better
programming model than you’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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I7'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I8>
</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">“GUI
builders” (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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I8'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I9>
</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I9'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I10>
</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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’s a big
library, but it’s designed to have appropriate complexity for the task at
hand—if something is simple, you don’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’ve got the power if you
need it.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I10'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I11>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Much of what you’ll like about
Swing could be called “orthogonality of use.” 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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I11'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For speed, all the
<A NAME="Index1556"></A>components are “lightweight,” and Swing is
written entirely in Java for portability.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER13_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index1557"></A><A NAME="Index1558"></A><FONT FACE="Georgia">Keyboard
navigation is automatic—you can run a Swing application without using the
mouse, and this doesn’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.
</backtalk:display>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -