Java API 分類導覽 - java.awt.event.MouseMotionAdapter

MouseMotionAdapter 類別 (class) 已經實作 MouseMotionListener 介面 (interface) ,因此可直接改寫 mouseDragged() 、 mouseMoved() ,不需要每個都實作。



修飾子
public abstract class MouseMotionAdapter


父類別
java.lang.Object


實作介面
MouseMotionListener
EventListener


建構子
public MouseMotionAdapter()


舉例如下
import java.awt.*;
import java.awt.event.*;
 
public class MouseMotionAdapterDemo extends MouseMotionAdapter {
    Frame frame;
    Panel panel;
    Label text;
    
    public static void main(String[] args) {
        new MouseMotionAdapterDemo();
    }
     
    public MouseMotionAdapterDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setSize(600, 400);
        
        panel = new Panel();
        panel.addMouseMotionListener(this);
        
        text = new Label("something happened..");
        
        panel.add(text);
        frame.add(panel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    
    // from MouseMotionListener
    public void mouseDragged(MouseEvent e) {
        text.setText("x: " + e.getX() + ", y: " + e.getY());
        System.out.println("mouseDragged");
     }
    
    public void mouseMoved(MouseEvent e) {
        text.setText("x: " + e.getX() + ", y: " + e.getY());
        System.out.println("mouseMoved");
    }
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:MouseMotionAdapterDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例的 MouseMotionAdapterDemo 直接繼承自 MouseMotionAdapter 類別
public class MouseMotionAdapterDemo extends MouseMotionAdapter {


MouseMotionListener 的 mouseDragged() 處理滑鼠拖曳的動作,所謂拖曳是指按住滑鼠按鈕然後同時移動滑鼠, mouseMoved() 則為處理滑鼠移動的動作
// from MouseMotionListener
public void mouseDragged(MouseEvent e) {
   text.setText("x: " + e.getX() + ", y: " + e.getY());
   System.out.println("mouseDragged");
}
    
public void mouseMoved(MouseEvent e) {
    text.setText("x: " + e.getX() + ", y: " + e.getY());
    System.out.println("mouseMoved");
}


編譯後執行,結果如下



中英文術語對照
類別class
介面interface


您可以繼續參考
事件處理


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseMotionAdapter.html

沒有留言: