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

MouseAdapter 類別 (class) 已經實作 MouseListener 、 MouseMotionListener 、 MouseWheelListener 介面 (interface) ,因此可直接改寫 MouseListener 的 mouseClicked() 、 mousePressed() 、 mouseReleased() 、 mouseEntered() 、 mouseExited() , MouseMotionListener 的 mouseDragged() 、 mouseMoved() , MouseWheelListener 的 mouseWheelMoved() , 不需要每個都實作。



修飾子
public abstract class MouseAdapter


父類別
java.lang.Object


實作介面
MouseListener
MouseMotionListener
MouseWheelListener
EventListener


建構子
public MouseAdapter()


舉例如下
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 類別
public class MouseAdapterDemo extends MouseAdapter {


MouseListener 的 mouseClicked() 處理滑鼠點擊的動作, mousePressed() 處理按住滑鼠鍵的動作, mouseReleased() 處理釋放滑鼠鍵的動作, mouseEntered() 處理滑鼠進入指定元件的動作, mouseExited() 處理滑鼠離開指定元件的動作
// 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");
}


MouseWheelListener 的 mouseWheelMoved() 處理滑鼠滾輪滾動的動作
// from MouseWheelListener
public void mouseWheelMoved(MouseWheelEvent e) {
    text.setText("mouseWheelMoved");
    System.out.println("mouseWheelMoved");
}


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");
}


編譯後執行,結果如下



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


您可以繼續參考
事件處理


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


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

沒有留言: