Christmas Pikachu 마우스 리스너 (MouseListener) - 2 (익명, 구현 클래스)
개발일지/자바

마우스 리스너 (MouseListener) - 2 (익명, 구현 클래스)

ZI_CO 2022. 9. 17.

package blogpractice;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseEventListener2 extends JFrame {

	JLabel label;

	final int LABEL_WIDTH = 100; // 라벨의 가로 크기
	final int LABEL_HEIGHT = 100; // 라벨의 세로 크기
	int xPosition = 100;
	int yPosition = 100;

	public MouseEventListener2() {
		initData();
		setInitLayout();
		addEventListener();
	}

	private void initData() {
		setTitle("마우스리스너 구현클래스 사용법");
		setSize(500, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		label = new JLabel("여기 여기");

	}

	private void setInitLayout() {
		setVisible(true);
		setLayout(null);

		label.setBounds(xPosition, yPosition, LABEL_WIDTH, LABEL_HEIGHT);
		add(label);
	}

	private void addEventListener() {
		this.addMouseListener(listener);

	}

	MouseListener listener = new MouseListener() {

		@Override
		public void mouseClicked(MouseEvent e) {
			System.out.println("클릭");

			label.setLocation(e.getX(), getY() - (label.getBounds().height));

		}

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

		}

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

		}

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

		}

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

		}

	};

}

 

 

 

 

댓글