- 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
廣告