修飾子與參數
void mouseDragged(MouseEvent e)
mouseDragged() 沒有回傳值 (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 已經實作 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 |
回傳值 | return value |
型態 | type |
物件 | object |
參數 | parameter |
您可以繼續參考
事件處理
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseMotionListener.html
沒有留言:
張貼留言