Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer."— Presentation transcript:

1 © Copyright 1992-2004 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

2 © Copyright 1992-2004 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.

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

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

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

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

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

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

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

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

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

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 22.2 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

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 22.2 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

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

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 22.2 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

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

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

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 22.2 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

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

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 22.2 Keyboard Events (Cont.) Figure 22.11 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

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

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 22.3 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

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

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

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 22.3 Jmenu s (Cont.) Figure 22.15 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

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

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

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

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 22.3 Jmenu s (Cont.) Figure 22.19 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

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

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 22.3 Jmenu s (Cont.) Figure 22.21 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

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

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

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 22.4 JColorChooser (Cont.) Figure 22.24 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

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 22.4 JColorChooser (Cont.) Figure 22.25 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

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

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 22.4 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

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

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 22.4 JColorChooser (Cont.) Figure 22.29 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

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 22.4 JColorChooser (Cont.) Figure 22.30 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

41  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; 12 13 // JMenu to show display options clear, invert colors, and color 14 private JMenu displayJMenu; 15 16 // JMenuItems to clear the JTextArea and choose color 17 private JMenuItem clearJMenuItem; 18 private JMenuItem colorJMenuItem; 19 20 // JMenu to display format options style and size 21 private JMenu formatJMenu; 22 23 // JMenu and array of JCheckBoxMenuItems to display style options 24 private JMenu styleJMenu; 25 private JCheckBoxMenuItem styleMenuItems[];

42  2004 Prentice Hall, Inc. All rights reserved. Outline 42 TypingApplication.java (2 of 31) 26 27 // JMenu, array of JRadioButtonMenuItems, and ButtonGroup to 28 // display size options 29 private JMenu sizeJMenu; 30 private JRadioButtonMenuItem sizeMenuItems[]; 31 private ButtonGroup sizeButtonGroup; 32 33 // JLabel and JTextArea to display text output 34 private JLabel prompt1JLabel, prompt2JLabel; 35 private JTextArea outputJTextArea; 36 37 // 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; 42 43 // 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

43  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; 52 53 // JButtons to represent fifth row of keys 54 private JButton shiftLeftJButton, zJButton, xJButton, cJButton, 55 vJButton, bJButton, nJButton, mJButton, commaJButton, 56 periodJButton, questionJButton, upJButton; 57 58 // JButtons to represent sixth row of keys 59 private JButton spaceJButton, leftJButton, downJButton, 60 rightJButton; 61 62 // JButton to store the last JButton typed 63 private JButton lastJButton; 64 65 // array of JButtons 66 private JButton[] keyJButtons = 67 new JButton[ KeyEvent.KEY_LAST + 1 ]; 68 69 // Font of outputJTextArea 70 private Font outputFont; 71 Array of JButton references indexed by virtual key constant

44  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” } 74 75 // String array of font styles 76 String sytleNames[] = { “Bold”, “Italic” } 77 78 // no-argument constructor 79 public TypingApplication() 80 { 81 createUserInterface(); 82 } 83 84 // 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(); 89 90 // enable explicit positioning of GUI components 91 contentPane.setLayout( null ); 92 93 // set up typingJMenuBar 94 typingJMenuBar = new JMenuBar(); 95 setJMenuBar( typingJMenuBar ); 96 Creating a JMenuBar

45  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 ); 101 102 // 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( 108 109 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 } 116 117 } // end anonymous inner class 118 119 ); // 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

46  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( 126 127 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 } 134 135 } // end anonymous inner class 136 137 ); // end call to addActionListener 138 139 // 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

47  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 ); 148 149 styleMenuItems = new JCheckBoxMenuItem[ styleNames.length ]; 150 151 // 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( 158 159 new ItemListener() // anonymous inner class 160 { 161 // event handler called when styleMenuItems selected 162 public void itemStateChanged( ItemEvent event ) 163 { 164 styleMenuItemsStateChanged( event ); 165 } 166 167 } // end anonymous inner class 168 169 ); // end call to addItemListener Creating the Style submenu Adding JCheckBox- MenuItem s to a JMenu

48  2004 Prentice Hall, Inc. All rights reserved. Outline 48 TypingApplication.java (8 of 31) 170 171 } // end for 172 173 // set up sizeJMenu 174 sizeJMenu = new JMenu( "Size" ); 175 sizeJMenu.setMnemonic( KeyEvent.VK_Z ); 176 formatJMenu.add( sizeJMenu ); 177 178 sizeMenuItems = new JRadioButtonMenuItem[ sizeNames.length ]; 179 sizeButtonGroup = new ButtonGroup(); 180 181 // 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( 189 190 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

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

50  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( 224 225 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 } 232 233 // event handler called when any key is released 234 public void keyReleased( KeyEvent event ) 235 { 236 outputJTextAreaKeyReleased( event ); 237 } 238 239 // 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

51  2004 Prentice Hall, Inc. All rights reserved. Outline 51 TypingApplication.java (11 of 31) 243 244 } // end anonymous inner class 245 246 ); // end call to addKeyListener 247 248 outputJTextArea.addFocusListener( 249 250 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 } 257 258 } // end anonymous inner class 259 260 ); // end call to addFocusListener 261 262 // 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

52  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; 273 274 // 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; 279 280 // 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; 285 286 // 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

53  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; 297 298 // 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; 303 304 // 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; 309 310 // 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

54  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; 321 322 // 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; 327 328 // set up hyphenJButton 329 hyphenJButton = new JButton( "-" ); 330 hyphenJButton.setBounds( 543, 250, 48, 48 ); 331 contentPane.add( hyphenJButton ); 332 keyJButtons[ KeyEvent.VK_MINUS ] = hyphenJButton; 333 334 // 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

55  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; 345 346 // 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; 351 352 // 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; 357 358 // 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

56  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; 369 370 // 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; 375 376 // 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; 381 382 // 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

57  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; 393 394 // 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; 399 400 // 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; 405 406 // 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

58  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; 417 418 // 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; 423 424 // 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; 429 430 // 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

59  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; 441 442 // 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; 447 448 // 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; 453 454 // 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

60  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; 465 466 // 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; 471 472 // 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; 477 478 // 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

61  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; 489 490 // set up colonJButton 491 colonJButton = new JButton( ":" ); 492 colonJButton.setBounds( 522, 346, 48, 48 ); 493 contentPane.add( colonJButton ); 494 keyJButtons[ KeyEvent.VK_SEMICOLON ] = colonJButton; 495 496 // set up quoteJButton 497 quoteJButton = new JButton( "\"" ); 498 quoteJButton.setBounds( 570, 346, 48, 48 ); 499 contentPane.add( quoteJButton ); 500 keyJButtons[ KeyEvent.VK_QUOTE ] = quoteJButton; 501 502 // 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

62  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; 513 514 // 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; 519 520 // 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; 525 526 // 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

63  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; 537 538 // 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; 543 544 // 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; 549 550 // 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

64  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; 561 562 // set up periodJButton 563 periodJButton = new JButton( "." ); 564 periodJButton.setBounds( 499, 394, 48, 48 ); 565 contentPane.add( periodJButton ); 566 keyJButtons[ KeyEvent.VK_PERIOD ] = periodJButton; 567 568 // set up questionJButton 569 questionJButton = new JButton( "?" ); 570 questionJButton.setBounds( 547, 394, 48, 48 ); 571 contentPane.add( questionJButton ); 572 keyJButtons[ KeyEvent.VK_SLASH ] = questionJButton; 573 574 // 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

65  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; 585 586 // set up leftJButton 587 leftJButton = new JButton( "<" ); 588 leftJButton.setBounds( 570, 442, 48, 48 ); 589 contentPane.add( leftJButton ); 590 keyJButtons[ KeyEvent.VK_LEFT ] = leftJButton; 591 592 // 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; 597 598 // 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

66  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 608 609 } // end method createUserInterface 610 611 // reset the color of the lastJButton 612 private void outputJTextAreaFocusLost( FocusEvent event ) 613 { 614 resetColor(); 615 616 } // end method outputJTextAreaFocusLost 617 618 // clear text 619 private void clearJMenuItemActionPerformed( ActionEvent event ) 620 { 621 outputJTextArea.setText( "" ); 622 623 } // end method clearJMenuItemActionPerformed 624

67  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 } 634 635 } // end method changeColor 636 637 // changes lastJButton's color back to default 638 private void resetColor() 639 { 640 if ( lastJButton != null ) 641 { 642 lastJButton.setBackground( this.getBackground() ); 643 } 644 645 } // end method resetColor 646

68  2004 Prentice Hall, Inc. All rights reserved. Outline 68 647 // 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 ); 652 653 // if the user selected a color 654 if ( foregroundColor != null ) 655 { 656 // set foreground color of outputJTextArea 657 outputJTextArea.setForeground( foregroundColor ); 658 } 659 660 } // end method colorMenuItemActionPerformed 661 662 // change font when user selects an item from Style submenu 663 private void styleMenuItemsStateChanged( ItemEvent event ) 664 { 665 int style = Font.PLAIN; 666 667 // 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

69  2004 Prentice Hall, Inc. All rights reserved. Outline 69 672 673 // check for italic selection 674 if ( styleMenuItems[ 1 ].isSelected() ) 675 { 676 style += Font.ITALIC; 677 } 678 679 // create a new Font with the specified style 680 outputFont = new Font( outputFont.getName(), style, 681 outputFont.getSize() ); 682 683 outputJTextArea.setFont( outputFont ); // set the font 684 685 } // end method styleMenuItemsStateChanged 686 687 // 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(); 692 693 // 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

70  2004 Prentice Hall, Inc. All rights reserved. Outline 70 697 698 outputJTextArea.setFont( outputFont ); // set the font 699 700 } // end method sizeMenuItemsActionPerformed 701 702 // 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(); 707 708 // change the color of the associated JButton 709 changeColor( keyJButtons[ buttonIndex ] ); 710 711 } // end method outputJTextAreaKeyPressed 712 713 // reset the color of the pressed key's JButton 714 private void outputJTextAreaKeyReleased( KeyEvent event ) 715 { 716 resetColor(); 717 718 } // 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

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


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

Similar presentations


Ads by Google