Java/Java.Swing

Swing - 5 (이벤트 리스너)

H_u 2024. 4. 29. 16:51
728x90
반응형
SMALL

package ch05;

 

import java.awt.FlowLayout;

 

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();

}

 

// 약속 되어 있던 추상메서드를 오버라이드 했다.

// 이벤트가 발생 되면 이 메서드를 수행해 약속 되어 있음

// 단, 어떤 컴포넌트가 이벤트가 할당 되었는지 등록을 먼저 해주어야 한다.

 

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("actionPerformed 메서스 호출()");

System.out.println(e.toString());

}

}

 

package ch05;

 

import java.awt.BorderLayout;

 

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());

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);

// // 이벤트 리스너를 구현하는 두번째 방법

// button1.addActionListener(new ActionListener() {

//

// @Override

// public void actionPerformed(ActionEvent e) {

// System.out.println("button1 이 눌러 졌어요.");

// }

// });

}

 

//이벤트가 일어나면 호출 되어지는 메서드

@Override

public void actionPerformed(ActionEvent e) {

 

if(e.getSource().equals(button1)){

System.out.println(button1);

panel.setBackground(Color.BLACK);

}if(e.getSource().equals(button2)) {

System.out.println(button2);

panel.setBackground(Color.BLUE);

}

}

 

public static void main(String[] args) {

new ColorChangeFrame2();

}

 

}

 

 

실습문제!!

package ch05;

 

import java.awt.BorderLayout;

 

public class ColorClick extends JFrame implements ActionListener {

private JPanel panel;

private JPanel buttonPanel;

private JButton button1;

private JButton button2;

private JButton button3;

private JLabel img;

 

public ColorClick() {

initData();

setInitLayout();

addEventListener();

}

 

private void initData() {

setSize(500, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

panel = new JPanel();

panel.setBackground(Color.yellow);

buttonPanel = new JPanel();

button1 = new JButton("click1");

button2 = new JButton("click2");

button3 = new JButton("click3");

}

 

private void setInitLayout() {

buttonPanel.add(button1);

buttonPanel.add(button2);

buttonPanel.add(button3);

add(buttonPanel, BorderLayout.SOUTH);

add(panel, BorderLayout.CENTER);

setVisible(true);

}

 

private void addEventListener() {

button1.addActionListener(this);

 

button2.addActionListener(this);

 

button3.addActionListener(this);

}

 

@Override

public void actionPerformed(ActionEvent e) {

 

if (e.getSource().equals(button1)) {

System.out.println(button1);

panel.setBackground(Color.BLACK);

}

if (e.getSource().equals(button2)) {

System.out.println(button2);

panel.setBackground(Color.BLUE);

}

if (e.getSource().equals(button3)) {

System.out.println(button3);

panel.setBackground(Color.GREEN);

}

 

}

 

public static void main(String[] args) {

new ColorClick();

}

 

}

기본화면
1번 버튼
2번 버튼
3번 버튼

728x90
반응형
SMALL

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

Swing 로또 게임 만들기 - 7  (0) 2024.04.30
Swing - 6 (Key Listener)  (0) 2024.04.30
Swing - 4 (이미지 겹치는 방법)  (0) 2024.04.29
Swing - 3 (이미지 올리기)  (0) 2024.04.29
Swing2  (0) 2024.04.26