Swing is single threaded. Youre blocking the EDT
with the call to Thread.sleep
preventing the first card from being painted. Swing Timers were designed to interact with Swing components. Use one instead to invoke CardLayout#show
in the timer's ActionListener
.
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cards.show(panel,"second");
}
});
timer.setRepeats(false);
timer.start();
Read: Concurrency in Swing