橘子味的心
标题:Swing MouseAdapter类

MouseAdapter类是一个用于接收鼠标事件的抽象(适配器)类。此类的所有方法都是空的。此类是用于创建监听器对象的便捷类。

类声明

以下是java.awt.event.MouseAdapter类的声明 -

public abstract class MouseAdapter
   extends Object
      implements MouseListener, MouseWheelListener, MouseMotionListener

类构造函数

编号 构造函数 描述说明
1 MouseAdapter() 无参数构造函数

类方法

编号 类方法 描述说明
1 void mouseClicked(MouseEvent e) 在组件上单击(按下并释放)鼠标按钮时调用。
2 void mouseDragged(MouseEvent e) 在组件上按下鼠标按钮然后拖动时调用。
3 void mouseEntered(MouseEvent e) 鼠标进入组件时调用。
4 void mouseExited(MouseEvent e) 鼠标退出组件时调用。
5 void mouseMoved(MouseEvent e) 将鼠标光标移动到组件但未按下任何按钮时调用。
6 void mousePressed(MouseEvent e) 在组件上按下鼠标按钮时调用。
7 void mouseReleased(MouseEvent e) 在组件上释放鼠标按钮时调用。
8 void mouseWheelMoved(MouseWheelEvent e) 旋转鼠标滚轮时调用。

方法继承

该类继承以下类中的方法 -

  • java.lang.Object

MouseAdapter示例

使用编辑器创建以下Java程序:MouseAdapterDemo.java

package com.yiibai.swing.listener;

import javax.swing.*;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;

public class MouseAdapterDemo {
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public MouseAdapterDemo() {
        prepareGUI();
    }

    public static void main(String[] args) {
        MouseAdapterDemo swingAdapterDemo = new MouseAdapterDemo();
        swingAdapterDemo.showMouseAdapterDemo();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Java SWING Examples");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));

        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);
        statusLabel.setSize(350, 100);

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void showMouseAdapterDemo() {
        headerLabel.setText("Listener in action: MouseAdapter");
        JPanel panel = new JPanel();
        panel.setBackground(Color.blue);
        panel.setLayout(new FlowLayout());

        panel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                statusLabel.setText("Mouse Clicked: (" + e.getX() + ", " + e.getY() + ")");
            }
        });
        JLabel msglabel = new JLabel("欢迎您来到易百教程/Swing", JLabel.CENTER);

        msglabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                statusLabel.setText("Mouse Clicked: (" + e.getX() + ", " + e.getY() + ")");
            }
        });
        panel.add(msglabel);
        controlPanel.add(panel);
        mainFrame.setVisible(true);
    }
}

执行上面示例代码,得到以下结果:


目录

分类