Apache web server for windows - Chapter 12 Graphical User Interface Components: Part 1

Chapter 12 Graphical User Interface Components: Part 1 691 73 74 // execute application 75 public static void main( String args[] ) 76 { 77 KeyDemo application = new KeyDemo(); 78 79 application.setDefaultCloseOperation( 80 JFrame.EXIT_ON_CLOSE ); 81 } 82 83 } // end class KeyDemo Fig. 12.22 Fig. 12.22 Demonstrating key event-handling (part 3 of 3). The constructor (lines 17 32) registers the application to handle its own key events with method addKeyListener at line 28. Method addKeyListener is defined in class Component, so every subclass of Componentcan notify KeyListeners of key events for that Component. Line 25 in the constructor adds JTextAreatextArea(where the program s output is displayed) to the content pane. Notice, in the screen captures, that textAreaoccupies the entire window. This is due to the content pane s default BorderLayout (discussed in Section 12.14.2 and demonstrated in Fig. 12.25). When a single Component is added to a BorderLayout, the Componentoccupies the entire Container. Methods keyPressed (lines 35 40) and keyReleased (lines 43 48) use KeyEventmethod getKeyCodeto get the virtual key code of the key that was pressed. Class KeyEvent maintains a set of constants the virtual key code constants that represent every key on the keyboard. These constants can be compared with the return value of getKeyCodeto test for individual keys on the keyboard. The value returned by get- KeyCodeis passed to KeyEventmethod getKeyText, which returns a Stringcon Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply