import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Subclass JFrame so you can display a window
public class CardLayoutAnimation4 extends JFrame implements ActionListener, Runnable {

	private CardLayout  cardLayoutManager;
	private JPanel		panel;
                     
	// Add a constructor for our frame.
    public CardLayoutAnimation4 (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){}
	}

	public void run() {
		for (int i=0; i<4; i++) {
			cardLayoutManager.next(panel);
			// Now redraw the window by updating its appearance
            panel.update(panel.getGraphics()); // a standard JComponent method
			delay();
		}
	}

	// implements the listener behavior, e.g. go to next button in the stack
	public void actionPerformed(ActionEvent theEvent) {
		new Thread(this).start();
	}
                      
	// Create main method to execute the application
	public static void main(String args[]) {
		new CardLayoutAnimation4("Thread Test 2").setVisible(true); 
	}         
}
