如何撰写键盘事件监听 : 键盘事件监听 « Swing事件 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing事件 » 键盘事件监听 
15. 21. 3. 如何撰写键盘事件监听
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class UsingKeyListener {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is the Window Title");

    aWindow.setBounds(50100300300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField typingArea = new JTextField(20);
    typingArea.addKeyListener(new KeyListener() {

      /** Handle the key typed event from the text field. */
      public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
      }

      /** Handle the key pressed event from the text field. */
      public void keyPressed(KeyEvent e) {
        displayInfo(e, "KEY PRESSED: ");
      }

      /** Handle the key released event from the text field. */
      public void keyReleased(KeyEvent e) {
        displayInfo(e, "KEY RELEASED: ");
      }

      protected void displayInfo(KeyEvent e, String s) {
        String keyString, modString, tmpString, actionString, locationString;

        // You should only rely on the key char if the event
        // is a key typed event.
        int id = e.getID();
        if (id == KeyEvent.KEY_TYPED) {
          char c = e.getKeyChar();
          keyString = "key character = '" + c + "'";
        else {
          int keyCode = e.getKeyCode();
          keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode")";
        }

        int modifiers = e.getModifiersEx();
        modString = "modifiers = " + modifiers;
        tmpString = KeyEvent.getModifiersExText(modifiers);
        if (tmpString.length() 0) {
          modString += " (" + tmpString + ")";
        else {
          modString += " (no modifiers)";
        }

        actionString = "action key? ";
        if (e.isActionKey()) {
          actionString += "YES";
        else {
          actionString += "NO";
        }

        locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
          locationString += "standard";
        else if (location == KeyEvent.KEY_LOCATION_LEFT) {
          locationString += "left";
        else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
          locationString += "right";
        else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
          locationString += "numpad";
        else // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
          locationString += "unknown";
        }

        System.out.println(keyString);
        System.out.println(modString);
        System.out.println(actionString);
        System.out.println(locationString);
      }

    });
    aWindow.add(typingArea);
    aWindow.setVisible(true);
  }
}
15. 21. 键盘事件监听
15. 21. 1. KeyListener接口
15. 21. 2. 事件ID和KeyEvent类定义
15. 21. 3. 如何撰写键盘事件监听
15. 21. 4. 键盘事件键盘事件
15. 21. 5. KeyListener和KeyEvent
15. 21. 6. 处理按键
15. 21. 7. Get key pressed as a key character (which is a Unicode character)
15. 21. 8. 键代码
15. 21. 9. 设置焦点遍历
15. 21. 10. 列出了键盘操
15. 21. 11. 使ENTER键像TAB键
15. 21. 12. Overriding Many Default Typed Key Bindings in a JTextComponent
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.