Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 783 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
783
Dung lượng
8,07 MB
Nội dung
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 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 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 Tables Code Examples Figures Preface To the Reader About This Book Conventions 10 CD-ROM 11 Acknowledgments 12 Chapter 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 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 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 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 Inheritance 195 Extending Classes 195 Object: The Cosmic Superclass 216 The Class Class 240 Reflection 244 Design Hints for Inheritance 262 Chapter Interfaces and Inner Classes 265 Interfaces 265 Object Cloning 276 Inner Classes 282 Proxies 299 Chapter 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 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 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 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 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 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 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 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 ... doubtful utility except for interfacing with the Windows API In addition to Java and J++ sharing a common syntax, their foundational libraries (strings, utilities, networking, multithreading, math,... 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... While not glitzy, this is an important area where Java, primarily due to its portability and multithreading and networking capabilities, can add real value Java is making great inroads in embedded