码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-07 12:02:52      阅读:203      评论:0      收藏:0      [点我收藏+]

标签: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() ;
   }
} ;


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

标签:keylistener

原文地址:http://blog.csdn.net/u012566693/article/details/44917235

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!