【Java学习笔记】52:MouseEvent事件的处理

简述

不止一个接口用于处理MouseEvent事件:

如果要处理在任何组件上都可以发生的按住、释放、移入组件、移出组件、单击触发的MouseEvent事件,应让监听器实现MouseListener接口。

如果要处理任何组件上都可以发生的拖动鼠标移动鼠标时候触发的MouseEvent事件,应让监听器实现MouseMotionListener接口。

MouseListener接口的例子

读取鼠标发生某些事件的位置,在文本区域显示出来。

MouseEvent类的对象携带了不止getX()和getY()的更多的获取发生MouseEvent事件时的信息的方法。

MousePolice.java
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//作为MouseEvent事件的监听器,需要实现接口中的5个方法
public class MousePolice implements MouseListener{
    JTextArea jta;

    //获取所需资源
    public void setTextArea(JTextArea jta) {
        this.jta=jta;
    }

    //单击
    @Override
    public void mouseClicked(MouseEvent e) {
        //多次单击即是连击
        if(e.getClickCount()>=2)
            jta.append("连击在:"+e.getX()+","+e.getY()+"\n");
    }

    //进入
    @Override
    public void mouseEntered(MouseEvent e) {
        //getSource直接获得事件源实例
        if(e.getSource() instanceof JButton)
            jta.append("进入按钮在:"+e.getX()+","+e.getY()+"\n");
        if(e.getSource() instanceof JTextField)
            jta.append("进入文本框在:"+e.getX()+","+e.getY()+"\n");
        if(e.getSource() instanceof JFrame)
            jta.append("进入窗口在:"+e.getX()+","+e.getY()+"\n");
    }

    //退出
    @Override
    public void mouseExited(MouseEvent e) {
        jta.append("退出在:"+e.getX()+","+e.getY()+"\n");
    }

    //按下
    @Override
    public void mousePressed(MouseEvent e) {
        jta.append("按下在:"+e.getX()+","+e.getY()+"\n");
    }

    //松开
    @Override
    public void mouseReleased(MouseEvent e) {
        jta.append("释放在:"+e.getX()+","+e.getY()+"\n");
    }
}
WindowMouse.java
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class WindowMouse extends JFrame{
    JTextField jtf;
    JButton jb;
    JTextArea jta;
    MousePolice mp;

    WindowMouse() {
        init();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void init() {
        this.setLayout(new FlowLayout());
        jtf=new JTextField(8);
        jb=new JButton("按钮");
        jta=new JTextArea(5,28);
        mp=new MousePolice();

        mp.setTextArea(jta);//为监听器传入资源

        //注册监听器
        jb.addMouseListener(mp);
        jta.addMouseListener(mp);
        jtf.addMouseListener(mp);
        this.addMouseListener(mp);

        this.add(jb);
        this.add(jtf);
        this.add(new JScrollPane(jta));

    }
}
Main.java
public class Main {

    public static void main(String[] args) {
        WindowMouse wm=new WindowMouse();
        wm.setTitle("处理MouseEvent事件");
        wm.setBounds(10,10,360,200);
    }

}
运行

这里写图片描述

MouseMotionListener接口的例子

在层次面板上拖动一个JButton的例子。

注意鼠标发生事件时位置是以事件源组件为坐标系,而组件(左上角)所处的是容纳它的容器的坐标系,拖动的新坐标公式的图示:
这里写图片描述

LP.java
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;

//中间容器层次面板,直接实现了监听器该实现的接口
//所以其中的组件按钮直接拿自己这个类做监听器就可以了
//这也是比较方便的编程方式
public class LP
    extends JLayeredPane
    implements MouseListener,MouseMotionListener{

    JButton jb;
    int x,y,a,b,x0,y0;

    LP(){
        jb=new JButton("被拖动的按钮");
        jb.addMouseListener(this);
        jb.addMouseMotionListener(this);
        this.setLayout(new FlowLayout());
        //把按钮添加进来
        this.add(jb, JLayeredPane.DEFAULT_LAYER);   
    }

    //MouseListener:按下
    @Override
    public void mousePressed(MouseEvent e) {
        //除顶层容器外所有Swing组件的基类
        JComponent jc=null;
        jc=(JComponent)e.getSource();//获得事件源,并向上转换

        //使用setLayer方法把组件设置在层次面板中的哪一层中出现
        //但是程序并没 有把该组件添加到该层中
        //在使用setLayer方法后还应使用add方法把组件添加进层面板的这一层中
        //而且add方法 应出现在setLayer方法之后
        this.setLayer(jc, JLayeredPane.DRAG_LAYER);

        //获取事件源(左上角)位置坐标
        a=jc.getBounds().x;
        b=jc.getBounds().y;
        //获取鼠标在事件源中的位置坐标
        //这里的x0,y0也就是按下时候的坐标
        x0=e.getX();
        y0=e.getY();
    }

    //MouseListener:释放
    @Override
    public void mouseReleased(MouseEvent e) {
        JComponent jc=null;
        jc=(JComponent)e.getSource();
        //拖拽完成后,放回原来那一层去
        this.setLayer(jc, JLayeredPane.DEFAULT_LAYER);
    }

    //MouseListener:进入
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    //MouseListener:单击
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    //MouseListener:退出
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    //MouseMotionListener:移动
    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    //MouseMotionListener:拖动
    @Override
    public void mouseDragged(MouseEvent e) {
        Component cpnt=null;//最大的组件类
        //如果事件源是组件,也不管是什么组件
        if(e.getSource() instanceof Component) {
            cpnt=(Component)e.getSource();//先向上转换成组件类对象
            //组件(左上角)坐标
            a=cpnt.getBounds().x;
            b=cpnt.getBounds().y;
            //拖动的实时坐标x,y
            x=e.getX();
            y=e.getY();
            //拖动的坐标影响组件的坐标,则看起来是动态的拖动
            a=a+x;
            b=b+y;
            //设置其新位置
            //也就是说,新的位置坐标=刚才位置坐标+(当前鼠标坐标-鼠标按下坐标)
            //注意!组件的坐标是在容器坐标系中的!
            //而鼠标的坐标是在其下的组件的坐标系中的!
            cpnt.setLocation(a-x0,b-y0);
        }
    }
}
WindowMove.java
import java.awt.BorderLayout;

import javax.swing.JFrame;

public class WindowMove extends JFrame{
    //往组件里装好监听器的层次面板
    LP lp;

    WindowMove() {
        lp=new LP();
        this.add(lp,BorderLayout.CENTER);//方位布局的中央
        this.setVisible(true);
        this.setBounds(12,12,300,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Main.java
public class Main {

    public static void main(String[] args) {
        WindowMove wm=new WindowMove();
        wm.setTitle("处理MouseEvent事件");
        wm.setBounds(10,10,400,300);
    }

}
运行

这里写图片描述

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值