【Java】鼠标监听器MouseListener、MouseMotionListener和MouseWheelListener的使用

Java基础学习与总结 同时被 2 个专栏收录
134 篇文章 1 订阅
34 篇文章 2 订阅

鼠标监听器MouseListener

监听鼠标事件MouseEvent。

相应事件和处理方法

鼠标事件          处理方法
MOUSE_CLICKED    MouseClicked (MouseEvent)    鼠标点击(单或双)
MOUSE_PRESSED    MousePressed (MouseEvent)    鼠标按下
MOUSE_RELEASED   MouseReleased(MouseEvent)    鼠标松开
MOUSE_ENTERED    MouseEntered (MouseEvent)    鼠标进入(某组件区域)
MOUSE_EXITED     MouseExited  (MouseEvent)    鼠标离开(某组件区域)

鼠标事件MouseEvent常用方法

int getClickCount()      得到点击次数1 OR 2;
int getX(), int getY()   得到鼠标的(象素)位置。

鼠标监听器MouseMotionListener

对于鼠标的移动和拖放,另外用鼠标运动监听器MouseMotionListener。
因为许多程序不需要监听鼠标运动,把两者分开可简化程序。

相应事件和处理方法

鼠标事件          处理方法
MOUSE_MOVED      MouseMoved  (MouseEvent)    鼠标在移动

MOUSE_DRAGGED    MouseDragged(MouseEvent)    鼠标被拖动

范例Java程序

我们把对这两个鼠标监听器综合运用。


/*******************************************************************************
 * @project: SchoolPaint_3
 * @package: com.burns.school.paint.test
 * @file: T.java
 * @author: zhangpei
 * @created: 2018-4-25
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2018 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.school.paint.test;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test_Mouse2 implements MouseMotionListener, MouseListener {
	Frame frame = new Frame("关于鼠标的多重监听器");
	TextField tField = new TextField(30);

	public Test_Mouse2() {
		Label label = new Label("请按下鼠标左键并拖动");
		frame.add(label, "North");
		frame.add(tField, "South");
		frame.setBackground(new Color(180, 255, 255));
		frame.addMouseListener(this);
		frame.addMouseMotionListener(this);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		frame.setSize(300, 200);
		frame.setLocation(400, 250);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		new Test_Mouse2();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.print("鼠标点击----" + "\t");
		if (e.getClickCount() == 1) {
			System.out.println("单击!");
		} else if (e.getClickCount() == 2) {
			System.out.println("双击!");
		} else if (e.getClickCount() == 3) {
			System.out.println("三连击!!");
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标按下");
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标松开");
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		tField.setText("鼠标已经进入窗体");
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		tField.setText("鼠标已经移出窗体");
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		String string = "鼠标拖动到位置:(" + e.getX() + "," + e.getY() + ")";
		tField.setText(string);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
	}

}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2018 AcconSys All rights reserved.
 ******************************************************************************/



MouseWheelListener说明

mousewheelmoved

void mousewheelmoved(mousewheelevent e)

鼠标滚轮旋转时调用。

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("滑轮向后....");
        }
    }
}



展开阅读全文
  • 9
    点赞
  • 0
    评论
  • 23
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值