Java键盘事件监听(1)

package com.han;

import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

/**
 * 键盘事件监听(1)
 * @author HAN
 *
 */
public class KeyEvent_1 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6762512303624322086L;

	public KeyEvent_1() {
		// TODO Auto-generated constructor stub
		Container container = getContentPane();
		SpringLayout springLayout = new SpringLayout();
		container.setLayout(springLayout);
		JLabel remarqueLabel = new JLabel("remarque :");
		JTextArea textArea = new JTextArea(3, 30);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(textArea);
		container.add(remarqueLabel);
		springLayout.putConstraint(SpringLayout.WEST, remarqueLabel, 5,
				SpringLayout.WEST, container);
		springLayout.putConstraint(SpringLayout.NORTH, remarqueLabel, 5,
				SpringLayout.NORTH, container);
		container.add(scrollPane);
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 5,
				SpringLayout.EAST, remarqueLabel);
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 5,
				SpringLayout.NORTH, container);
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, -5,
				SpringLayout.EAST, container);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -5,
				SpringLayout.SOUTH, container);

		textArea.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				System.out.println("The charactor you typed : " + "\""
						+ e.getKeyChar() + "\"");
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
				// 写大写字母时,释放按键是不知道是否先释放的是shift还是字母按键;但是
				// 可以肯定的是按按键时必须是先按的shift。
				if (e.getKeyCode() == KeyEvent.VK_CONTROL || 
						e.getKeyCode() == KeyEvent.VK_SHIFT ||
						e.getKeyCode() == KeyEvent.VK_ALT) {
					System.out.println("\n" + "The Component triggered : " + e.getSource());
				}
				if (!(e.isControlDown() || e.isShiftDown() || e.isAltDown())) {
					System.out.println("\n" + "The Component triggered : " + e.getSource());
				}
				String keyText = KeyEvent.getKeyText(e.getKeyCode());
				if (e.isActionKey()) {
					System.out.print("The action key you pressed : " + "\""
							+ keyText + "\"");
				} else {
					System.out.print("The non-action key you pressed : " + "\""
							+ keyText + "\"");
					int keyCode = e.getKeyCode();
					switch (keyCode) {
					case KeyEvent.VK_CONTROL:
						System.out.print(", Control key is pressed");
						break;
					case KeyEvent.VK_SHIFT:
						System.out.print(", Shift key is pressed");
						break;
					case KeyEvent.VK_ALT:
						System.out.print(", Alt key is pressed");
						break;
					}
				}
				System.out.println();
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				// the integer code for an actual key on the keyboard. 
				// (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)
				System.out.println("The key you released : " +  "\"" +
						KeyEvent.getKeyText(e.getKeyCode()) + "\"");
			}

		});
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		KeyEvent_1 frame = new KeyEvent_1();
		frame.setTitle("KeyEvent Test");
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0, 0, 600, 300);
	}

}


posted on 2012-07-12 05:13  java课程设计例子  阅读(494)  评论(0编辑  收藏  举报