標題:
滑鼠事件
[打印本頁]
作者:
tonyh
時間:
2019-5-25 14:23
標題:
滑鼠事件
一、
滑鼠事件說明
視窗應用程式中,滑鼠是最常使用的輸入工具。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();
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-5-25 15:07
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();
}
}
複製代碼
作者:
蔡幸融
時間:
2019-6-10 19:23
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Ch87 implements MouseListener
{
JFrame fm;
ImageIcon ic,ii;
JLabel lb;
int x=200,y=100;
Ch87()
{
ii=new ImageIcon(Ch87.class.getResource("pic/santa.png"));
ic=new ImageIcon(Ch87.class.getResource("pic/star.png"));
lb=new JLabel(ii);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("MouseEvent");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
fm.addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
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);
}
@Override
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new Ch87();
}
}
複製代碼
作者:
莊旻叡
時間:
2019-6-10 19:37
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();
}
}
複製代碼
作者:
黃宥鈞
時間:
2019-6-22 14:22
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();
}
}
複製代碼
作者:
高睿辰
時間:
2019-6-22 14:58
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://seed.istak.org.tw/)
Powered by Discuz! 7.2