JAVA/Java

Frame에 MouseMotionListener 사용하는 방법01 [JAVA]

민돌이 2019. 7. 17. 19:11
반응형

01. MouseMotionListener를 implements로 받아 사용하는 방법

public class TimerTest extends Frame implements MouseMotionListener {
	static int timer = 0;

	TimerTest(){
		addMouseMotionListener(this);  
		
		setSize(300,300);  
		setLayout(null);  
		setVisible(true);  
	}
	
	public static void main(String[] args) {
		new TimerTest();
		testGo();
	}
	
	public static void testGo() {
		while (true) {
			try {
				System.out.println(timer);
				Thread.sleep(60000);
				if ( timer == 15 ) {
					return;
				}
                timer++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("Mouse moved : "+ timer);
		timer = 0;
	}
	
}

 

이 코드는 timer가 15가 되면 종료하게 되어지며,

while문과 sleep를 이용하여 수행할 수 있게 합니다.

또한 생성되어진 영역안에서 마우스를 움직이면 timer가 0으로 초기화 되는 코드입니다.

 

 

02. 결과

영역에서 마우스 움직일 시 0으로 초기화

흰색 안에 영역에서 마우스를 움직이게 되면 0으로 초기화 되는 모습입니다.

 

 

다음에는 MouseMotionListener 사용하는 방법02로 돌아오겠습니다 :)

반응형