本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

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

MouseMotionListener 介面 (interface) 進行滑鼠移動事件的處理,需要實作 mouseDragged() 、 mouseMoved() 方法 (method) ,其參數 (parameter) e 為 MouseEvent 物件 (object)



修飾子
public interface MouseMotionListener


父介面
java.util.EventListener


實作方法
名稱敘述
mouseDragged()處理滑鼠拖曳事件
mouseMoved()處理滑鼠移動事件


舉例如下
import java.awt.*;
import java.awt.event.*;
 
public class MouseAdapterDemo extends MouseAdapter {
    Frame frame;
    Panel panel;
    Label text;
    
    public static void main(String[] args) {
        new MouseAdapterDemo();
    }
     
    public MouseAdapterDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setSize(600, 400);
        
        panel = new Panel();
        panel.addMouseListener(this);
        panel.addMouseWheelListener(this);
        panel.addMouseMotionListener(this);
        
        text = new Label("something happened..");
        
        panel.add(text);
        frame.add(panel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    
    // from MouseListener
    public void mouseClicked(MouseEvent e) {
        text.setText("mouseClicked");
        System.out.println("mouseClicked");
    }
    
    public void mousePressed(MouseEvent e) {
        text.setText("mousePressed");
        System.out.println("mousePressed");
    }
    
    public void mouseReleased(MouseEvent e) {
        text.setText("mouseReleased");
        System.out.println("mouseReleased");
    }
    
    public void mouseEntered(MouseEvent e) {
        text.setText("mouseEntered");
        System.out.println("mouseEntered");
    }
    
    public void mouseExited(MouseEvent e) {
        text.setText("mouseExited");
        System.out.println("mouseExited");
    }

    // from MouseWheelListener
    public void mouseWheelMoved(MouseWheelEvent e) {
        text.setText("mouseWheelMoved");
        System.out.println("mouseWheelMoved");
    }

    // from MouseMotionListener
    public void mouseDragged(MouseEvent e) {
        text.setText("mouseDragged");
        System.out.println("mouseDragged");
    }
    
    public void mouseMoved(MouseEvent e) {
        text.setText("mouseMoved");
        System.out.println("mouseMoved");
    }
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

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


此例的 MouseAdapterDemo 直接繼承自 MouseAdapter 類別, MouseAdapter 已經實作 MouseMotionListener 介面
public class MouseAdapterDemo extends MouseAdapter {


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


編譯後執行,結果如下



中英文術語對照
介面interface
方法method
參數parameter
物件object


您可以繼續參考
事件處理


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


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

沒有留言: