Daily coding

Java Basic : day 14 - AWT & SWING 8 : 마우스리스너 본문

Language/Java_basic

Java Basic : day 14 - AWT & SWING 8 : 마우스리스너

sunnnkim 2019. 12. 8. 15:39

마우스리스너 MouseLisner

 

 

- MouseListener 인터페이스를 상속받으면 마우스 속성을 제어하고 설정할 수 있다.

 

 

* 상속 시 오버라이딩 메소드

-  e.getX(); ---> X 좌표의 값

-  e.getY(); ---> Y 좌표의 값

 

 

// 마우스 클릭 했을 때
@Override
public void mousePressed(MouseEvent e) {
         x = e.getX();
         y = e.getY();

         label.setText("x = " + x +", y = " + y);
}


@Override
public void mouseReleased(MouseEvent e) {

}
@Override
public void mouseEntered(MouseEvent e) {

}


@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

 

// 마우스 드래그 할때
@Override
public void mouseDragged(MouseEvent e) {

         x = e.getX();
         y = e.getY();

         label.setText("x = " + x +", y = " + y);
}


@Override
public void mouseMoved(MouseEvent e) {

            label.setText("x = " + e.getX() +", y = " +  e.getY());

            // 레이블이 마우스를 따라다니도록 만들기
            label.setBounds(x, y, 200, 50);

package day15.Lecture01_AWT.test13_mouseEvent;

import java.awt.Frame;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


// Mouse Event
// 마우스는 MouseListener 로 듣는다
public class WindowTest extends Frame implements WindowListener, MouseListener, MouseMotionListener {

	Label label;
	int x, y; // 마우스의 좌표값 확인할 변수
	
	
	public WindowTest() {
		// TODO Auto-generated constructor stub
	
		setLayout(null);
		
		label = new Label("x = y =");
		label.setBounds(500, 100, 200, 30);
		add(label);
		
		
		setSize(800,600);
		setLocation(0, 0);
		addWindowListener(this);
		setVisible(true);
		
        
        
        
		// 마우스 리스너 추가해줘야함
		addMouseListener(this);
		addMouseMotionListener(this);
	
    
    
	}
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.exit(0);
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}
	
	
	
	// MouseListener 이벤트 메소드 
	
	// mouseMoved : 
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	// 마우스 클릭 했을 때
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
		x = e.getX();
		y = e.getY();

		label.setText("x = " + x +", y = " + y);
	
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		x = e.getX();
		y = e.getY();

		label.setText("x = " + x +", y = " + y);
		
				
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		label.setText("x = " + e.getX() +", y = " +  e.getY());
//		label.setBounds(x, y, 200, 50);
	}
}