자바 Event 핸들러 MouseMotionListener, MouseMotionAdapter와 MouseEvent

자바 Event 핸들러 MouseMotionListener, MouseMotionAdapter와 MouseEvent


MouseMotionListener, MouseMotionAdapter와 MouseEvent


  • . MouseMotion 타입의 Event는 마우스의 움직임과 관련된 행위를 처리하는 Event이다
  • . 메서드(Method)
  • public void mouseMoved(MouseEvent e){}
  • public void MouseDragged(MouseEvent e){}

ex )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

import java.awt.*;
import java.awt.event.*;

class Round19_Ex07_Sub extends Frame implements MouseMotionListener {// 임플리먼츠
// 시킨다.
private Label lb = new Label("x = 000, y = 000", Label.RIGHT);
private Label lb1 = new Label("x = 000, y = 000");// 모션 좌표의 초기값

public Round19_Ex07_Sub() {
super("Test");
this.init();
this.start();
this.setSize(500, 500);
this.setVisible(true);
}

public void init() {
this.setLayout(new BorderLayout());
this.add("North", lb);
this.add("South", lb1);
}

public void start() {
lb.addMouseMotionListener(this);
lb1.addMouseMotionListener(this);
// MouseMotion을 Listener 시킨다 즉 lb에 마우스가 올라오면 실행한다
}

public void mouseMoved(MouseEvent e) {
// Listener에서 마우스가 움직이면 MouseEvent의 매게변수를 가져온다
int xpos = e.getX();// e.getX는 마우스의 x좌표를 int형으로 가져온다
int ypos = e.getY();// e,getY는 마우스의 y좌표를 int형으로 가져온다
lb.setText("x = " + xpos + ", y = " + ypos);
// 가져온 x와 y의 좌표를 lb에 setText한다
}

public void mouseDragged(MouseEvent e) {
// 마우스의 키를 누른상태에서 즉 Dragged할때 발생하는 이벤트
int xpos = e.getX();// e.getX는 마우스의 x좌표를 int형으로 가져온다
int ypos = e.getY();// e.getY는 마우스의 y좌표를 int형으로 가져온다
lb1.setText("x = " + xpos + ", y = " + ypos);
// 가져온 x와 y의 좌표를 lb1에 setText한다
}
}

public class Round19_Ex07 {
public static void main(String[] ar) {
Round19_Ex07_Sub ms = new Round19_Ex07_Sub();
}
}

Share