Java API 分類導覽 - java.awt.event.MouseListener mousePressed()

MouseListener 介面 (interface) 的 mousePressed() 方法 (method) 用來設定按住滑鼠按鈕的動作。



修飾子與參數
void mousePressed(MouseEvent e)


mousePressed() 沒有回傳值 (return value) ,參數 (paramenter) 為 MouseEvent 型態 (type) 的物件 (object) 。


舉例如下
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 已經實作 MouseListener 介面
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");
}


編譯後執行,結果如下



中英文術語對照
介面interface
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
事件處理


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


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

沒有留言: