DOC

Learn Java (GUI Applications) - Contents

By Roy King,2014-04-24 12:38
13 views 0
Learn Java (GUI Applications) - Contents

    i

    Course Notes for:

    Learn Java

    (GUI Applications)

    ? Lou Tylee, 2004

    KIDware

    PO Box 28234

    Seattle, WA 98118

    (206) 721-2556

    FAX (508) 464-0455

    E-Mail: support@kidwaresoftware.com Web Site: www.kidwaresoftware.com

    ? KIDware (206) 721-2556

     ii Learn Java (GUI Applications)

    Notice

    These notes were developed for the course, “Learn Java (GUI Applications).” They are not intended to be a complete reference to Java. Consult other texts and various websites for more detailed information.

    The notes refer to several software and hardware products by their trade names. These references are for informational purposes only and all trademarks are the property of their respective companies.

     Lou Tylee

     Seattle, Washington

     January 2004

    ? KIDware (206) 721-2556

    Contents iii

    Learn Java

    (GUI Applications)

    Contents

1. Introduction to Java

     Preview ...................................................................................................... 1-1

     Course Objectives ...................................................................................... 1-2

     What is Java? ............................................................................................ 1-3

     What is a GUI Application? ........................................................................ 1-4

     A Brief Look at Object-Oriented Programming (OOP) ............................... 1-6

     What is Needed to Learn Java? ................................................................ 1-7

     Downloading and Installing Java ............................................................... 1-8

     Downloading and Installing JCreator ....................................................... 1-10

     Testing the Installation ............................................................................. 1-12

     Getting Help with a Java Program ........................................................... 1-18

     Structure of a Java Program .................................................................... 1-19

     Structure of a Java GUI Application ......................................................... 1-20

     Swing Controls ......................................................................................... 1-22

     Creating a Java Project with JCreator ...................................................... 1-25

     Create a Frame ........................................................................................ 1-31

     Saving Java Projects with JCreator ......................................................... 1-34

     JCreator and Java Files ........................................................................... 1-35

     Create the User Interface ........................................................................ 1-36

     Example 1-1: Stopwatch Application - Adding Controls ........................... 1-40

     Adding Event Methods ............................................................................. 1-46

     Variables .................................................................................................. 1-50

     Java Data Types ...................................................................................... 1-51

     Variable Declaration................................................................................. 1-53

     Arrays ...................................................................................................... 1-54

     Constants ................................................................................................. 1-55

     Variable Initialization ................................................................................ 1-56

     Example 1-2: Stopwatch Application - Writing Code ............................... 1-57

    ? KIDware (206) 721-2556

    iv Learn Java (GUI Applications) 1. Introduction to Java (continued)

     Class Review ........................................................................................... 1-65

     Practice Problems 1 ................................................................................. 1-66

     Problem 1-1. Beep Problem

     Problem 1-2. Caption Problem

     Problem 1-3. Enabled Problem

     Problem 1-4. Date Problem

     Exercise 1: Calendar/Time Display .......................................................... 1-67

     Addenda: Java Programming without a Development Environment ........ 1-68

    ? KIDware (206) 721-2556

    Contents v

    2. The Java Language

     Review and Preview .................................................................................. 2-1

     A Brief History of Java ............................................................................... 2-2

     Rules of Java Programming ....................................................................... 2-3

     Java Statements and Expressions ............................................................. 2-4

     Type Casting .............................................................................................. 2-6

     Java Arithmetic Operators ......................................................................... 2-7

     Comparison and Logical Operators ........................................................... 2-9

     Concatenation Operators ......................................................................... 2-11

     Strings to Numbers to Strings .................................................................. 2-12

     Java String Methods ................................................................................ 2-14

     Random Number Generator .................................................................... 2-19

     Math Functions ........................................................................................ 2-20

     Example 2-1: Savings Account ................................................................ 2-22

     Focus Traversal ....................................................................................... 2-33

     Example 2-2: Savings Accounts Setting Focus .................................... 2-34

     Improving a Java Application ................................................................... 2-35

     Java Decisions - if Statements ................................................................. 2-36

     Switch Statement - Another Way to Branch ............................................ 2-40

     Control Focus .......................................................................................... 2-43

     Input Validation ........................................................................................ 2-45

     Example 2-3: Savings Account Input Validation ................................... 2-47

     Java Looping ........................................................................................... 2-57

     Java Counting .......................................................................................... 2-60

     Example 2-4: Savings Account - Decisions ............................................. 2-62

     Class Review ........................................................................................... 2-79

     Practice Problems 2 ................................................................................. 2-80

     Problem 2-1. Random Number Problem

     Problem 2-2. Price Problem

     Problem 2-3. Odd Integers Problem

     Problem 2-4. Pennies Problem

     Problem 2-5. Code Problem

     Exercise 2-1: Computing a Mean and Standard Deviation ...................... 2-81

     Exercise 2-2: Flash Card Addition Problems ........................................... 2-82

    ? KIDware (206) 721-2556

vi Learn Java (GUI Applications)

    3. Java Swing Controls

     Review and Preview .................................................................................. 3-1

     Function Overloading ................................................................................. 3-2

     Confirm Dialog (JOptionPane) ................................................................... 3-3

     Font Object ................................................................................................ 3-8

     Color Object ............................................................................................. 3-10

     JFrame Object ......................................................................................... 3-13

     Frame Layout and Centering ................................................................... 3-17

     JButton Control ........................................................................................ 3-21

     JLabel Control .......................................................................................... 3-23

     JTextField Control .................................................................................... 3-26

     JTextArea Control .................................................................................... 3-29

     Example 3-1: Password Validation .......................................................... 3-31

     JCheckBox Control .................................................................................. 3-43

     JRadioButton Control ............................................................................... 3-45

     JPanel Control ......................................................................................... 3-48

     Handling Multiple Events in a Single Procedure ...................................... 3-50

     Control Arrays .......................................................................................... 3-52

     Example 3-2: Pizza Order ........................................................................ 3-54

     JList Control ............................................................................................. 3-78

     JScrollPane Control ................................................................................. 3-82

     JComboBox Control ................................................................................. 3-85

     Example 3-3: Flight Planner ..................................................................... 3-89

     Class Review ......................................................................................... 3-104

     Practice Problems 3 ............................................................................... 3-105

     Problem 3-1. Message Box Problem

     Problem 3-2. Tray Problem

     Problem 3-3. List Box Problem

     Problem 3-4. Combo Box Problem

     Exercise 3: Customer Database Input Screen ....................................... 3-106

    ? KIDware (206) 721-2556

    Contents vii

    4. More Java Swing Controls

     Review and Preview .................................................................................. 4-1

     JSpinner Control ........................................................................................ 4-2

     Example 4-1: Date Input Device ................................................................ 4-7

     JScrollBar Control .................................................................................... 4-16

     JSlider Control ......................................................................................... 4-20

     Example 4-2: Temperature Conversion ................................................... 4-23

     JLabel Control (Revisited) ........................................................................ 4-35

     Example 4-3: “Find the Burger” Game ..................................................... 4-39

     JFileChooser Control (Open Files) .......................................................... 4-49

     Example 4-4: Image Viewer ..................................................................... 4-57

     Class Review ........................................................................................... 4-63

     Practice Problems 4 ................................................................................. 4-64

     Problem 4-1. Number Guess Problem

     Problem 4-2. RGB Color Problem

     Problem 4-3. Tic-Tac-Toe Problem

     Problem 4-4. File Times Problem

     Exercise 4: Student Database Input Screen ............................................ 4-65

    ? KIDware (206) 721-2556

viii Learn Java (GUI Applications)

    5. Java GUI Application Design and Distribution

     Review and Preview .................................................................................. 5-1

     Application Design Considerations ............................................................ 5-2

     JTabbedPane Control ................................................................................ 5-3

     Example 5-1: Shopping Cart ...................................................................... 5-6

     Using General Methods in Applications ................................................... 5-28

     Example 5-2: Average Value ................................................................... 5-31

     Returning Multiple Values from General Methods ................................... 5-47

     Example 5-3: Circle Geometry ................................................................. 5-49

     Adding Menus to Java Applications ......................................................... 5-62

     Example 5-4: Note Editor ......................................................................... 5-77

     Distribution of a Java GUI Application ..................................................... 5-92

     Class Review ........................................................................................... 5-95

     Practice Problems 5 ................................................................................. 5-96

     Problem 5-1 Tabbed Pane Problem

     Problem 5-2 Note Editor About Box Problem

     Problem 5-3 Normal Numbers Problem

     Exercise 5: US/World Capitals Quiz ........................................................ 5-97

    ? KIDware (206) 721-2556

    Contents ix

    6. Exception Handling, Debugging and Sequential Files

     Review and Preview .................................................................................. 6-1

     Program Errors .......................................................................................... 6-2

     Exception Handling .................................................................................... 6-3

     Debugging Java Programs ........................................................................ 6-7

     Simple Debugging ...................................................................................... 6-8

     Example 6-1: Debugging Example ............................................................ 6-9

     Using the Java Debugger ........................................................................ 6-16

     Using the Debugging Tools ...................................................................... 6-18

     Debugging Strategies .............................................................................. 6-22

     Sequential Files ....................................................................................... 6-23

     Sequential File Output (Variables) ........................................................... 6-25

     Example 6-2: Writing Variables to Sequential Files ................................. 6-29

     Sequential File Input (Variables) .............................................................. 6-32

     Example 6-3: Reading Variables from Sequential Files ........................... 6-35

     Parsing Data Lines................................................................................... 6-37

     Example 6-4. Parsing Data Lines ............................................................ 6-39

     Reading Tokenized Lines ........................................................................ 6-44

     Example 6-5. Reading Tokenized Data Lines .......................................... 6-45

     Building Data Lines .................................................................................. 6-47

     Example 6-6: Building Data Lines ............................................................ 6-50

     Configuration Files ................................................................................... 6-63

     Example 6-7: Configuration Files ............................................................. 6-65

     Writing and Reading Text Using Sequential Files .................................... 6-71

     JFileChooser Control (Save Files) ........................................................... 6-74

     Example 6-8: Note Editor - Reading and Saving Text Files ..................... 6-79

     Class Review ........................................................................................... 6-93

     Practice Problems 6 ................................................................................. 6-94

     Problem 6-1. Debugging Problem

     Problem 6-2. Option Saving Problem

     Problem 6-3. Text File Problem

     Problem 6-4. Data File Problem

     Exercise 6-1: Information Tracking .......................................................... 6-96

     Exercise 6-2: „Recent Files‟ Menu Option ................................................ 6-97

    ? KIDware (206) 721-2556

x Learn Java (GUI Applications)

    7. Graphics Techniques with Java

     Review and Preview .................................................................................. 7-1

     Simple Animation ....................................................................................... 7-2

     Example 7-1: Simple Animation ................................................................. 7-4

     Timer Object ............................................................................................ 7-10

     Example 7-2: Timer Example ................................................................... 7-13

     Basic Animation ....................................................................................... 7-17

     Example 7-3: Basic Animation ................................................................. 7-19

     Random Numbers (Revisited) and Games .............................................. 7-29

     Example 7-4: One-Buttoned Bandit ......................................................... 7-31

     Randomly Sorting Integers ...................................................................... 7-48

     Example 7-5: Random Integers ............................................................... 7-50

     Java2D Graphics ..................................................................................... 7-60

     Graphics2D Object................................................................................... 7-61

     Stroke and Paint Objects ......................................................................... 7-62

     Line2D Shape .......................................................................................... 7-65

     Graphics Demonstration .......................................................................... 7-67

     Persistent Graphics.................................................................................. 7-72

     Example 7-6: Drawing Lines .................................................................... 7-78

     Rectangle2D Shape ................................................................................. 7-89

     RoundRectangle2D Shape ...................................................................... 7-92

     Example 7-7: Drawing Rectangles ........................................................... 7-94

     Ellipse2D Shape .................................................................................... 7-108

     Example 7-8: Drawing Ellipses .............................................................. 7-111

     Arc2D Shape ......................................................................................... 7-119

     Example 7-9: Drawing Pie Segments .................................................... 7-122

     Pie Charts .............................................................................................. 7-135

     Line Charts and Bar Charts ................................................................... 7-139

     Coordinate Conversions ........................................................................ 7-143

     Example 7-10: Line, Bar and Pie Charts ................................................ 7-153

     Class Review ......................................................................................... 7-169

     Practice Problems 7 ............................................................................... 7-170

     Problem 7-1. Dice Rolling Problem

     Problem 7-2. Shape Guessing Problem

     Problem 7-3. Pie Chart Problem

     Problem 7-4. Plotting Problem

     Exercise 7-1: Blackjack .......................................................................... 7-171

     Exercise 7-2: Information Tracking Plotting ........................................... 7-172

    ? KIDware (206) 721-2556

Report this document

For any questions or suggestions please email
cust-service@docsford.com