import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Subclass JFrame so you can display a window
public class CardLayoutAnimation2 extends JFrame implements ActionListener {

	private CardLayout  cardLayoutManager;
	private JPanel		panel;
                     
	// Add a constructor for our frame.
    public CardLayoutAnimation2 (String title) { 
		super(title);

		setLayout(new FlowLayout());
		
		// Set the layoutManager
		cardLayoutManager = new CardLayout(0,0);
		panel = new JPanel();
		panel.setLayout(cardLayoutManager);

		// Add 4 buttons, each with a different picture as the icon
		for (int i=0; i<4; i++) {
			JButton aButton = new JButton(new ImageIcon("Stick" + i + ".gif"));
			panel.add(String.valueOf(i), aButton);
			aButton.addActionListener(this);
		} 
		
		JButton b = new JButton("Press Me");
		add(panel);
		add(b);
		
		b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent theEvent) {
              	System.out.println("Hello");
        }});
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(80,160);		
	}

	// Make a brief delay
	private void delay() {
		try {
			Thread.sleep(1000);
		}
		catch (InterruptedException e){}
	}

	// implements the listener behavior, e.g. go to next button in the stack
	public void actionPerformed(ActionEvent theEvent) {
		for (int i=0; i<4; i++) {
			cardLayoutManager.next(panel);
			
			// Now redraw the window by updating its appearance
            update(getGraphics()); // a standard JComponent method
            
			delay();
		}
	}
                      
           
	// Create main method to execute the application
	public static void main(String args[]) {
		new CardLayoutAnimation2("Delay Test").setVisible(true); 
	}         
}
