- SWING 教程
- SWING - 主页
- SWING - 概述
- SWING - 环境
- SWING - 控件
- SWING - 事件处理
- SWING - 事件类
- SWING - 事件监听器
- SWING - 事件适配器
- SWING - 布局
- SWING - 菜单
- SWING - 容器
- SWING 实用资源
- SWING - 快速指南
- SWING - 实用资源
- SWING - 讨论
SWING - MouseMotionAdapter 类
简介
MouseMotionAdapter 类是一个抽象(适配器)类,用于接收鼠标移动事件。该类中的所有方法都为空。该类是用于创建侦听器对象的便利类。
类声明
以下是 java.awt.event.MouseMotionAdapter 类的声明 -
public abstract class MouseMotionAdapter
extends Object
implements MouseMotionListener
类构造方法
| 序号 | 构造方法及说明 |
|---|---|
| 1 |
MouseMotionAdapter() |
类方法
| 序号 | 方法及说明 |
|---|---|
| 1 |
void mouseDragged(MouseEvent e) 当鼠标按钮按在组件上,然后拖动时调用。 |
| 2 |
void mouseMoved(MouseEvent e) 当鼠标光标移动到组件上,但任何按钮没有按下时调用。 |
继承的方法
本类从下列类继承方法 -
- java.lang.Object
MouseMotionAdapter 示例
使用任意编辑器创建以下 Java 程序,例如 D:/ > SWING > com > tutorialspoint > gui >
SwingAdapterDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class SwingAdapterDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingAdapterDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo();
swingAdapterDemo.showMouseMotionAdapterDemo();
}
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 showMouseMotionAdapterDemo(){
headerLabel.setText("Listener in action: MouseMotionAdapter");
JPanel panel = new JPanel();
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() +")");
}
});
JLabel msglabel
= new JLabel("Welcome to TutorialsPoint SWING Tutorial.",JLabel.CENTER);
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
使用命令提示符编译程序。转到 D:/ > SWING,然后键入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingAdapterDemo.java
如果未发生错误,则表示编译成功。使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingAdapterDemo
验证以下输出。
swing_event_adapters.htm
广告