Essential Series Springer-Verlag London Ltd.
Also in this series: Essential Visual Basic 4.0 fast 3-540-19998-5 Essential Delphi 2.0 fast 3-540-76026-1 Essential Java fast 3-540-76052-0 Essential Visual Basic 5.0 fast 3-540-76148-9 Duncan Reed and Peter Thomas Essential HTML fast 3-540-76199-3 Essential Delphi 3 fast 3-540-76150-0 John Hunt Essential JavaBeans fast 1-85233-032-5 John Vince Essential Virtual Reality fast 1-85233-012-0 Essential Visual J++ 6.0 fast 1-85233-013-9
Essential Java 2 fast How to develop applications and applets with Java 2, Springer
, BSc(Hons), MPhil, PhD Department of Computer and Information Sciences, De Montfort University Kents Hill Campus, Hammerwood Gate, Kents Hill Milton Keynes, MK7 6HP, UK ISBN 978-1-85233-071-2 British Library Cataloguing in Publication Data Cowell, John, 1957- Essential Java 2 fast: how to develop applications and applets with Java 2. - (Essential series) 1.J ava (Computer program language) I.Title 005.2'76'2 ISBN 978-1-85233-071-2 Library of Congress Cataloging-in-Publication Data Cowell, John, 1957- Essential Java 2 fast: how to develop applications and applets with Java 2/. p. cm. ISBN 978-1-85233-071-2 DOI 10.1007/978-1-4471-0573-2 1. Title. QA76.73.J38C694 1999 005.2'762--dc21 ISBN 978-1-4471-0573-2 (ebook) 99-25430 CIP Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may on1y be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms oflicences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. Springer-V erlag London 1999 Originally published by Springer-Verlag London Berlin Heidelberg in 1999 Microsoft, MS, Visual Basic, Windows, Windows 95 and Windows 98 are trademarks of Microsoft Corporation Ltd. Java and Hot Java are trademarks of Sun Microsystems Corporation Ltd. Pentium is a trademark of Intel Corporation Ud. The use of registered names, trademarks etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Typesetting: Camera-ready by author 34/3830-5432 Printed on acid-free paper SPIN 10786551
1 WHY JAVA 2 IS SPECIAL... 1 Introduction......... 1 Is this book for you?... 2 What you need to run Java 2... 2 How to use this book... 3 An object-oriented approach... 4 Conventions... 4 2 AN OBJECT-ORIENTED APPLICATION... 6 Introduction... 6 A first application... 7 What can go wrong?... 8 How does the application work?... 8 Classes and objects... 9 The main method... 10 What's new... 11 Running the application... 12 Using classes and objects... 12 Naming conventions... 12 3 THE JAVA LANGUAGE..................... 14 Introduction... 14 Applications and applets... 14 Comments... 14 Terminating Java statements... 15 Data types... 15 The integer data types... 15 The floating point data types... 16 The boolean data type...,... 16 The char data type...... 16 The String data type... 17 Names in Java... 17 Declaring data... 17 Arithmetic in Java... 18 Mixing data types... 18 Casting... 19 v
vi Assignment operators... 20 Using strings in Java... 21 Operator precedence... 22 Displaying information on your screen... 22 4 BRANCHING AND LOOPING... 24 Introduction... 24 The If... else statement... 24 Logical operators... 26 The switch statement... 27 Block statements... 29 Looping... 29 The for loop... 29 The while loop... 31 The do... while loop... 31 The break statement... 32 Arrays... 33 5 CREATING AND USING CLASSES AND OBJECTS... 35 Introduction... '"... 35 The Account class... 35 The displaydetails method... 36 The incredit method... '"... 37 The credit and debit methods... 3 7 The main method... 37 Using constructors... 40 Encapsulating data... 41 Calling methods... 44 Command line arguments for applications....45 6 USIN'G.APPLETS... 47 Introduction... '"... 47 Writing applets... 47 The APPLET tag... 48 Key methods of the Applet class... 50 The init method... 50 The start method... 50 The stop method... 50 The destroy method... 51 The paint method... 51 Command line arguments for applets... 51
vii 7 THE GRAPHICS, COLOR AND FONT CLASSES... 54 Introduction... 54 The co-ordinate frame... 54 Importing classes... 55 The Graphics class... 56 Drawing lines... 56 Drawing rectangles... 57 Drawing circles and ovals... 59 Drawing arcs... 60 Drawing polygons... 61 The Color class... 63 Creating colours... 64 Displaying text and changing fonts... 65 Displaying images... 67 8 INHERITANCE AND POLYMORPHISM... 70 Introduction... 70 A hierarchy of computer components... 70 Defining the Memory subclass... 73 Polymorphism... 74 The MBoard class... 75 Extending a subclass... 76 Class and instance variables... 78 Static methods... 80 Overloading methods... 81 Access control... 82 Interfaces... 84 9 MOUSE AND KEYBOARD EVENTS... 85 Introduction... 85 The MouseEvent class... 85 The MouseListener interface... 87 Which button was pressed?... 90 The MouseAdapter class... 92 The MouseMotionListener interface... 94 The KeyEvent class... 96 The KeyAdapter class... 97 The KeyListener interface... 100 10 THE AWT COMPONENTS... 102 Introduction... 102 Event-driven programming... 102
viii The A WT classes... 102 The Button class... 103 Handling events... 104 The Label class... '"... 106 The Scrollbar class... 107 Handling Scrollbar events... 108 The Checkbox class... 111 The List class... 114 Multiple selection in List objects... 116 The TextComponent class... 118 The TextArea class... 119 Using the TextListener interface... 123 The TextField class... 124 The Canvas class... 12 6 11 THE LAYOUT MANAGERS... 128 Introduction... 128 The FlowLayout class... 128 The BorderLayout class... 130 The CardLayout class... 132 The GridLayout class... 135 The GridBagLayout class... 136 12 WINDOWS, DIALOGS AND MENUS........................ 140 Introduction... 140 The Container class... 140 The Frame class... 141 Creating an application which uses a frame... 141 The WindowListener interface... 144 Creating an applet which uses a frame... 145 The MenuBar class... 1 47 The Dialog class... 1 50 The FileDialog class... 153 The Panel class... 15 9 13 THE SWING COMPONENTS... 162 Introduction... 162 Differences between Swing and A WT components... 162 The Swing components... 163 The Swing container classes... 163 The AbstractButton class... 164 The Swing and A WT buttons... 164 Changing the look and feel... 167
ix The JButton class..................... 171 The IToggleButton, JRadioButton and JCheclcBox classes... 173 Displaying text... 178 The llabel class... 178 The ITextField class... 182 The ITextArea class... 183 The llist class...,... 184 The JColorChooser class... 188 The JFileChooser class... 193 14 JAVA EXCEPTIONS... 198 Introduction........................... 198 The Exception class... 198 The try and catch clauses... ", 198 Throwing an exception... 201 Creating new exceptions... 202 Throwing without catching... 202 15 WRITING THREADED APPLICATIONS 203 Introduction... 203 Pre-emptive scheduling..,... 203 The Runnable interface... 205 The Thread class... 207 Synchronizing threads... 208 The Thread class... 210 Creating animation... 211 The bouncing ball applet... 214 16 FILES AND STREAMS... 218 Introduction... 218 The File class... 218 The InputStream and OutputStream classes... 221 The DatalnputStream and DataOutputStream classes... 224 Using the DataOutputStream class... 225 Using the DatalnputStream class... 229 Reading text flies... 232 Reading from the keyboard... 233 Converting strings to other data types... 234 IN"DEX................................ 236