tailieunhanh - murachs Java SE 2010 phần 8

300 trang đầu tiên sẽ mang bạn (như vậy sẽ hầu hết các phần còn lại của cuốn sách). Cho dù bạn biết hoặc không bộ phận trong số 200 trang cuối cùng giả sử bạn là một chuyên gia và thậm chí các bang chủ đề tác giả đặt ra sẽ không được bảo hiểm kể từ khi nó được giả định bạn đã familar. Lá bạn tự hỏi cuốn sách thực sự. | Chapter 17 How to handle events and validate data 569 Methods of the Key Listener interface Method Description void keyPressed KeyEvent e void keyReleased KeyEvent e void keyTyped KeyEvent e Invoked when a key is pressed. Invoked when a key is released. Invoked when a key is pressed and released. Common methods of the KeyEvent class Method Description getKeyCode Returns an int code that represents the key pressed. getKeyChar Returns a char that represents the key pressed. isControlDown Returns a boolean that indicates if the Ctrl key is down. isAltDown0 Returns a boolean that indicates if the Alt key is down. isShiftDown 0 Returns a boolean that indicates if the Shift key is down. A method of the InputEvent class Method consume Description Stops further processing of the event. Example 1 A class that implements the KeyListener interface public class NumFilter implements KeyLỉstener _ _ _ public void keyTyped KeyEvent e char c if c 0 c I c 2 c 3 c 4 c 5 c 1 6 c 1 7 c 1 8 c 9 a c a c a c e. I consume public void keyPressed KeyEvent e public void keyReleased KeyEvent e Example 2 A text field that uses the key listener paymentTextField new JTextField 10 new NumFilter0 paymentTextField Description A keyboard event occurs when a user presses releases or presses and releases a key. This example uses the keyTyped method to limit the user s input to numeric data. The KeyEvent class inherits the InputEvent class. Figure 17-11 How to work with keyboard events 570 Section 4 GUI programming with Java How to work with adapter classes Since it can be tedious to code all the methods in a low-level listener interface just to use one of them the Java API provides an adapter class for each of its listener interfaces. An adapter class is a class that implements all the methods of a listener interface but doesn t code any statements within these methods. Then your listener class can inherit the adapter class and override any