歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java鍵盤事件監聽

Java鍵盤事件監聽(一):

  1. package com.han;  
  2.   
  3. import java.awt.Container;  
  4. import java.awt.event.KeyEvent;  
  5. import java.awt.event.KeyListener;  
  6.   
  7. import javax.swing.JFrame;  
  8. import javax.swing.JLabel;  
  9. import javax.swing.JScrollPane;  
  10. import javax.swing.JTextArea;  
  11. import javax.swing.SpringLayout;  
  12.   
  13. /** 
  14.  * 鍵盤事件監聽(1) 
  15.  * @author HAN 
  16.  * 
  17.  */  
  18. public class KeyEvent_1 extends JFrame {  
  19.   
  20.     /** 
  21.      *  
  22.      */  
  23.     private static final long serialVersionUID = -6762512303624322086L;  
  24.   
  25.     public KeyEvent_1() {  
  26.         // TODO Auto-generated constructor stub   
  27.         Container container = getContentPane();  
  28.         SpringLayout springLayout = new SpringLayout();  
  29.         container.setLayout(springLayout);  
  30.         JLabel remarqueLabel = new JLabel("remarque :");  
  31.         JTextArea textArea = new JTextArea(330);  
  32.         JScrollPane scrollPane = new JScrollPane();  
  33.         scrollPane.setViewportView(textArea);  
  34.         container.add(remarqueLabel);  
  35.         springLayout.putConstraint(SpringLayout.WEST, remarqueLabel, 5,  
  36.                 SpringLayout.WEST, container);  
  37.         springLayout.putConstraint(SpringLayout.NORTH, remarqueLabel, 5,  
  38.                 SpringLayout.NORTH, container);  
  39.         container.add(scrollPane);  
  40.         springLayout.putConstraint(SpringLayout.WEST, scrollPane, 5,  
  41.                 SpringLayout.EAST, remarqueLabel);  
  42.         springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 5,  
  43.                 SpringLayout.NORTH, container);  
  44.         springLayout.putConstraint(SpringLayout.EAST, scrollPane, -5,  
  45.                 SpringLayout.EAST, container);  
  46.         springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -5,  
  47.                 SpringLayout.SOUTH, container);  
  48.   
  49.         textArea.addKeyListener(new KeyListener() {  
  50.   
  51.             @Override  
  52.             public void keyTyped(KeyEvent e) {  
  53.                 // TODO Auto-generated method stub   
  54.                 System.out.println("The charactor you typed : " + "\""  
  55.                         + e.getKeyChar() + "\"");  
  56.             }  
  57.   
  58.             @Override  
  59.             public void keyPressed(KeyEvent e) {  
  60.                 // TODO Auto-generated method stub   
  61.                   
  62.                 // 寫大寫字母時,釋放按鍵是不知道是否先釋放的是shift還是字母按鍵;但是   
  63.                 // 可以肯定的是按按鍵時必須是先按的shift。   
  64.                 if (e.getKeyCode() == KeyEvent.VK_CONTROL ||   
  65.                         e.getKeyCode() == KeyEvent.VK_SHIFT ||  
  66.                         e.getKeyCode() == KeyEvent.VK_ALT) {  
  67.                     System.out.println("\n" + "The Component triggered : " + e.getSource());  
  68.                 }  
  69.                 if (!(e.isControlDown() || e.isShiftDown() || e.isAltDown())) {  
  70.                     System.out.println("\n" + "The Component triggered : " + e.getSource());  
  71.                 }  
  72.                 String keyText = KeyEvent.getKeyText(e.getKeyCode());  
  73.                 if (e.isActionKey()) {  
  74.                     System.out.print("The action key you pressed : " + "\""  
  75.                             + keyText + "\"");  
  76.                 } else {  
  77.                     System.out.print("The non-action key you pressed : " + "\""  
  78.                             + keyText + "\"");  
  79.                     int keyCode = e.getKeyCode();  
  80.                     switch (keyCode) {  
  81.                     case KeyEvent.VK_CONTROL:  
  82.                         System.out.print(", Control key is pressed");  
  83.                         break;  
  84.                     case KeyEvent.VK_SHIFT:  
  85.                         System.out.print(", Shift key is pressed");  
  86.                         break;  
  87.                     case KeyEvent.VK_ALT:  
  88.                         System.out.print(", Alt key is pressed");  
  89.                         break;  
  90.                     }  
  91.                 }  
  92.                 System.out.println();  
  93.             }  
  94.   
  95.             @Override  
  96.             public void keyReleased(KeyEvent e) {  
  97.                 // TODO Auto-generated method stub   
  98.                 // the integer code for an actual key on the keyboard.    
  99.                 // (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)   
  100.                 System.out.println("The key you released : " +  "\"" +  
  101.                         KeyEvent.getKeyText(e.getKeyCode()) + "\"");  
  102.             }  
  103.   
  104.         });  
  105.     }  
  106.   
  107.     /** 
  108.      * @param args 
  109.      */  
  110.     public static void main(String[] args) {  
  111.         // TODO Auto-generated method stub   
  112.         KeyEvent_1 frame = new KeyEvent_1();  
  113.         frame.setTitle("KeyEvent Test");  
  114.         frame.setVisible(true);  
  115.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  116.         frame.setBounds(00600300);  
  117.     }  
  118.   
  119. }  
Copyright © Linux教程網 All Rights Reserved