標題:
滑鼠事件
[打印本頁]
作者:
周政輝
時間:
2018-3-3 11:30
標題:
滑鼠事件
一、滑鼠事件說明
視窗應用程式中,滑鼠是最常使用的輸入工具。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次後,圖片左上角對準至滑鼠指標位置.
[attach]3471[/attach]
[attach]3472[/attach]
作者:
黃茂勛
時間:
2018-3-3 11:48
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int x2, y2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 379);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ImageIcon icon = new ImageIcon(Main.class.getResource("l02.png"));
JLabel lblNewLabel = new JLabel();
lblNewLabel.setBounds(165, 132, 117, 102);
lblNewLabel.setIcon(icon);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x2 = e.getX()-50;
y2 = e.getY()-70;
if(e.getButton() == 1)
lblNewLabel.setLocation(x2, y2);
}
});
contentPane.add(lblNewLabel);
}
}
複製代碼
作者:
蔡季樺
時間:
2018-3-3 11:50
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int x,y;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/kd001.png")));
lblNewLabel.setBounds(114, 83, 102, 102);
contentPane.add(lblNewLabel);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
x = arg0.getX();
y = arg0.getY();
lblNewLabel.setLocation(x-51,y-51);
}
});
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-3-3 12:01
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Amove extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Amove frame = new Amove();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Amove() {
icon = new ImageIcon(crocodile.class.getResource("A-icon.png"));
Image img = icon.getImage().getScaledInstance(32,32,java.awt.Image.SCALE_FAST );
ImageIcon newicon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Alabel = new JLabel(newicon);
Alabel.setBounds(144, 70, 32, 32);
contentPane.add(Alabel);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int x = arg0.getX();
int y = arg0.getY();
Alabel.setLocation(x, y);
}
});
}
}
複製代碼
作者:
張健勳
時間:
2018-3-4 21:48
本帖最後由 張健勳 於 2018-3-4 22:22 編輯
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int x,y;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("icon.png")));
setTitle("\u6ED1\u9F20\u6307\u6A19\u727D\u5F15\u5716\u6848");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lb = new JLabel("");
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent a) {
x = a.getX();
y = a.getY();
lb.setLocation(x,y);
}
});
lb.setIcon(new ImageIcon(Main.class.getResource("icon.png")));
lb.setBounds(10, 10, 89, 79);
contentPane.add(lb);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2