按键监听类KeyListener及适配器改良方案

 导包导的恶心,真是马上要对notepad累觉不爱了,好在顺利导完。。


监听按键信息
//source code

import java.awt.Color ;
import java.awt.Font ;
import java.awt.GridLayout ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.awt.event.WindowListener ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.awt.event.KeyEvent ;
import java.awt.event.KeyListener ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JTextField ;
import javax.swing.JTextArea ;
import javax.swing.JScrollPane ;
import javax.swing.JPasswordField ;

class MyHandleKey extends JFrame implements KeyListener
{
    JTextArea text = new JTextArea() ;
    public MyHandleKey()
    {
        super.setTitle("按键精灵") ;
        JScrollPane sc = new JScrollPane(text) ;
        sc.setBounds(5,5,300,200) ;
        super.add(sc) ;
        text.addKeyListener(this) ;
       
        super.addWindowListener(new WindowAdapter()
        {
           public void WindowClosing(WindowEvent e)
           {
               System.exit(1) ;
           }
        }) ;
       
        super.setSize(400,400) ;
        super.setVisible(true) ;
    }
   
    public void keyPressed(KeyEvent e)    //按键被按下
    {
        text.append(" "+KeyEvent.getKeyText(e.getKeyCode())+" 键被按下\n") ;
    }
   
    public void keyReleased(KeyEvent e)     //按键松开
    {
        text.append(" "+KeyEvent.getKeyText(e.getKeyCode())+"被松开\n") ;
    }
   
    public void keyTyped(KeyEvent e)
    {
        text.append("输入的内容是:"+e.getKeyChar()+"\n") ;
    }
} ;

class Tester
{
   public static void main(String args[])
   {
      MyHandleKey mhk = new MyHandleKey() ;
   }
} ;

与鼠标监听同样的问题,这样写代码太冗余,下面提供适配器改良

import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.awt.event.WindowListener ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.awt.event.KeyEvent ;
import java.awt.event.KeyAdapter ;   //ferfect
import java.awt.event.KeyListener ;
import javax.swing.JTextArea ;
import javax.swing.JFrame ;
import javax.swing.JScrollPane ;
import javax.swing.JPasswordField ;

class MyHandleKey extends JFrame
{
    JTextArea text = new JTextArea() ;
    public MyHandleKey()
    {
        super.setTitle("按键精灵") ;
        JScrollPane sc = new JScrollPane(text) ;
        sc.setBounds(5,5,300,200) ;
        super.add(sc) ;
        text.addKeyListener(new KeyAdapter()    //再次应用Adapter
        {
            public void keyPressed(KeyEvent e)
            {  
                //text.setText("") ;
                text.append(KeyEvent.getKeyText(e.getKeyCode())+" 被按下\n") ;
            }
        }) ;
       
        super.addWindowListener(new WindowAdapter()
        {
           public void WindowClosing(WindowEvent e)
           {
               System.exit(1) ;
           }
        }) ;
       
        super.setSize(400,400) ;
        super.setVisible(true) ;
    }
   
} ;

class Tester
{
   public static void main(String args[])
   {
      MyHandleKey mhk = new MyHandleKey() ;
   }
} ;


posted @ 2015-04-07 10:37  emoji  阅读(658)  评论(0编辑  收藏  举报