標題:
滑鼠事件
[打印本頁]
作者:
tonyh
時間:
2016-5-21 17:45
標題:
滑鼠事件
本帖最後由 tonyh 於 2017-6-16 21:28 編輯
一、
滑鼠事件說明
視窗應用程式中,滑鼠是最常使用的輸入工具。Java用滑鼠來處理觸發事件的傾聽機制,方式有「實作MouseListener介面」、「繼承MouseAdapter類別」、「實作MouseMotionListener介面」、「繼承MouseMotionAdapter類別」,在本單元我們將練習實作MouseListener介面。
二、
MouseListener介面與MouseAdapter類別皆提供了五個滑鼠事件處理方法
1. void mouseClicked(MouseEvent e)
當滑鼠鍵被按一下時所觸發的事件,包括按下及放開的過程。
2. void mousePressed(MouseEvent e)
當滑鼠鍵被按下時所觸發的事件。
3. void mouseReleased(MouseEvent e)
當已按下的滑鼠鍵被放開時所觸發的事件。
4. void mouseEntered(MouseEvent e)
當滑鼠指標移入來源物件時所觸發的事件。
5. void mouseExited(MouseEvent e)
當滑鼠指標從來源物件移出來時所觸發的事件。
三、
MouseMotionListener介面與MouseMotionAdapter類別皆提供了二個滑鼠事件處理方法
1. void mouseMoved(MouseEvent e)
當滑鼠指標在來源物件內移動時所觸發的事件。
2. void mouseDragged(MouseEvent e)
當滑鼠指標拖曳來源物件時所觸發的事件。
四、
MouseEvent類別常用的方法
1. int getX()
傳回滑鼠指標在視窗物件內的水平座標。
2. int getY()
傳回滑鼠指標在視窗物件內的垂直座標。
3. int getClickCount()
傳回滑鼠鍵被按了幾下。
4. int getButton()
傳回滑鼠被按下或放開的鍵是哪一個按鍵。若傳回1,表示左鍵;傳回2,表示中鍵;傳回3,表示右鍵。
五、
實作MouseListener介面
滑鼠事件的「事件來源」就是視窗物件,因此要使用 addMouseListener(this) 敘述將視窗物件與滑鼠事件傾聽者連結在一起。
實作點擊滑鼠左鍵1次後,圖片左上角對準至滑鼠指標位置.
實作點擊滑鼠左鍵2次後,圖片正中央對準至滑鼠指標位置.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch98 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target;
private int x=100,y=100;
Ch98()
{
icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
target=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
lb=new JLabel(target);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("滑鼠指標牽引圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==1)
{
x=e.getX()-3;
y=e.getY()-25;
}
if(e.getClickCount()==2)
{
x=e.getX()-3-64;
y=e.getY()-25-64;
}
lb.setLocation(x, y);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch98();
}
}
複製代碼
作者:
李允軒
時間:
2016-5-28 15:06
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch98 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,icon2;
private int x=100,y=100;
Ch98()
{
icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("滑鼠指標牽引圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==1)
{
x=e.getX()-3;
y=e.getY()-25;
}
if(e.getClickCount()==2)
{
x=e.getX()-3-64;
y=e.getY()-25-64;
}
lb.setLocation(x, y);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch98();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-5-28 15:26
import javax.swing.*;
import java.awt.event.*;
public class Ch98 implements MouseListener{
private JFrame fm=new JFrame("滑鼠指標牽引圖形");;
private JLabel lb;
private ImageIcon icon,target;
private int x=100,y=100;
Ch98()
{
icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
target=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
lb=new JLabel(target);
lb.setBounds(x, y, 128, 128);
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==1)
{
x=e.getX()-3;
y=e.getY()-25;
}
if(e.getClickCount()==2)
{
x=e.getX()-3-64;
y=e.getY()-25-64;
}
lb.setLocation(x, y);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch98();
}
}
複製代碼
作者:
劉得恩
時間:
2016-5-28 16:23
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch98 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target;
private int x=100,y=100;
Ch98()
{
icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
target=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
lb=new JLabel(target);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("滑鼠指標牽引圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==1)
{
x=e.getX()-3;
y=e.getY()-25;
}
if(e.getClickCount()==2)
{
x=e.getX()-3-64;
y=e.getY()-25-64;
}
lb.setLocation(x, y);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch98();
}
}
複製代碼
作者:
林宇翔
時間:
2016-5-28 16:33
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class ch98 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target;
private int x=100,y=100;
ch98()
{
icon=new ImageIcon(ch98.class.getResource("pic/star.png"));
target=new ImageIcon(ch98.class.getResource("pic/santa.png"));
lb=new JLabel(target);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("滑鼠指標牽引圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==1)
{
x=e.getX()-3;
y=e.getY()-25;
}
if(e.getClickCount()==2)
{
x=e.getX()-3-64;
y=e.getY()-25-64;
}
lb.setLocation(x,y);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new ch98();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2