欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java抓取鼠标事件和鼠标滚轮事件示例

程序员文章站 2024-02-28 23:15:46
java抓取鼠标事件和滚轮事件 复制代码 代码如下:package demo; import java.awt.event.mouseevent;import java...

java抓取鼠标事件和滚轮事件

复制代码 代码如下:

package demo;

import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
import java.awt.event.mousewheelevent;
import java.awt.event.mousewheellistener;

import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;

public class mousedemo extends jframe  implements mouselistener,mousewheellistener{

    static int chang=400,kuang=400;
    public mousedemo(){
    jpanel j=new jpanel();
    jbutton jb=new jbutton("click");

    j.add(jb);
    this.add(j);

    this.setvisible(true);
    this.setsize(chang,kuang);

    jb.addmouselistener(this);
    this.addmousewheellistener(this);

    }

    public   void mouseclicked(mouseevent e){

        //左击事件
        if(e.getbutton()==mouseevent.button1)
            system.out.println("zuo ji shi jian ....");

        //滑轮按下事件
        if(e.getbutton()==mouseevent.button2)
            system.out.println("zhong  ji shi jian ....");

        //右击事件
        if(e.getbutton()==mouseevent.button3)
            system.out.println("you ji shi jian ....");

    }

    public static void main(string[] args) {
        // todo auto-generated method stub
        new mousedemo();
    }

    @override
    public void mouseentered(mouseevent e) {
        // todo auto-generated method stub

    }

    @override
    public void mouseexited(mouseevent e) {
        // todo auto-generated method stub

    }

    @override
    public void mousepressed(mouseevent e) {
        // todo auto-generated method stub

    }

    @override
    public void mousereleased(mouseevent e) {
        // todo auto-generated method stub

    }

    @override
    //利用滚轮事件将窗口放大和缩小
    public void mousewheelmoved(mousewheelevent e) {
        // todo auto-generated method stub
        if(e.getwheelrotation()==1){
            chang+=10;
            kuang+=10;
            this.setsize(chang,kuang);
            this.validate();
            system.out.println("滑轮向前。。。。");
        }
        if(e.getwheelrotation()==-1){
            chang-=10;
            kuang-=10;
            this.setsize(chang,kuang);
            this.validate();
            system.out.println("滑轮向后....");
        }
    }
}