© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Graphical User Interfaces (Part IV)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Event Handling Events and Listeners Timers and Animation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Objectives –Display a date and time. –Use a JPasswordField.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 26.1 IBM Cloudscape Database 26.2 Test-Driving.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Chapter 13 – More GUI Components Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A First Look at GUI Applications
Chapter 13: Advanced GUIs and Graphics
Advanced GUIs and Graphics
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer Application 22.2 Keyboard Events 22.3 JMenu s 22.4 JColorChooser 22.5Wrap-Up Tutorial 22 – Typing Skills Developer Application Introducing Keyboard Events and JMenu s

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Handle keyboard events. –Create menus for your Java applications. –Display a color dialog to enable users to choose colors.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.1 Typing Skills Developer application with a key pressed. Virtual keyboard Highlighted key

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.1 Typing Skills Developer application with a key pressed.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.2 Setting the size and style of the typed font. Style submenu JCheckBoxMenuItem

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.2 Setting the size and style of the typed font. Size submenu JRadioButtonMenuItem

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.3 JColorChooser dialog displayed when Display > Color… is selected. JColorChooser color dialog

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Separator bar –The horizontal rule in the menu –Used to group related menu items

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.4 Selecting the Clear Text JMenuItem. Display JMenu Separator bar

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Typing Skills Developer Application (Cont.) Figure 22.5 Cleared Typing Skills Developer application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events When the user presses a key Highlight in yellow the corresponding JButton on the virtual keyboard When the user releases a key Reset the corresponding JButton to its default color When the user selects the Display JMenu Display the Clear JTextArea and Color... JMenuItems When the user selects the Clear Text JMenuItem Clear the JTextArea When the user selects the Color… JMenuItem Display the JColorChooser dialog Update the JTextArea text’s color When the user selects the Format JMenu Display the Style and Size JMenus

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) When the user selects the Style JMenu Display the Style JCheckBoxMenuItems When the user selects a Style JCheckBoxMenuItem Update the JTextArea font’s style When the user selects the Size JMenu Display the Size JRadioButtonMenuItems When the user selects a Size JRadioButtonMenuItems Update the JTextArea font’s size

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) keyListener –Declares three event handlers for keyboard events keyPressed –Called when a key is pressed keyReleased –Called when a key is released KeyTyped –Called when a character key is pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure 22.7 Adding a KeyListener to outputJTextArea. Adding a KeyListener Creating an anonymous inner class

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure 22.8 Coding the keyPressed and keyReleased event handlers.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure 22.9 Creating an array of JButton s. Using KeyEvent constant KEY_LAST KeyEvent.LAST_KEY –The KeyEvent class declares a number of constants for use with keyboard events –The KEY_LAST constant is the maximum value of these constants

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure Adding JButton s to the array. Adding qJButton to the array Adding wJButton to the array

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure Highlighting the JButton associated with the pressed key. Calling the getKeyCode method Using the virtual key code to index the array The getKeyCode method returns an int, which is the virtual key code associated with the key that was pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Keyboard Events (Cont.) Figure Removing the highlight from the JButton. Resetting the color of the JButton

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JMenu s Menus –Allow you to group related commands for you applications –Are often added to a menu bar JMenuBar component –A container for the menus in your application The setJMenuBar method sets the menu bar for your application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating a JMenuBar. Creating a JMenuBar

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating displayJMenu. Creating a JMenu Top-level menu Adding a JMenu to a JMenuBar

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Adding a JMenuItem to your application. Creating a JMenuItem JMenuItem –Single menu item performs some action when it is selected by the user The addSeparator method of class JMenu creates a recessed, horizontal rule Adding a separator bar Adding a JMenuItem to a JMenu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Adding colorJMenuItem to your application. Creating a JMenuItem Adding a JMenuItem to a JMenu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Adding a second JMenu to your application. Creating top-level Format menu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating the Style submenu. Submenu is –A JMenu component –Added to another JMenu component Creating the Style submenu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating JCheckBoxMenuItem s. Adding a JCheckBoxMenuItem to a JMenu JCheckBoxMenuItem –Has a checkbox to its left –Allows the user to see if the item is currently selected

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating the Size submenu. Creating the Size submenu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Creating the Size JRadioButtonMenuItem s. JRadioButtonMenuItem component –Has a JRadioButton to its left –Aallows the user to see if the item is currently selected Adding JRadio- ButtonMenuItems to the menu

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Jmenu s (Cont.) Figure Typing Skills Developer Application with JMenu s added.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser Figure Displaying the JColorChooser dialog. Displaying the JColorChooser showDialog method of JColorChooser –Displays the JColorChooser dialog

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Setting the JTextArea ’s text color. setForeground method –Sets the text’s color to the color selected by the user Changing the text to the color selected by user

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Declaring a variable for the Font ’s style. Initialize a variable to hold the font style Font class –Allows you to create and manipulate objects that represent fonts –Provides constants for the style of a Font

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Determining the style of the Font. Testing whether the Bold menu item was selected Testing whether the Italic menu item was selected

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) getName method of class Font –Returns name of the Font getSize method of class Font –Returns the size of the Font setFont method of JTextArea –Sets the Font of a JTextArea

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Creating a new Font with the specified style. Figure Using the setFont method to change the text’s font. Setting the font of outputJTextArea Creating a new Font with the specified style

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Obtaining a reference to the event source. Storing a reference to the JRadio- ButtonMenuItem getSource method of ActionEvent –returns a reference (the source for this event) of type Object

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JColorChooser (Cont.) Figure Setting the Font for the new size. getStyle method of Font –Returns the style of the Font Creating a new Font with the specified size

 2004 Prentice Hall, Inc. All rights reserved. Outline 41 TypingApplication.java (1 of 31) 1 // Tutorial 22: TypingApplication.java 2 // Application enables users to practice typing 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.text.*; 7 8 public class TypingApplication extends JFrame 9 { 10 // JMenuBar for display and format options 11 private JMenuBar typingJMenuBar; // JMenu to show display options clear, invert colors, and color 14 private JMenu displayJMenu; // JMenuItems to clear the JTextArea and choose color 17 private JMenuItem clearJMenuItem; 18 private JMenuItem colorJMenuItem; // JMenu to display format options style and size 21 private JMenu formatJMenu; // JMenu and array of JCheckBoxMenuItems to display style options 24 private JMenu styleJMenu; 25 private JCheckBoxMenuItem styleMenuItems[];

 2004 Prentice Hall, Inc. All rights reserved. Outline 42 TypingApplication.java (2 of 31) // JMenu, array of JRadioButtonMenuItems, and ButtonGroup to 28 // display size options 29 private JMenu sizeJMenu; 30 private JRadioButtonMenuItem sizeMenuItems[]; 31 private ButtonGroup sizeButtonGroup; // JLabel and JTextArea to display text output 34 private JLabel prompt1JLabel, prompt2JLabel; 35 private JTextArea outputJTextArea; // JButtons to represent second row of keys 38 private JButton tildeJButton, oneJButton, twoJButton, 39 threeJButton, fourJButton, fiveJButton, sixJButton, 40 sevenJButton, eightJButton, nineJButton, zeroJButton, 41 hyphenJButton, plusJButton, backspaceJButton; // JButtons to represent third row of keys 44 private JButton tabJButton, qJButton, wJButton, eJButton, 45 rJButton, tJButton, yJButton, uJButton, iJButton, oJButton, 46 pJButton, leftBraceJButton, rightBraceJButton, slashJButton; 47

 2004 Prentice Hall, Inc. All rights reserved. Outline 43 TypingApplication.java (3 of 31) 48 // JButtons to represent fourth row of keys 49 private JButton capsJButton, aJButton, sJButton, dJButton, 50 fJButton, gJButton, hJButton, jJButton, kJButton, lJButton, 51 colonJButton, quoteJButton, enterJButton; // JButtons to represent fifth row of keys 54 private JButton shiftLeftJButton, zJButton, xJButton, cJButton, 55 vJButton, bJButton, nJButton, mJButton, commaJButton, 56 periodJButton, questionJButton, upJButton; // JButtons to represent sixth row of keys 59 private JButton spaceJButton, leftJButton, downJButton, 60 rightJButton; // JButton to store the last JButton typed 63 private JButton lastJButton; // array of JButtons 66 private JButton[] keyJButtons = 67 new JButton[ KeyEvent.KEY_LAST + 1 ]; // Font of outputJTextArea 70 private Font outputFont; 71 Array of JButton references indexed by virtual key constant

 2004 Prentice Hall, Inc. All rights reserved. Outline 44 TypingApplication.java (4 of 31) 72 // String array of font sizes 73 String sizeNames[] = { “12”, “16”, “20” } // String array of font styles 76 String sytleNames[] = { “Bold”, “Italic” } // no-argument constructor 79 public TypingApplication() 80 { 81 createUserInterface(); 82 } // set and position GUI components; register event handlers 85 private void createUserInterface() 86 { 87 // get content pane for attaching GUI components 88 Container contentPane = getContentPane(); // enable explicit positioning of GUI components 91 contentPane.setLayout( null ); // set up typingJMenuBar 94 typingJMenuBar = new JMenuBar(); 95 setJMenuBar( typingJMenuBar ); 96 Creating a JMenuBar

 2004 Prentice Hall, Inc. All rights reserved. Outline 45 TypingApplication.java (5 of 31) 97 // set up displayJMenu 98 displayJMenu = new JMenu( "Display" ); 99 displayJMenu.setMnemonic( KeyEvent.VK_D ); 100 typingJMenuBar.add( displayJMenu ); // set up clearJMenuItem 103 clearJMenuItem = new JMenuItem( "Clear Text" ); 104 clearJMenuItem.setMnemonic( KeyEvent.VK_C ); 105 displayJMenu.add( clearJMenuItem ); 106 displayJMenu.addSeparator(); 107 clearJMenuItem.addActionListener( new ActionListener() // anonymous inner class 110 { 111 // event handler called when clearJMenuItem is selected 112 public void actionPerformed( ActionEvent event ) 113 { 114 clearJMenuItemActionPerformed( event ); 115 } } // end anonymous inner class ); // end call to addActionListener 120 Adding a separator bar to a JMenu Adding a JMenu to a JMenuBar Creating a JMenuItem Creating a JMenu Adding a JMenu- Item to a JMenu

 2004 Prentice Hall, Inc. All rights reserved. Outline 46 TypingApplication.java (6 of 31) 121 // set up colorJMenuItem 122 colorJMenuItem = new JMenuItem( "Color..." ); 123 colorJMenuItem.setMnemonic( KeyEvent.VK_O ); 124 displayJMenu.add( colorJMenuItem ); 125 colorJMenuItem.addActionListener( new ActionListener() // anonymous inner class 128 { 129 // event handler called when colorJMenuItem is selected 130 public void actionPerformed( ActionEvent event ) 131 { 132 colorJMenuItemActionPerformed( event ); 133 } } // end anonymous inner class ); // end call to addActionListener // set up formatJMenu 140 formatJMenu = new JMenu( "Format" ); 141 formatJMenu.setMnemonic( KeyEvent.VK_F ); 142 typingJMenuBar.add( formatJMenu ); 143 Creating top-level Format menu Creating a JMenuItem Adding a JMenu- Item to a JMenu

 2004 Prentice Hall, Inc. All rights reserved. Outline 47 TypingApplication.java (7 of 31) 144 // set up styleJMenu 145 styleJMenu = new JMenu( "Style" ); 146 styleJMenu.setMnemonic( KeyEvent.VK_S ); 147 formatJMenu.add( styleJMenu ); styleMenuItems = new JCheckBoxMenuItem[ styleNames.length ]; // set up styleMenuItems 152 for ( int count = 0; count < styleMenuItems.length; count++ ) 153 { 154 styleMenuItems[ count ] = new JCheckBoxMenuItem( 155 styleNames[ count ] ); 156 styleJMenu.add( styleMenuItems[ count ] ); 157 styleMenuItems[ count ].addItemListener( new ItemListener() // anonymous inner class 160 { 161 // event handler called when styleMenuItems selected 162 public void itemStateChanged( ItemEvent event ) 163 { 164 styleMenuItemsStateChanged( event ); 165 } } // end anonymous inner class ); // end call to addItemListener Creating the Style submenu Adding JCheckBox- MenuItem s to a JMenu

 2004 Prentice Hall, Inc. All rights reserved. Outline 48 TypingApplication.java (8 of 31) } // end for // set up sizeJMenu 174 sizeJMenu = new JMenu( "Size" ); 175 sizeJMenu.setMnemonic( KeyEvent.VK_Z ); 176 formatJMenu.add( sizeJMenu ); sizeMenuItems = new JRadioButtonMenuItem[ sizeNames.length ]; 179 sizeButtonGroup = new ButtonGroup(); // set up sizeMenuItems 182 for ( int count = 0; count < sizeMenuItems.length; count++ ) 183 { 184 sizeMenuItems[ count ] = new JRadioButtonMenuItem( 185 sizeNames[ count ] ); 186 sizeJMenu.add( sizeMenuItems[ count ] ); 187 sizeButtonGroup.add( sizeMenuItems[ count ] ); 188 sizeMenuItems[ count ].addActionListener( new ActionListener() // anonymous inner class 191 { 192 // event handler called when sizeMenuItems is selected 193 public void actionPerformed( ActionEvent event ) Creating the Size submenu Creating the JRadioButton- MenuItem

 2004 Prentice Hall, Inc. All rights reserved. Outline 49 TypingApplication.java (9 of 31) 194 { 195 sizeMenuItemsActionPerformed( event ); 196 } } // end anonymous inner class ); // end call to addActionListener } // end for // set up prompt1JLabel 205 prompt1JLabel = new JLabel( "Type some text using your " "keyboard. The keys you press will be highlighted and " "the text will be displayed." ); 208 prompt1JLabel.setBounds( 15, 5, 725, 20 ); 209 contentPane.add( prompt1JLabel ); // set up prompt2JLabel 212 prompt2JLabel = new JLabel( "Note: Clicking the buttons " "with your mouse will not perform any action." ); 214 prompt2JLabel.setBounds( 15, 20, 725, 25 ); 215 contentPane.add( prompt2JLabel ); 216

 2004 Prentice Hall, Inc. All rights reserved. Outline 50 TypingApplication.java (10 of 31) 217 // set up outputJTextArea 218 outputJTextArea = new JTextArea(); 219 outputJTextArea.setBounds( 15, 50, 725, 175 ); 220 outputJTextArea.setLineWrap( true ); 221 contentPane.add( outputJTextArea ); 222 outputFont = outputJTextArea.getFont(); 223 outputJTextArea.addKeyListener( new KeyListener() // anonymous inner class 226 { 227 // event handler called when any key is pressed 228 public void keyPressed( KeyEvent event ) 229 { 230 outputJTextAreaKeyPressed( event ); 231 } // event handler called when any key is released 234 public void keyReleased( KeyEvent event ) 235 { 236 outputJTextAreaKeyReleased( event ); 237 } // event handler called when any key is typed 240 public void keyTyped( KeyEvent event ) 241 { 242 } Calling method addKeyListener to register a KeyListener object Declaring the keyPressed event handler Declaring the keyReleased event handler Declaring the empty keyTyped event handler

 2004 Prentice Hall, Inc. All rights reserved. Outline 51 TypingApplication.java (11 of 31) } // end anonymous inner class ); // end call to addKeyListener outputJTextArea.addFocusListener( new FocusAdapter() // anonymous inner class 251 { 252 // event handler called when outputJTextArea loses focus 253 public void focusLost( FocusEvent event ) 254 { 255 outputJTextAreaFocusLost( event ); 256 } } // end anonymous inner class ); // end call to addFocusListener // set up tildeJButton 263 tildeJButton = new JButton( "~" ); 264 tildeJButton.setBounds( 15, 250, 48, 48 ); 265 contentPane.add( tildeJButton ); 266 keyJButtons[ KeyEvent.VK_BACK_QUOTE ] = tildeJButton; 267

 2004 Prentice Hall, Inc. All rights reserved. Outline 52 TypingApplication.java (12 of 31) 268 // set up oneJButton 269 oneJButton = new JButton( "1" ); 270 oneJButton.setBounds( 63, 250, 48, 48 ); 271 contentPane.add( oneJButton ); 272 keyJButtons[ KeyEvent.VK_1 ] = oneJButton; // set up twoJButton 275 twoJButton = new JButton( "2" ); 276 twoJButton.setBounds( 111, 250, 48, 48 ); 277 contentPane.add( twoJButton ); 278 keyJButtons[ KeyEvent.VK_2 ] = twoJButton; // set up threeJButton 281 threeJButton = new JButton( "3" ); 282 threeJButton.setBounds( 159, 250, 48, 48 ); 283 contentPane.add( threeJButton ); 284 keyJButtons[ KeyEvent.VK_3 ] = threeJButton; // set up fourJButton 287 fourJButton = new JButton( "4" ); 288 fourJButton.setBounds( 207, 250, 48, 48 ); 289 contentPane.add( fourJButton ); 290 keyJButtons[ KeyEvent.VK_4 ] = fourJButton; 291

 2004 Prentice Hall, Inc. All rights reserved. Outline 53 TypingApplication.java (13 of 31) 292 // set up fiveJButton 293 fiveJButton = new JButton( "5" ); 294 fiveJButton.setBounds( 255, 250, 48, 48 ); 295 contentPane.add( fiveJButton ); 296 keyJButtons[ KeyEvent.VK_5 ] = fiveJButton; // set up sixJButton 299 sixJButton = new JButton( "6" ); 300 sixJButton.setBounds( 303, 250, 48, 48 ); 301 contentPane.add( sixJButton ); 302 keyJButtons[ KeyEvent.VK_6 ] = sixJButton; // set up sevenJButton 305 sevenJButton = new JButton( "7" ); 306 sevenJButton.setBounds( 351, 250, 48, 48 ); 307 contentPane.add( sevenJButton ); 308 keyJButtons[ KeyEvent.VK_7 ] = sevenJButton; // set up eightJButton 311 eightJButton = new JButton( "8" ); 312 eightJButton.setBounds( 399, 250, 48, 48 ); 313 contentPane.add( eightJButton ); 314 keyJButtons[ KeyEvent.VK_8 ] = eightJButton; 315

 2004 Prentice Hall, Inc. All rights reserved. Outline 54 TypingApplication.java (14 of 31) 316 // set up nineJButton 317 nineJButton = new JButton( "9" ); 318 nineJButton.setBounds( 447, 250, 48, 48 ); 319 contentPane.add( nineJButton ); 320 keyJButtons[ KeyEvent.VK_9 ] = nineJButton; // set up zeroJButton 323 zeroJButton = new JButton( "0" ); 324 zeroJButton.setBounds( 495, 250, 48, 48 ); 325 contentPane.add( zeroJButton ); 326 keyJButtons[ KeyEvent.VK_0 ] = zeroJButton; // set up hyphenJButton 329 hyphenJButton = new JButton( "-" ); 330 hyphenJButton.setBounds( 543, 250, 48, 48 ); 331 contentPane.add( hyphenJButton ); 332 keyJButtons[ KeyEvent.VK_MINUS ] = hyphenJButton; // set up plusJButton 335 plusJButton = new JButton( "+" ); 336 plusJButton.setBounds( 591, 250, 48, 48 ); 337 contentPane.add( plusJButton ); 338 keyJButtons[ KeyEvent.VK_EQUALS ] = plusJButton; 339

 2004 Prentice Hall, Inc. All rights reserved. Outline 55 TypingApplication.java (15 of 31) 340 // set up backspaceJButton 341 backspaceJButton = new JButton( "Backspace" ); 342 backspaceJButton.setBounds( 639, 250, 100, 48 ); 343 contentPane.add( backspaceJButton ); 344 keyJButtons[ KeyEvent.VK_BACK_SPACE ] = backspaceJButton; // set up tabJButton 347 tabJButton = new JButton( "Tab" ); 348 tabJButton.setBounds( 15, 298, 75, 48 ); 349 contentPane.add( tabJButton ); 350 keyJButtons[ KeyEvent.VK_TAB ] = tabJButton; // set up qJButton 353 qJButton = new JButton( "Q" ); 354 qJButton.setBounds( 90, 298, 48, 48 ); 355 contentPane.add( qJButton ); 356 keyJButtons[ KeyEvent.VK_Q ] = qJButton; // set up wJButton 359 wJButton = new JButton( "W" ); 360 wJButton.setBounds( 138, 298, 48, 48); 361 contentPane.add( wJButton ); 362 keyJButtons[ KeyEvent.VK_W ] = wJButton; 363

 2004 Prentice Hall, Inc. All rights reserved. Outline 56 TypingApplication.java (16 of 31) 364 // set up eJButton 365 eJButton = new JButton( "E" ); 366 eJButton.setBounds( 186, 298, 48, 48 ); 367 contentPane.add( eJButton ); 368 keyJButtons[ KeyEvent.VK_E ] = eJButton; // set up rJButton 371 rJButton = new JButton( "R" ); 372 rJButton.setBounds( 234, 298, 48, 48 ); 373 contentPane.add( rJButton ); 374 keyJButtons[ KeyEvent.VK_R ] = rJButton; // set up tJButton 377 tJButton = new JButton( "T" ); 378 tJButton.setBounds( 282, 298, 48, 48 ); 379 contentPane.add( tJButton ); 380 keyJButtons[ KeyEvent.VK_T ] = tJButton; // set up yJButton 383 yJButton = new JButton( "Y" ); 384 yJButton.setBounds( 330, 298, 48, 48 ); 385 contentPane.add( yJButton ); 386 keyJButtons[ KeyEvent.VK_Y ] = yJButton; 387

 2004 Prentice Hall, Inc. All rights reserved. Outline 57 TypingApplication.java (17 of 31) 388 // set up uJButton 389 uJButton = new JButton( "U" ); 390 uJButton.setBounds( 378, 298, 48, 48 ); 391 contentPane.add( uJButton ); 392 keyJButtons[ KeyEvent.VK_U ] = uJButton; // set up iJButton 395 iJButton = new JButton( "I" ); 396 iJButton.setBounds( 426, 298, 48, 48 ); 397 contentPane.add( iJButton ); 398 keyJButtons[ KeyEvent.VK_I ] = iJButton; // set up oJButton 401 oJButton = new JButton( "O" ); 402 oJButton.setBounds( 474, 298, 48, 48 ); 403 contentPane.add( oJButton ); 404 keyJButtons[ KeyEvent.VK_O ] = oJButton; // set up pJButton 407 pJButton = new JButton( "P" ); 408 pJButton.setBounds( 522, 298, 48, 48 ); 409 contentPane.add( pJButton ); 410 keyJButtons[ KeyEvent.VK_P ] = pJButton; 411

 2004 Prentice Hall, Inc. All rights reserved. Outline 58 TypingApplication.java (18 of 31) 412 // set up leftBraceJButton 413 leftBraceJButton = new JButton( "[" ); 414 leftBraceJButton.setBounds( 570, 298, 48, 48 ); 415 contentPane.add( leftBraceJButton ); 416 keyJButtons[ KeyEvent.VK_OPEN_BRACKET ] = leftBraceJButton; // set up rightBraceJButton 419 rightBraceJButton = new JButton( "]" ); 420 rightBraceJButton.setBounds( 618, 298, 48, 48 ); 421 contentPane.add( rightBraceJButton ); 422 keyJButtons[ KeyEvent.VK_CLOSE_BRACKET ] = rightBraceJButton; // set up slashJButton 425 slashJButton = new JButton( "\\" ); 426 slashJButton.setBounds( 666, 298, 48, v ); 427 contentPane.add( slashJButton ); 428 keyJButtons[ KeyEvent.VK_BACK_SLASH ] = slashJButton; // set up capsJButton 431 capsJButton = new JButton( "Caps" ); 432 capsJButton.setBounds( 15, 346, 75, 48 ); 433 contentPane.add( capsJButton ); 434 keyJButtons[ KeyEvent.VK_CAPS_LOCK ] = capsJButton; 435

 2004 Prentice Hall, Inc. All rights reserved. Outline 59 TypingApplication.java (19 of 31) 436 // set up aJButton 437 aJButton = new JButton( "A" ); 438 aJButton.setBounds( 90, 346, 48, 48 ); 439 contentPane.add( aJButton ); 440 keyJButtons[ KeyEvent.VK_A ] = aJButton; // set up sJButton 443 sJButton = new JButton( "S" ); 444 sJButton.setBounds( 138, 346, 48, 48 ); 445 contentPane.add( sJButton ); 446 keyJButtons[ KeyEvent.VK_S ] = sJButton; // set up dJButton 449 dJButton = new JButton( "D" ); 450 dJButton.setBounds( 186, 346, 48, 48 ); 451 contentPane.add( dJButton ); 452 keyJButtons[ KeyEvent.VK_D ] = dJButton; // set up fJButton 455 fJButton = new JButton( "F" ); 456 fJButton.setBounds( 234, 346, 48, 48 ); 457 contentPane.add( fJButton ); 458 keyJButtons[ KeyEvent.VK_F ] = fJButton; 459

 2004 Prentice Hall, Inc. All rights reserved. Outline 60 TypingApplication.java (20 of 31) 460 // set up gJButton 461 gJButton = new JButton( "G" ); 462 gJButton.setBounds( 282, 346, 48, 48 ); 463 contentPane.add( gJButton ); 464 keyJButtons[ KeyEvent.VK_G ] = gJButton; // set up hJButton 467 hJButton = new JButton( "H" ); 468 hJButton.setBounds( 330, 346, 48, 48 ); 469 contentPane.add( hJButton ); 470 keyJButtons[ KeyEvent.VK_H ] = hJButton; // set up jJButton 473 jJButton = new JButton( "J" ); 474 jJButton.setBounds( 378, 346, 48, 48 ); 475 contentPane.add( jJButton ); 476 keyJButtons[ KeyEvent.VK_J ] = jJButton; // set up kJButton 479 kJButton = new JButton( "K" ); 480 kJButton.setBounds( 426, 346, 48, 48 ); 481 contentPane.add( kJButton ); 482 keyJButtons[ KeyEvent.VK_K ] = kJButton; 483

 2004 Prentice Hall, Inc. All rights reserved. Outline 61 TypingApplication.java (21 of 31) 484 // set up lJButton 485 lJButton = new JButton( "L" ); 486 lJButton.setBounds( 474, 346, 48, 48 ); 487 contentPane.add( lJButton ); 488 keyJButtons[ KeyEvent.VK_L ] = lJButton; // set up colonJButton 491 colonJButton = new JButton( ":" ); 492 colonJButton.setBounds( 522, 346, 48, 48 ); 493 contentPane.add( colonJButton ); 494 keyJButtons[ KeyEvent.VK_SEMICOLON ] = colonJButton; // set up quoteJButton 497 quoteJButton = new JButton( "\"" ); 498 quoteJButton.setBounds( 570, 346, 48, 48 ); 499 contentPane.add( quoteJButton ); 500 keyJButtons[ KeyEvent.VK_QUOTE ] = quoteJButton; // set up enterJButton 503 enterJButton = new JButton( "Enter" ); 504 enterJButton.setBounds( 618, 346, 96, 48 ); 505 contentPane.add( enterJButton ); 506 keyJButtons[ KeyEvent.VK_ENTER ] = enterJButton; 507

 2004 Prentice Hall, Inc. All rights reserved. Outline 62 TypingApplication.java (22 of 31) 508 // set up shiftLeftJButton 509 shiftLeftJButton = new JButton( "Shift" ); 510 shiftLeftJButton.setBounds( 15, 394, 100, 48 ); 511 contentPane.add( shiftLeftJButton ); 512 keyJButtons[ KeyEvent.VK_SHIFT ] = shiftLeftJButton; // set up zJButton 515 zJButton = new JButton( "Z" ); 516 zJButton.setBounds( 115, 394, 48, 48 ); 517 contentPane.add( zJButton ); 518 keyJButtons[ KeyEvent.VK_Z ] = zJButton; // set up xJButton 521 xJButton = new JButton( "X" ); 522 xJButton.setBounds( 163, 394, 48, 48 ); 523 contentPane.add( xJButton ); 524 keyJButtons[ KeyEvent.VK_X ] = xJButton; // set up cJButton 527 cJButton = new JButton( "C" ); 528 cJButton.setBounds( 211, 394, 48, 48 ); 529 contentPane.add( cJButton ); 530 keyJButtons[ KeyEvent.VK_C ] = cJButton; 531

 2004 Prentice Hall, Inc. All rights reserved. Outline 63 TypingApplication.java (23 of 31) 532 // set up vJButton 533 vJButton = new JButton( "V" ); 534 vJButton.setBounds( 259, 394, 48, 48 ); 535 contentPane.add( vJButton ); 536 keyJButtons[ KeyEvent.VK_V ] = vJButton; // set up bJButton 539 bJButton = new JButton( "B" ); 540 bJButton.setBounds( 307, 394, 48, 48 ); 541 contentPane.add( bJButton ); 542 keyJButtons[ KeyEvent.VK_B ] = bJButton; // set up nJButton 545 nJButton = new JButton( "N" ); 546 nJButton.setBounds( 355, 394, 48, 48 ); 547 contentPane.add( nJButton ); 548 keyJButtons[ KeyEvent.VK_N ] = nJButton; // set up mJButton 551 mJButton = new JButton( "M" ); 552 mJButton.setBounds( 403, 394, 48, 48 ); 553 contentPane.add( mJButton ); 554 keyJButtons[ KeyEvent.VK_M ] = mJButton; 555

 2004 Prentice Hall, Inc. All rights reserved. Outline 64 TypingApplication.java (24 of 31) 556 // set up commaJButton 557 commaJButton = new JButton( "," ); 558 commaJButton.setBounds( 451, 394, 48, 48 ); 559 contentPane.add( commaJButton ); 560 keyJButtons[ KeyEvent.VK_COMMA ] = commaJButton; // set up periodJButton 563 periodJButton = new JButton( "." ); 564 periodJButton.setBounds( 499, 394, 48, 48 ); 565 contentPane.add( periodJButton ); 566 keyJButtons[ KeyEvent.VK_PERIOD ] = periodJButton; // set up questionJButton 569 questionJButton = new JButton( "?" ); 570 questionJButton.setBounds( 547, 394, 48, 48 ); 571 contentPane.add( questionJButton ); 572 keyJButtons[ KeyEvent.VK_SLASH ] = questionJButton; // set up upJButton 575 upJButton = new JButton( "^" ); 576 upJButton.setBounds( 618, 394, 48, 48 ); 577 contentPane.add( upJButton ); 578 keyJButtons[ KeyEvent.VK_UP ] = upJButton; 579

 2004 Prentice Hall, Inc. All rights reserved. Outline 65 TypingApplication.java (25 of 31) 580 // set up spaceJButton 581 spaceJButton = new JButton( "" ); 582 spaceJButton.setBounds( 208, 442, 300, 48 ); 583 contentPane.add( spaceJButton ); 584 keyJButtons[ KeyEvent.VK_SPACE ] = spaceJButton; // set up leftJButton 587 leftJButton = new JButton( "<" ); 588 leftJButton.setBounds( 570, 442, 48, 48 ); 589 contentPane.add( leftJButton ); 590 keyJButtons[ KeyEvent.VK_LEFT ] = leftJButton; // set up downJButton 593 downJButton = new JButton( "v" ); 594 downJButton.setBounds( 618, 442, 48, 48 ); 595 contentPane.add( downJButton ); 596 keyJButtons[ KeyEvent.VK_DOWN ] = downJButton; // set up rightJButton 599 rightJButton = new JButton( ">" ); 600 rightJButton.setBounds( 666, 442, 48, 48 ); 601 contentPane.add( rightJButton ); 602 keyJButtons[ KeyEvent.VK_RIGHT ] = rightJButton; 603

 2004 Prentice Hall, Inc. All rights reserved. Outline 66 TypingApplication.java (26 of 31) 604 // set properties of application's window 605 setTitle( "Typing Skills Developer" ); // set title bar string 606 setSize( 760, 550 ); // set window size 607 setVisible( true ); // display window } // end method createUserInterface // reset the color of the lastJButton 612 private void outputJTextAreaFocusLost( FocusEvent event ) 613 { 614 resetColor(); } // end method outputJTextAreaFocusLost // clear text 619 private void clearJMenuItemActionPerformed( ActionEvent event ) 620 { 621 outputJTextArea.setText( "" ); } // end method clearJMenuItemActionPerformed 624

 2004 Prentice Hall, Inc. All rights reserved. Outline 67 TypingApplication.java (27 of 31) 625 // highlight JButton passed as argument 626 private void changeColor( JButton changeJButton ) 627 { 628 if ( changeJButton != null ) 629 { 630 resetColor(); 631 changeJButton.setBackground( Color.YELLOW ); 632 lastJButton = changeJButton; 633 } } // end method changeColor // changes lastJButton's color back to default 638 private void resetColor() 639 { 640 if ( lastJButton != null ) 641 { 642 lastJButton.setBackground( this.getBackground() ); 643 } } // end method resetColor 646

 2004 Prentice Hall, Inc. All rights reserved. Outline // change text color when user selects Colors... menu item 648 private void colorJMenuItemActionPerformed( ActionEvent event ) 649 { 650 Color foregroundColor = JColorChooser.showDialog( 651 this, "Choose a color", Color.BLACK ); // if the user selected a color 654 if ( foregroundColor != null ) 655 { 656 // set foreground color of outputJTextArea 657 outputJTextArea.setForeground( foregroundColor ); 658 } } // end method colorMenuItemActionPerformed // change font when user selects an item from Style submenu 663 private void styleMenuItemsStateChanged( ItemEvent event ) 664 { 665 int style = Font.PLAIN; // check for bold selection 668 if ( styleMenuItems[ 0 ].isSelected() ) 669 { 670 style += Font.BOLD; 671 } TypingApplication.java (28 of 31) Testing whether the Bold menu item was selected Displaying JColorChooser Changing the text to the color selected by the user Initializing a variable to hold the style

 2004 Prentice Hall, Inc. All rights reserved. Outline // check for italic selection 674 if ( styleMenuItems[ 1 ].isSelected() ) 675 { 676 style += Font.ITALIC; 677 } // create a new Font with the specified style 680 outputFont = new Font( outputFont.getName(), style, 681 outputFont.getSize() ); outputJTextArea.setFont( outputFont ); // set the font } // end method styleMenuItemsStateChanged // change font size when user selects an item from size submenu 688 private void sizeMenuItemsActionPerformed( ActionEvent event ) 689 { 690 JRadioButtonMenuItem sizeMenuItem = 691 ( JRadioButtonMenuItem ) event.getSource(); // create a new Font with the specified size 694 outputFont = new Font( outputFont.getName(), 695 outputFont.getStyle(), 696 Integer.parseInt( sizeMenuItem.getText() ) ); TypingApplication.java (29 of 31) Testing whether the Italic menu item was selected Creating a new Font with the specified style Setting the font of outputJTextArea Storing a reference to the JRadio- ButtonMenuItem Creating a new Font with the specified size

 2004 Prentice Hall, Inc. All rights reserved. Outline outputJTextArea.setFont( outputFont ); // set the font } // end method sizeMenuItemsActionPerformed // highlight corresponding JButton when a key is pressed 703 private void outputJTextAreaKeyPressed( KeyEvent event ) 704 { 705 // get the key code for this event 706 int buttonIndex = event.getKeyCode(); // change the color of the associated JButton 709 changeColor( keyJButtons[ buttonIndex ] ); } // end method outputJTextAreaKeyPressed // reset the color of the pressed key's JButton 714 private void outputJTextAreaKeyReleased( KeyEvent event ) 715 { 716 resetColor(); } // end method outputJTextAreaKeyReleased TypingApplication.java (30 of 31) Using the getKeyCode method to determine which key was pressed Changing the background color for the lastJButton to the default color

 2004 Prentice Hall, Inc. All rights reserved. Outline 71 TypingApplication.java (31 of 31) // main method 721 public static void main( String[] args ) 722 { 723 TypingApplication application = new TypingApplication(); 725 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class TypingApplication