Java/Java Swing

Swing - 5 (이벤트 리스너)

도준영 2024. 7. 3. 17:31
package ch05;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
// 이벤트 리스너를 사용하는 방법
// 1. implements ActionListener 사용 하는 방법 
// ActionListener --> 운영 체제가 제어하는 이벤트를 등록할 수 있다. 
public class ColorChangeFrame extends JFrame implements ActionListener {

	// 이벤트 리스너에 대한 개념을 이해하자. 
	private JButton button1;
	
	public ColorChangeFrame() {
		initData();
		setInitLayout();
		addEventListener();
	}
	
	private void initData() {
		setSize(500, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		button1 = new JButton("button1");
	}
	
	private void setInitLayout() {
		setLayout(new FlowLayout());
		add(button1);
		setVisible(true);
	}
	
	private void addEventListener() {
		// button1이 눌러지는지 계속 이벤트를 지켜 보고 있어 
		// 이벤트 등록 
		button1.addActionListener(this);
	}
	
	// 코드 테스트 
	public static void main(String[] args) {
		new ColorChangeFrame();
	} // end of main 

	// 약속 되어 있던 추상메서드를 오버라이드 했다. 
	// 이벤트가 발생 되면 이 메서드를 수행해 약속 되어 있음 
	// 단, 어떤 컴포넌트가 이벤트가 할당 되었는지 등록을 먼저 해주어야 한다. 
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("actionPerformed 메서드 호출()");
		System.out.println(e.toString());
	}
	
} // end of class

 

 

package ch05;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ColorChangeFrame2 extends JFrame implements ActionListener  {

	private JPanel panel;
	private JButton button1; 
	private JButton button2; 
	
	public ColorChangeFrame2() {
		initData();
		setInitLayout();
		addEventListener();
	}
	
	private void initData() {
		setSize(500, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout()); // BorderLayout 
		panel = new JPanel();
		panel.setBackground(Color.yellow);
		
		button1 = new JButton("click1");
		button2 = new JButton("click2");
		
	}
	private void setInitLayout() {
		add(button1, BorderLayout.NORTH);
		add(button2, BorderLayout.SOUTH);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
	}
	// 이 메서드에 책임은 이벤트 리스너만 등록 
	private void addEventListener() {
		button1.addActionListener(this);
		button2.addActionListener(this);
	}
	
	// 오버라이드 : 이벤트가 일어나면 호출 되어지는 메서드 
	@Override
	public void actionPerformed(ActionEvent e) {
		Object object = e.getSource();
		// 주소값으로 비교도 가능 
		// 문자열 값으로 비교 가능 
		JButton selectedButton = (JButton)e.getSource();
		if(selectedButton == this.button1) {
			System.out.println("button1 객체가 눌러졌다라고 판명 가능 ");
			panel.setBackground(Color.yellow);
		} else {
			System.out.println("button2 객체가 눌러졌다라고 판명 가능 ");
			panel.setBackground(Color.black);
		}
	}
	
	// 코드 테스트 
	public static void main(String[] args) {
		new ColorChangeFrame2();
	} // end of main 	
}

'Java > Java Swing' 카테고리의 다른 글

Swing - 7 로또 게임 만들기  (0) 2024.07.15
Swing - 6 (Key Listener)  (0) 2024.07.15
Swing - 4 (이미지 겹치는 방법)  (0) 2024.07.03
Swing - 3 (이미지 올리기)  (0) 2024.05.31
Swing - 2  (0) 2024.05.31