package CHAPTER11;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class C11_05_AnonymousClassEventExample extends JFrame {
	public C11_05_AnonymousClassEventExample() {

		setTitle("숫자 카운트 프로그램");
		setSize(300, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);

		JLabel label = new JLabel("Count: 0");

		label.setBounds(110, 50, 100, 30);
		add(label);

		JButton button = new JButton("Click Me");
		button.setBounds(100, 100, 100, 30);
		add(button);

		// 익명 클래스 사용하여 이벤트 처리

		button.addActionListener(new ActionListener() {
			private int counter = 0; // 클릭 횟수 저장 변수

			@Override
			public void actionPerformed(ActionEvent e) {
				counter = (counter % 100) + 1; // 1~100 순환
				label.setText("Count: " + counter);
			}
		});
		setVisible(true);
	}

	public static void main(String[] args) {
		C11_05_AnonymousClassEventExample ac = new C11_05_AnonymousClassEventExample();
	}
}
//import javax.swing.JButton;
//import javax.swing.JFrame;
//import javax.swing.JLabel;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//
//public class C10_05_AnonymousClassEventExample extends JFrame {
//    public C10_05_AnonymousClassEventExample() {
//        // JFrame 기본 설정
//        setTitle("Counter App");
//        setSize(300, 200);
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setLayout(null);
//
//        // 레이블 생성
//        JLabel label = new JLabel("Count: 0");
//        label.setBounds(50, 30, 200, 30);
//        add(label);
//
//        // 버튼 생성
//        JButton button = new JButton("Click Me");
//        button.setBounds(100, 80, 100, 30);
//        add(button);
//
//        // 무명 클래스 사용하여 이벤트 처리
//        button.addActionListener(new ActionListener() {
//            private int counter = 0; // 클릭 횟수 저장 변수
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                counter = (counter % 100) + 1; // 1~100 순환
//                label.setText("Count: " + counter);
//            }
//        });
//        setVisible(true);
//    }
//
//    public static void main(String[] args) {        
//    	C10_05_AnonymousClassEventExample app = new C10_05_AnonymousClassEventExample();
//    }
//}
