/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.layouts;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CardLayoutExample extends JPanel {

	CardLayoutExample() {
		super(new BorderLayout());

		final JButton button1 = new JButton("Button 1");
		final JButton button2 = new JButton("Button 2");
		final JButton button3 = new JButton("Button 3");
		final JButton button4 = new JButton("Button 4");
		final JButton button5 = new JButton("Button 5");
		final JToolBar toolBar  =new JToolBar();
		toolBar.add(button1);
		toolBar.add(button2);
		toolBar.add(button3);
		toolBar.add(button4);
		toolBar.add(button5);
		add(toolBar, BorderLayout.PAGE_START);


		final JPanel cardPanel = new JPanel(new CardLayout());
		cardPanel.add(new JLabel("A"), "card 1");
		cardPanel.add(new JLabel("B"), "card 2");
		add(cardPanel, BorderLayout.PAGE_END);

		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CardLayout layout = (CardLayout) cardPanel.getLayout();
				layout.show(cardPanel, "card 1");
			}
		});
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CardLayout layout = (CardLayout) cardPanel.getLayout();
				layout.show(cardPanel, "card 2");
			}
		});
	}

	public static void main(String... args) {

		// 프레임을 생성한다.
		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new CardLayoutExample());

		// 이벤트 디스패치 스레드에서 프레임을 표시한다.
		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});

	}
}
