AWT MouseMotionAdapter 类

简介

MouseMotionAdapter 是用于接收鼠标移动事件的抽象(适配器)类。此类的所有方法均为空。此类是创建侦听器对象的便利类。

类声明

以下是 java.awt.event.MouseMotionAdapter 类的声明:

public abstract class MouseMotionAdapter
    extends Object
        implements MouseMotionListener

类构造函数

S.N.构造函数 &描述
1

MouseMotionAdapter()

类方法

S.N.方法 &描述
1

void mouseDragged(MouseEvent e)

当鼠标按钮在组件上按下然后拖动时调用。

2

void mouseMoved(MouseEvent e)

当鼠标光标移动到组件上但未按下任何按钮时调用。

继承的方法

此类从以下类继承方法:

  • java.lang.Object

MouseMotionAdapter 示例

使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui > 中创建以下 Java 程序

AwtAdapterDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtAdapterDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtAdapterDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtAdapterDemo  awtAdapterDemo = new AwtAdapterDemo();        
      awtAdapterDemo.showMouseMotionAdapterDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

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

   private void showMouseMotionAdapterDemo(){
      headerLabel.setText("Listener in action: MouseMotionAdapter");      

      Panel panel = new Panel();      
      panel.setBackground(Color.magenta);
      panel.setLayout(new FlowLayout());        
      panel.addMouseMotionListener(new MouseMotionAdapter(){
         public void mouseMoved(MouseEvent e) {
            statusLabel.setText("Mouse Moved: ("+e.getX()+", "+e.getY() +")");
         }                
      });

      Label msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
      panel.add(msglabel);

      controlPanel.add(panel);

      mainFrame.setVisible(true);  
   }
}

使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。

D:\AWT>javac com	utorialspoint\gui\AwtAdapterDemo.java

如果没有错误,则表示编译成功。使用以下命令运行程序。

D:\AWT>java com.tutorialspoint.gui.AwtAdapterDemo

验证以下输出

AWT MouseMotionAdapter

awt_event_adapters.html