我是靠谱客的博主 缓慢蛋挞,最近开发中收集的这篇文章主要介绍【Java】鼠标监听器MouseListener、MouseMotionListener和MouseWheelListener的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

鼠标监听器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("滑轮向后....");
        }
    }
}



最后

以上就是缓慢蛋挞为你收集整理的【Java】鼠标监听器MouseListener、MouseMotionListener和MouseWheelListener的使用的全部内容,希望文章能够帮你解决【Java】鼠标监听器MouseListener、MouseMotionListener和MouseWheelListener的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部