当前位置:编程学习 > JAVA >>

Java键盘事件监听(1)

\

[java]
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(); 
                 &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,