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

鼠标动作监听类MouseListener

时间:2015-04-07 12:09:20      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:鼠标监听

技术分享


<span style="font-size:14px;">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.MouseEvent ;
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyListener ;
import java.awt.event.MouseListener ;
import javax.swing.JTextArea ;
import javax.swing.JFrame ;
import javax.swing.JScrollPane ;
import javax.swing.JPasswordField ;

class MyMouse extends JFrame implements MouseListener
{  
        JTextArea text = new JTextArea() ;
        public MyMouse()
        {
            super.setTitle("鼠标精灵") ;
            JScrollPane scr = new JScrollPane(text) ;
            scr.setBounds(5,5,300,200) ;
            super.add(scr) ;
            text.addMouseListener(this) ;
            super.addWindowListener(new WindowAdapter()
           {
             public void WindowClosing(WindowEvent e)
            {
               System.exit(1) ;
            }
           }) ;
          
           super.setSize(400,300) ;
           super.setVisible(true) ;   
        }
       
        public void mouseClicked(MouseEvent e)    //得到鼠标键值
       {
           int c = e.getButton() ;
           String mouseinfo = null;
           if(c==MouseEvent.BUTTON1)
           {
              mouseinfo = "左键\n" ;
           }
           if(c==MouseEvent.BUTTON2)
           {
              mouseinfo = "滚轮\n" ;
           }
           if(c==MouseEvent.BUTTON3)
           {
              mouseinfo = "右键\n" ;
           }
           text.append("被按下的是"+mouseinfo) ;
       }
       
        public void mouseEntered(MouseEvent e)   //鼠标进入组件
        {
           text.append("鼠标进入组件\n") ;
        }
       
        public void mouseExited(MouseEvent e)     //鼠标离开组件
        {
            text.append("鼠标离开组件\n") ;
        }
       
        public void mousePressed(MouseEvent e)   //鼠标被按下
        {
           text.append("鼠标被按下\n") ;
        }
       
        public void mouseReleased(MouseEvent e)   //鼠标被放开
        {
           text.append("鼠标被松开\n") ;
        }
      
   
} ;

class Tester
{
   public static void main(String args[])
   {
      MyMouse mhk = new MyMouse() ;
   }
} ;
</span>


鼠标动作监听类MouseListener

标签:鼠标监听

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

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