Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

core java 2  volume i fundamentals
PREMIUM
Số trang
783
Kích thước
8.1 MB
Định dạng
PDF
Lượt xem
1757

core java 2 volume i fundamentals

Nội dung xem thử

Mô tả chi tiết

Core Java™ 2: Volume I–Fundamentals

Cay S. Horstmann

Gary Cornell

Publisher: Prentice Hall PTR

Fifth Edition December 01, 2000

ISBN: 0-13-089468-0, 832 pages

Ask any experienced Java programmer, Core Java delivers the real-world guidance you need

to accomplish even the most challenging tasks That’s why it’s been an international best seller

for five straight years. Core Java 2, Volume 1-Fundamentals covers the fundamentals of

Java 2 Platform Standard Edition, Version 1.3 and includes completely revised discussions of

object-oriented Java development, enhanced coverage of Swing user interface components,

and much more.

The fifth edition delivers even more of the robust, real-world programs previous editions are

famous for- updated to reflect JDK 1.3 deployment and performance enhancements.

Volume 1 includes thorough explanations of inner classes, dynamic proxy classes, exception

handling, debugging, the Java event model, Input/Output, and file management. For

experienced programmers, Core Java 2, Volume 1-Fundamentals sets the standard-again!

Table of Contents

List of Tables, Code Examples and Figures....................................................................... 1

Tables ................................................................................................................................. 1

Code Examples................................................................................................................... 1

Figures................................................................................................................................ 3

Preface................................................................................................................................... 7

To the Reader ..................................................................................................................... 7

About This Book ................................................................................................................ 8

Conventions...................................................................................................................... 10

CD-ROM.......................................................................................................................... 11

Acknowledgments............................................................................................................... 12

Chapter 1. An Introduction to Java ................................................................................. 13

Java as a Programming Tool ............................................................................................ 13

Advantages of Java........................................................................................................... 14

The Java “White Paper” Buzzwords ................................................................................ 15

Java and the Internet......................................................................................................... 22

A Short History of Java.................................................................................................... 24

Common Misconceptions About Java.............................................................................. 26

Chapter 2. The Java Programming Environment .......................................................... 30

Installing the Java Software Development Kit................................................................. 30

Development Environments............................................................................................. 34

Using the Command Line Tools ...................................................................................... 35

Using an Integrated Development Environment.............................................................. 38

Compiling and Running Programs from a Text Editor.................................................... 42

Graphical Applications..................................................................................................... 46

Applets ............................................................................................................................. 49

Chapter 3. Fundamental Programming Structures in Java .......................................... 54

A Simple Java Program.................................................................................................... 54

Comments......................................................................................................................... 57

Data Types........................................................................................................................ 58

Variables........................................................................................................................... 62

Assignments and Initializations ....................................................................................... 63

Operators .......................................................................................................................... 65

Strings............................................................................................................................... 73

Control Flow .................................................................................................................... 87

Big Numbers .................................................................................................................. 106

Arrays............................................................................................................................. 108

Chapter 4. Objects and Classes....................................................................................... 123

Introduction to Object-Oriented Programming .............................................................. 123

Using Existing Classes................................................................................................... 131

Building Your Own Classes........................................................................................... 143

Static Fields and Methods .............................................................................................. 155

Method Parameters......................................................................................................... 161

Object Construction........................................................................................................ 168

Packages......................................................................................................................... 177

Documentation Comments............................................................................................. 187

Class Design Hints ......................................................................................................... 192

Chapter 5. Inheritance..................................................................................................... 195

Extending Classes .......................................................................................................... 195

Object: The Cosmic Superclass ................................................................................... 216

The Class Class............................................................................................................. 240

Reflection ....................................................................................................................... 244

Design Hints for Inheritance .......................................................................................... 262

Chapter 6. Interfaces and Inner Classes ........................................................................ 265

Interfaces ........................................................................................................................ 265

Object Cloning ............................................................................................................... 276

Inner Classes .................................................................................................................. 282

Proxies............................................................................................................................ 299

Chapter 7. Graphics Programming................................................................................ 306

Introduction to Swing..................................................................................................... 306

Creating a Frame ............................................................................................................ 310

Frame Positioning .......................................................................................................... 314

Displaying Information in a Panel ................................................................................. 319

2D Shapes....................................................................................................................... 326

Colors ............................................................................................................................. 335

Text and Fonts................................................................................................................ 340

Images ............................................................................................................................ 351

Chapter 8. Event Handling.............................................................................................. 358

Basics of Event Handling............................................................................................... 358

The AWT Event Hierarchy ............................................................................................ 378

Semantic and Low-Level Events in the AWT ............................................................... 380

Low-Level Event Types ................................................................................................. 384

Actions ........................................................................................................................... 402

Multicasting.................................................................................................................... 411

The Event Queue............................................................................................................ 414

Chapter 9. User Interface Components with Swing...................................................... 424

The Model-View-Controller Design Pattern.................................................................. 424

An Introduction to Layout Management........................................................................ 430

Text Input ....................................................................................................................... 437

Making Choices.............................................................................................................. 464

Menus............................................................................................................................. 488

Sophisticated Layout Management ................................................................................ 512

Dialog Boxes .................................................................................................................. 540

Chapter 10. Applets.......................................................................................................... 582

Applet Basics.................................................................................................................. 582

The Applet HTML Tags and Attributes......................................................................... 600

Multimedia ..................................................................................................................... 614

The Applet Context ........................................................................................................ 617

JAR Files........................................................................................................................ 628

Chapter 11. Exceptions and Debugging ......................................................................... 640

Dealing with Errors ........................................................................................................ 640

Catching Exceptions....................................................................................................... 648

Some Tips on Using Exceptions .................................................................................... 658

Debugging Techniques................................................................................................... 661

Using a Debugger........................................................................................................... 684

Chapter 12. Streams and Files ........................................................................................ 693

Streams ........................................................................................................................... 693

The Complete Stream Zoo ............................................................................................. 696

ZIP File Streams............................................................................................................. 718

Putting Streams to Use ................................................................................................... 727

Object Streams ............................................................................................................... 741

File Management............................................................................................................ 768

Appendix Java Keywords................................................................................................ 776

Core Java™ 2: Volume I–Fundamentals

1

List of Tables, Code Examples and Figures

Tables

Table 2-1: Java directory tree

Table 3-1: Java integer types

Table 3-2: Floating-point types

Table 3-3: Special characters

Table 3-4: Operator precedence

Table 3-5: Growth of an investment at different interest rates

Table 4-1: UML notation for class relationships

Table 7-1: Standard colors

Table 7-2: System colors

Table 8-1: Event handling summary

Table 8-2: Sample cursor shapes

Table 8-3: Predefined action table names

Table 8-4: Input map conditions

Table 9-1: The accessor methods of the ButtonModel interface

Table 10-1: Applet positioning attributes

Table 10-2: Translating between APPLET and OBJECT attributes

Table 10-3: showDocument arguments

Table 10-4: jar program options

Table 11-1: Timing data

Table 11-2: HPROF options

Table 11-3: Debugging commands

Table 12-1: Basic character encodings (in rt.jar)

Table 12-2: Extended Character Encodings (in i18n.jar)

Code Examples

Example 2-1: Welcome.java

Example 2-2: ImageViewer.java

Example 2-3: WelcomeApplet.html

Example 2-4: WelcomeAppletPlugin.html

Example 2-5: WelcomeApplet.java

Example 3-1: FirstSample.java

Example 3-2: InputTest.java

Example 3-3: Retirement.java

Example 3-4: Retirement2.java

Example 3-5: LotteryOdds.java

Example 3-6: BigIntegerTest.java

Example 3-7: LotteryDrawing.java

Example 3-8: CompoundInterest.java

Example 3-9: LotteryArray.java

Example 4-1: CalendarTest.java

Example 4-2: EmployeeTest.java

Example 4-3: StaticTest.java

Example 4-4: ParamTest.java

Example 4-5: ConstructorTest.java

Core Java™ 2: Volume I–Fundamentals

2

Example 4-6: PackageTest.java

Example 4-7: Employee.java

Example 5-1: ManagerTest.java

Example 5-2: PersonTest.java

Example 5-3: EqualsTest.java

Example 5-4: ArrayListTest.java

Example 5-5: ReflectionTest.java

Example 5-6: ObjectAnalyzerTest.java

Example 5-7: ArrayGrowTest.java

Example 5-8: MethodPointerTest.java

Example 6-1: EmployeeSortTest.java

Example 6-2: TimerTest.java

Example 6-3: CloneTest.java

Example 6-4: InnerClassTest.java

Example 6-5: AnonymousInnerClassTest.java

Example 6-6: StaticInnerClassTest.java

Example 6-7: ProxyTest.java

Example 7-1: SimpleFrameTest.java

Example 7-2: CenteredFrameTest.java

Example 7-3: NotHelloWorld.java

Example 7-4: DrawTest.java

Example 7-5: FillTest.java

Example 7-6: FontTest.java

Example 7-7: ImageTest.java

Example 8-1: ButtonTest.java

Example 8-2: PlafTest.java

Example 8-3: Sketch.java

Example 8-4: MouseTest.java

Example 8-5: ActionTest.java

Example 8-6: MulticastTest.java

Example 8-7: CustomEventTest.java

Example 9-1: TextTest.java

Example 9-2: ValidationTest.java

Example 9-3: TextAreaTest.java

Example 9-4: TextEditTest.java

Example 9-5: CheckBoxTest.java

Example 9-6: RadioButtonTest.java

Example 9-7: BorderTest.java

Example 9-8: ComboBoxTest.java

Example 9-9: SliderTest.java

Example 9-10: MenuTest.java

Example 9-11: ToolBarTest.java

Example 9-12: Calculator.java

Example 9-13: BoxLayoutTest.java

Example 9-14: FontDialog.java

Example 9-15: CircleLayoutTest.java

Example 9-16: OptionDialogTest.java

Example 9-17: DialogTest.java

Example 9-18: DataExchangeTest.java

Example 9-19: FileChooserTest.java

Core Java™ 2: Volume I–Fundamentals

3

Example 9-20: ColorChooserTest.java

Example 10-1: NotHelloWorldApplet.java

Example 10-2: NotHelloWorldAppletPlugin.html

Example 10-3: Calculator.html (before processing with the HTML converter)

Example 10-4: CalculatorApplet.java

Example 10-5: PopupCalculatorApplet.java

Example 10-6: Chart.java

Example 10-7: Bookmark.html

Example 10-8: Left.html (before processing with the HTML converter)

Example 10-9: Right.html

Example 10-10: Bookmark.java

Example 10-11: AppletFrame.java

Example 10-12: CalculatorAppletApplication.java

Example 10-13: ResourceTest.html

Example 10-14: ResourceTest.java

Example 11-1: ExceptTest.java

Example 11-2: ExceptionalTest.java

Example 11-3: ConsoleWindow.java

Example 11-4: EventTracer.java

Example 11-5: EventTracerTest.java

Example 11-6: RobotTest.java

Example 11-7: WordCount.java

Example 11-8: BuggyButtonTest.java

Example 11-9: BuggyButtonFrame.java

Example 11-10: BuggyButtonPanel.java

Example 12-1: ZipTest.java

Example 12-2: DataFileTest.java

Example 12-3: RandomFileTest.java

Example 12-4: ObjectFileTest.java

Example 12-5: ObjectRefTest.java

Example 12-6: SerialCloneTest.java

Example 12-7: FindDirectories.java

Figures

Figure 1-1: The Jmol applet

Figure 2-1: Compiling and running Welcome.java

Figure 2-2: Starting Forte

Figure 2-3: The edit window of Forte

Figure 2-4: The output window of Forte

Figure 2-5: Error messages in Forte

Figure 2-6: Starting a new program in Forte

Figure 2-7: Compiling a program with Xemacs

Figure 2-8: Running a program from within Xemacs

Figure 2-9: Locating compilation errors in TextPad

Figure 2-10: Running a Java program from TextPad

Figure 2-11: Running the ImageViewer application

Figure 2-12: The WelcomeApplet applet as viewed by the applet viewer

Figure 2-13: Running the WelcomeApplet applet in a browser

Figure 3-1: Legal conversions between numeric types

Core Java™ 2: Volume I–Fundamentals

4

Figure 3-2: The three panes of the API documentation

Figure 3-3: Class description for the String class

Figure 3-4: Method summary of the String class

Figure 3-5: Detailed description of a String method

Figure 3-6: An input dialog

Figure 3-7: Flowchart for the if statement

Figure 3-8: Flowchart for the if/else statement

Figure 3-9: Flowchart for the if/else if (multiple branches)

Figure 3-10: Flowchart for the while statement

Figure 3-11: Flowchart for the do/while statement

Figure 3-12: Flowchart for the for statement

Figure 3-13: Flowchart for the switch statement

Figure 3-14: Copying an array variable

Figure 3-15: Copying values between arrays

Figure 3-16: A two-dimensional array

Figure 4-1: A class diagram

Figure 4-2: Procedural vs. OO programming

Figure 4-3: Creating a new object

Figure 4-4: Object variables that refer to the same object

Figure 4-5: Returning a reference to a mutable data field

Figure 4-6: Modifying a numeric parameter has no lasting effect

Figure 4-7: Modifying an object parameter has a lasting effect

Figure 4-8: Swapping object parameters has no lasting effect

Figure 4-9: Changing the warning string in an applet window

Figure 5-1: Employee inheritance hierarchy

Figure 5-2: Inheritance diagram for Person and its subclasses

Figure 6-1: Copying and cloning

Figure 6-2: A shallow copy

Figure 6-3: An inner class object has a reference to an outer class object

Figure 7-1: The Windows look and feel of Swing

Figure 7-2: The Motif look and feel of Swing

Figure 7-3: The Metal look and feel of Swing

Figure 7-4: The simplest visible frame

Figure 7-5: Inheritance hierarchy for the JFrame and JPanel classes

Figure 7-6: A simple graphical program

Figure 7-7: The internal structure of a Jframe

Figure 7-8: 2D rectangle classes

Figure 7-9: The bounding rectangle of an ellipse

Figure 7-10: Relationships between the shape classes

Figure 7-11: Rectangles and ellipses

Figure 7-12: Filled rectangles and ellipses

Figure 7-13: Typesetting terms illustrated

Figure 7-14: Drawing the baseline and string bounds

Figure 7-15: Window with tiled graphics image

Figure 8-1: Event notification

Figure 8-2: A panel filled with buttons

Figure 8-3: Switching the Look and Feel

Figure 8-4: A window listener

Figure 8-5: Inheritance diagram of the AWT event classes

Figure 8-6: Relationship between event sources and listeners

Core Java™ 2: Volume I–Fundamentals

5

Figure 8-7: A sketch program

Figure 8-8: A mouse test program

Figure 8-9: Buttons display the icons from the Action objects

Figure 8-10: All frames listen to the Close all command

Figure 8-11: Using custom timer events to simulate rainfall

Figure 9-1: Model and view of a text field

Figure 9-2: Two separate views of the same model

Figure 9-3: A window place

Figure 9-4: Interactions between model, view, and controller objects

Figure 9-5: A panel with three buttons

Figure 9-6: A panel with six buttons managed by a flow layout

Figure 9-7: Changing the panel size rearranges the buttons automatically

Figure 9-8: Border layout

Figure 9-9: A single button managed by a border layout

Figure 9-10: A panel placed at the south end of the frame

Figure 9-11: Text field example

Figure 9-12: A text area

Figure 9-13: Testing text editing

Figure 9-14: Check boxes

Figure 9-15: A radio button group

Figure 9-16: Testing border types

Figure 9-17: A combo box

Figure 9-18: Sliders

Figure 9-19: A menu with a submenu

Figure 9-20: Icons in menu items

Figure 9-21: A checked menu item and menu items with radio buttons

Figure 9-22: A pop-up menu

Figure 9-23: Keyboard mnemonics

Figure 9-24: Accelerators

Figure 9-25: Disabled menu items

Figure 9-26: A tool bar

Figure 9-27: Dragging the tool bar

Figure 9-28: Dragging the tool bar to another border

Figure 9-29: Detaching the tool bar

Figure 9-30: A tool tip

Figure 9-31: Inheritance hierarchy for the Component class

Figure 9-32: A calculator

Figure 9-33: Box layouts

Figure 9-34: Font dialog box

Figure 9-35: Dialog box grid used in design

Figure 9-36: Circle layout

Figure 9-37: Geometric traversal order

Figure 9-38: An option dialog

Figure 9-39: The OptionDialogTest program

Figure 9-40: An About dialog box

Figure 9-41: Password dialog box

Figure 9-42: File chooser dialog box

Figure 9-43: A file dialog with a preview accessory

Figure 9-44: The “swatches” pane of color chooser

Figure 9-45: The HSB pane of a color chooser

Core Java™ 2: Volume I–Fundamentals

6

Figure 9-46: The RGB pane of a color chooser

Figure 10-1: Selecting the Java Virtual Machine in the Java Plug-In

Figure 10-2: Applet inheritance hierarchy

Figure 10-3: Viewing an applet in the applet viewer

Figure 10-4: The Java Plug-In Control Panel

Figure 10-5: The Java Console

Figure 10-6: The Java Plug-In HTML converter

Figure 10-7: Viewing an applet in a browser

Figure 10-8: A calculator applet

Figure 10-9: A pop-up window inside a browser

Figure 10-10: Applet alignment

Figure 10-11: A chart applet

Figure 10-12: A bookmark applet

Figure 10-13: The calculator as an application

Figure 10-14: The calculator as an applet

Figure 10-15: Displaying a resource from a JAR file

Figure 11-1: Exception hierarchy in Java

Figure 11-2: A program that generates exceptions

Figure 11-3: The console window

Figure 11-4: The EventTracer class at work

Figure 11-5: A breakpoint in the Forte debugger

Figure 11-6: The breakpoint list

Figure 11-7: The Forte watch window

Figure 12-1: Input and Output stream hierarchy

Figure 12-2: Reader and Writer hierarchy

Figure 12-3: A sequence of filtered stream

Figure 12-4: The ZipTest program

Figure 12-5: Two managers can share a mutual employee

Figure 12-6: Here, Harry is saved three times

Figure 12-7: An example of object serialization

Figure 12-8: Objects saved in random order

Figure 12-9: The graphical version of the serialver program

Figure 12-10: Reading an object with fewer data fields

Figure 12-11: Reading an object with more data fields

Core Java™ 2: Volume I–Fundamentals

7

Preface

To the Reader

In late 1995, the Java programming language burst onto the Internet scene and gained instant

celebrity status. The promise of Java is that it will become the universal glue that connects

users with information, whether that information comes from Web servers, databases,

information providers, and any other imaginable source. Indeed Java is in a unique position to

fulfill this promise. It is an extremely solidly engineered language that has gained acceptance

by all major vendors, except for Microsoft. Its built-in security and safety features are

reassuring both to programmers and to the users of Java programs. Java even has built-in

support that makes advanced programming tasks, such as network programming, database

connectivity, and multithreading, straightforward.

Since then, Sun Microsystems has released four major revisions of the Java Software

Development Kit. Version 1.02, released in 1996, supported database connectivity and

distributed objects. Version 1.1, released in 1997, added a robust event model,

internationalization, and the Java Beans component model. Version 1.2, released at the end of

1998, has numerous enhancements, but one major improvement stands out: the “Swing” user

interface toolkit that finally allows programmers to write truly portable GUI applications.

Version 1.3, released in the spring of 2000, delivered many incremental improvements.

The book you have in your hand is the first volume of the fifth edition of the Core Java book.

Each time, the book followed the release of the Java development kit as quickly as possible,

and each time, we rewrote the book to take advantage of the newest Java features.

As with the previous editions of this book, we still target serious programmers who want to

put Java to work on real projects. We still guarantee no nervous text or dancing tooth-shaped

characters. We think of you, our reader, as a programmer with a solid background in a

programming language. But you do not need to know C++ or object-oriented programming.

Based on the responses we have received to the earlier editions of this book, we remain

confident that experienced Visual Basic, C, or COBOL programmers will have no trouble

with this book. (You don't even need any experience in building graphical user interfaces in

Windows, Unix, or the Macintosh.)

What we do is assume you want to:

• Write real code to solve real problems

and

• Don't like books filled with toy examples (such as kitchen appliances or fruit trees).

You will find lots of sample code on the accompanying CD that demonstrates almost every

language and library feature that we discuss. We kept the sample programs purposefully

simple to focus on the major points, but, for the most part, they aren't fake and they don't cut

corners. They should make good starting points for your own code.

Core Java™ 2: Volume I–Fundamentals

8

We assume you are willing, even eager, to learn about all the advanced features that Java puts

at your disposal. For example, we give you a detailed treatment of:

• Object-oriented programming

• Reflection and proxies

• Interfaces and inner classes

• The event listener model

• Graphical user interface design with the Swing UI toolkit

• Exception handling

• Stream input/output and object serialization

We still don't spend much time on the fun but less serious kind of Java programs whose sole

purpose is to liven up your Web page. There are quite a few sources for this kind of material

already—we recommend John Pew's book Instant Java, also published by Sun Microsystems

Press/Prentice Hall.

Finally, with the explosive growth of the Java class library, a one-volume treatment of all the

features of Java that serious programmers need to know is no longer possible. Hence, we

decided to break the book up into two volumes. The first volume, which you hold in your

hands, concentrates on the fundamental concepts of the Java language, along with the basics

of user-interface programming. The second volume goes further into the enterprise features

and advanced user-interface programming. It includes detailed discussions of:

• Multithreading

• Network programming

• Distributed objects

• Collection classes

• Databases

• Advanced graphics

• Advanced GUI components

• Internationalization

• Native methods

• JavaBeans

When writing a book, errors and inaccuracies are inevitable. We'd very much like to know

about them. But, of course, we'd prefer to learn about each of them only once. We have put up

a list of frequently asked questions, bugs fixes, and workarounds in a Web page at

http://www.horstmann.com/corejava.html. Strategically placed at the end of the FAQ (to

encourage you to read through it first) is a form you can use to report bugs and suggest

improvements. Please don't be disappointed if we don't answer every query or if we don't get

back to you immediately. We do read all e-mail and appreciate your input to make future

editions of this book clearer and more informative.

We hope that you find this book enjoyable and helpful in your Java programming.

About This Book

Chapter 1 gives an overview of the capabilities of Java that set it apart from other

programming languages. We explain what the designers of the language set out to do and to

Core Java™ 2: Volume I–Fundamentals

9

what extent they succeeded. Then, we give a short history of how Java came into being and

how it has evolved.

In Chapter 2, we tell you how to install Java and the companion software for this book from

the CD-ROM onto your computer. Then we guide you through compiling and running three

typical Java programs, a console application, a graphical application, and an applet.

Chapter 3 starts the discussion of the Java language. In this chapter, we cover the basics:

variables, loops, and simple functions. If you are a C or C++ programmer, this is smooth

sailing because the syntax for these language features is essentially the same as in C. If you

come from a non-C background such as Visual Basic or COBOL, you will want to read this

chapter carefully.

Object-oriented programming (OOP) is now in the mainstream of programming practice, and

Java is completely object oriented. Chapter 4 introduces encapsulation, the first of two

fundamental building blocks of object orientation, and the Java language mechanism to

implement it, that is, classes and methods. In addition to the rules of the Java language, we

also give advice on sound OOP design. Finally, we cover the marvelous javadoc tool that

formats your code comments as a set of hyperlinked web pages. If you are familiar with C++,

then you can browse through this chapter quickly. Programmers coming from a non-object￾oriented background should expect to spend some time mastering OOP concepts before going

further with Java.

Classes and encapsulation are only one part of the OOP story, and Chapter 5 introduces the

other, namely, inheritance. Inheritance lets you take an existing class and modify it according

to your needs. This is a fundamental technique for programming in Java. The inheritance

mechanism in Java is quite similar to that in C++. Once again, C++ programmers can focus

on the differences between the languages.

Chapter 6 shows you how to use Java's notion of an interface. Interfaces let you go beyond

the simple inheritance model of Chapter 5. Mastering interfaces allows you full access to the

power of Java's completely object-oriented approach to programming. We also cover a useful

technical feature of Java here. These are called inner classes. Inner classes help make your

code cleaner and more concise.

In Chapter 7, we begin application programming in earnest. We show how you can make

windows, how to paint on them, how to draw with geometric shapes, how to format text in

multiple fonts, and how to display images.

Chapter 8 is a detailed discussion of the event model of the AWT, the abstract windows

toolkit. (We discuss the event model that was added to Java 1.1, not the obsolete and

simplistic 1.0 event model.) You'll see how to write the code that responds to events like

mouse clicks or key presses. Along the way you'll see how to handle basic GUI elements like

buttons and panels.

Chapter 9 discusses the Swing GUI toolkit in great detail. The Swing toolkit is how you can

use Java to build a cross-platform graphical user interface. You'll learn all about the various

kinds of buttons, text components, borders, sliders, list boxes, menus, and dialog boxes.

However, some of the more advanced components are discussed in Volume 2.

Core Java™ 2: Volume I–Fundamentals

10

After you finish Chapter 9, you finally have all mechanisms in place to write applets, those

mini-programs that can live inside a Web page, and so applets are the topic of Chapter 10. We

show you a number of useful and fun applets, but more importantly, we show you what goes

on behind the scenes. And we show you how to use the Java Plug-in that enables you to roll

out applets that take advantage of all the newest Java features, even if your users use old

browsers or browsers made by hostile vendors.

Chapter 11 discusses exception handling, Java's robust mechanism to deal with the fact that

bad things can happen to good programs. For example, a network connection can become

unavailable in the middle of a file download, a disk can fill up, and so on. Exceptions give

you an efficient way of separating the normal processing code from the error handling. Of

course, even after hardening your program by handling all exceptional conditions, it still

might fail to work as expected. In the second half of this chapter, we give you a large number

of useful debugging tips. Finally, we guide you through sample sessions with various tools:

the JDB debugger, the debugger of the Forte development environment, a profiler, a code

coverage testing tool and the AWT robot.

We finish the book with input and output handling. In Java, all I/O is handled through so￾called streams. Streams let you deal in a uniform manner with communicating with any

source of data, such as files, network connections, or memory blocks. We include detailed

coverage of the reader and writer classes, which make it easy to deal with Unicode; and we

show you what goes on under the hood when you use object serialization mechanism, which

makes saving and loading objects easy and convenient.

An appendix lists the Java language keywords.

Conventions

As is common in many computer books, we use courier type to represent computer code.

There are many C++ notes that explain the difference between Java and

C++. You can skip over them if you don't have a background in C++ or if

you consider your experience with that language a bad dream of which

you'd rather not be reminded.

Notes and tips are tagged with “note” and “tip” icons that look like these.

Tải ngay đi em, còn do dự, trời tối mất!