Chapter 12 (Web server type) Graphical User Interface Components: Part 1
Friday, February 15th, 2008Chapter 12 Graphical User Interface Components: Part 1 689 InputEventmethod Description isMetaDown() This method returns truewhen the user clicks the right mouse button on a mouse with two or three buttons. To simulate a right- mouse-button click on a one-button mouse, the user can press the Meta key on the keyboard and click the mouse button. isAltDown() This method returns truewhen the user clicks the middle mouse button on a mouse with three buttons. To simulate a middle- mouse-button click on a one-or two-button mouse, the user can press the Alt key on the keyboard and click the mouse button. Fig. 12.21 InputEventmethods that help distinguish among left-, center-and right-mouse-button clicks . 12.13 Keyboard Event Handling This section presents the KeyListenerevent-listener interface for handling key events. Key events are generated when keys on the keyboard are pressed and released. A class that implements KeyListener must provide definitions for methods keyPressed, key- Releasedand keyTyped, each of which receives a KeyEventas its argument. Class KeyEvent is a subclass of InputEvent. Method keyPressed is called in response to pressing any key. Method keyTyped is called in response to pressing any key that is not an action key (e.g., an arrow key, Home, End, Page Up, Page Down, a function key, Num Lock, Print Screen, Scroll Lock, Caps Lock and Pause). Method keyReleased is called when the key is released after any keyPressedor keyTypedevent. Figure 12.22 demonstrates the KeyListenermethods. Class KeyDemoimplements the KeyListenerinterface, so all three methods are defined in the application. 1 // Fig. 12.22: KeyDemo.java 2 // Demonstrating keystroke events. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class KeyDemo extends JFrame implements KeyListener { 12 private String line1 = “”, line2 = “”; 13 private String line3 = “”; 14 private JTextArea textArea; 15 16 // set up GUI 17 public KeyDemo() 18 { 19 super( “Demonstrating Keystroke Events” ); Fig. 12.22 Fig. 12.22 Demonstrating key event-handling (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01