목차
1. implements ActionListener를 사용 하는 방법
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 ColoerChangeFrame extends JFrame implements ActionListener {
// 이벤트 리스너에 대한 개념을 이해하자.
private JButton button1;
public ColoerChangeFrame() {
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);
}
// OS에게 이벤트 등록을 요청함
// OS에게 마우스나 키보드 입력을 지켜보게하고 동작하면 알려달라고함
private void addEventListener() {
// button1이 눌러지는지 계속 이벤트를 지켜 보고 있어.
// 이벤트 등록
button1.addActionListener(this);
}
// 코드 테스트
public static void main(String[] args) {
new ColoerChangeFrame();
} // end of main
// 약속 되어 있던 추상메서드를 오버라이드
// 이벤트가 발생 되면 이 메서드를 수행하라고 약속 되어 있음
// 단, 어떤 컴포넌트가 이벤트에 할당 되었는지 등록을 먼저 해주어야 한다.
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("actionPerformed 메서드 호출");
System.out.println(e.toString());
}
}
*콜백 메서드
actionPerformed는 객체에 접근해서 호출한적이 없는데 호출되었음
콜백 메서드이기 때문
2개 이상의 버튼 구현
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 ColoerChangeFrame2 extends JFrame implements ActionListener {
private JPanel panel;
private JButton button1;
private JButton button2;
public ColoerChangeFrame2() {
initData();
setInitLayout();
addEventListener();
}
private void initData() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout()); // BorderLayout
panel = new JPanel();
button1 = new JButton("click1");
button2 = new JButton("click2");
}
private void setInitLayout() {
add(button1, BorderLayout.NORTH);
add(button2, BorderLayout.SOUTH);
add(panel, BorderLayout.CENTER);
panel.setBackground(Color.YELLOW);
setVisible(true);
}
// 이 메서드의 책임은 이벤트 리스너만 등록
private void addEventListener() {
button1.addActionListener(this);
button2.addActionListener(this);
}
// 코드 테스트
public static void main(String[] args) {
new ColoerChangeFrame2();
} // end of main
// 이벤트가 일어나면 호출되는 메서드
@Override
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == button1) {
System.out.println("button1 객체가 눌러졌다");
panel.setBackground(Color.BLACK);
}
if ((JButton) e.getSource() == button2) {
System.out.println("button2 객체가 눌러졌다");
panel.setBackground(Color.WHITE);
}
}
}
도전 문제
화면을 위 아래 두 구역으로 나누어 아래 영역에는 버튼 3개를 배치한다.
(위는 BorderLayout의 CENTER, 아래는 SOUTH)
각각의 버튼을 눌렀을 때 위 구역의 색이 바뀌도록 한다.
package ch05;
import java.awt.BorderLayout;
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;
import javax.swing.JPanel;
public class ColorChangeFrame3 extends JFrame implements ActionListener {
private JPanel panel1;
private JPanel panel2;
private JButton button1;
private JButton button2;
private JButton button3;
public ColorChangeFrame3() {
initData();
setInitLayout();
addEventListener();
}
private void initData() {
setSize (600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
panel1 = new JPanel();
panel2 = new JPanel();
button1 = new JButton("RED");
button2 = new JButton("GREEN");
button3 = new JButton("BULE");
}
private void setInitLayout() {
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
panel1.setBackground(Color.WHITE);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 20));
panel2.add(button1);
button1.setBackground(Color.RED);
panel2.add(button2);
button2.setBackground(Color.GREEN);
panel2.add(button3);
button3.setBackground(Color.BLUE);
panel2.setBackground(Color.BLACK);
setVisible(true);
}
private void addEventListener() {
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton)e.getSource();
if (clickedButton == button1) {
panel1.setBackground(Color.RED);
System.out.println("RED버튼이 눌러짐");
} else if (clickedButton == button2) {
panel1.setBackground(Color.GREEN);
System.out.println("GREEN버튼이 눌러짐");
} else if (clickedButton == button3) {
panel1.setBackground(Color.BLUE);
System.out.println("BLUE버튼이 눌러짐");
}
}
public static void main(String[] args) {
new ColorChangeFrame3();
}
}
![]() |
![]() |
![]() |
![]() |
2. 익명 클래스를 활용해서 인터페이스 구현
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 ColoerChangeFrame2 extends JFrame {
private JPanel panel;
private JButton button1;
private JButton button2;
public ColoerChangeFrame2() {
initData();
setInitLayout();
addEventListener();
}
private void initData() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout()); // BorderLayout
panel = new JPanel();
button1 = new JButton("click1");
}
private void setInitLayout() {
add(button1, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
panel.setBackground(Color.YELLOW);
setVisible(true);
}
private void addEventListener() {
// 이벤트 리스너를 구현하는 두번째 방법
// 익명 클래스 활용
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button1이 눌러졌어요");
panel.setBackground(Color.BLACK);
}
});
}
// 코드 테스트
public static void main(String[] args) {
new ColoerChangeFrame2();
} // end of main
}
'Java > Swing 프로젝트' 카테고리의 다른 글
Swing 로또 게임 만들기 - 7 (0) | 2024.04.30 |
---|---|
Swing Key Listener - 6 (0) | 2024.04.30 |
Swing image 위에 image - 4 (0) | 2024.04.29 |
Swing image 출력 - 3 (0) | 2024.04.29 |
Swing 기초연습 - 2 (0) | 2024.04.26 |