690 Graphical (Hosting your own web site) User Interface Components: Part 1 Chapter
690 Graphical User Interface Components: Part 1 Chapter 12 20 21 // set up JTextArea 22 textArea = new JTextArea( 10, 15 ); 23 textArea.setText( “Press any key on the keyboard…” ); 24 textArea.setEnabled( false ); 25 getContentPane().add( textArea ); 26 27 // allow frame to process Key events 28 addKeyListener( this ); 29 30 setSize( 350, 100 ); 31 setVisible( true ); 32 } 33 34 // handle press of any key 35 public void keyPressed( KeyEvent event ) 36 { 37 line1 = “Key pressed: ” + 38 event.getKeyText( event.getKeyCode() ); 39 setLines2and3( event ); 40 } 41 42 // handle release of any key 43 public void keyReleased( KeyEvent event ) 44 { 45 line1 = “Key released: ” + 46 event.getKeyText( event.getKeyCode() ); 47 setLines2and3( event ); 48 } 49 50 // handle press of an action key 51 public void keyTyped( KeyEvent event ) 52 { 53 line1 = “Key typed: ” + event.getKeyChar(); 54 setLines2and3( event ); 55 } 56 57 // set second and third lines of output 58 private void setLines2and3( KeyEvent event ) 59 { 60 line2 = “This key is ” + 61 ( event.isActionKey() ? “” : “not ” ) + 62 “an action key”; 63 64 String temp = 65 event.getKeyModifiersText( event.getModifiers() ); 66 67 line3 = “Modifier keys pressed: ” + 68 ( temp.equals( “” ) ? “none” : temp ); 69 70 textArea.setText( 71 line1 + “n” + line2 + “n” + line3 + “n” ); 72 } Fig. 12.22 Fig. 12.22 Demonstrating key event-handling (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01