Skip to content

Instantly share code, notes, and snippets.

@seanh
Created May 17, 2009 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanh/113083 to your computer and use it in GitHub Desktop.
Save seanh/113083 to your computer and use it in GitHub Desktop.
A simple demonstration of Java Swing's CardLayout.
package scratch;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple demonstration of Swing's CardLayout.
*
* @author seanh
*/
class CardLayoutDemo {
private JFrame frame = new JFrame("Swing Refresh Bug?");
private Container contentPane = frame.getContentPane();
private JPanel cardPanel = new JPanel();
private CardLayout cardLayout = new CardLayout();
private Component currentComponent;
private JButton next;
CardLayoutDemo() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardPanel.setLayout(cardLayout);
cardPanel.add(new JLabel("One"),"One");
cardPanel.add(new JLabel("Two"),"Two");
cardPanel.add(new JLabel("Three"),"Three");
next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.next(cardPanel);
}
});
contentPane.setLayout(new BorderLayout());
contentPane.add(cardPanel,BorderLayout.CENTER);
contentPane.add(next,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
CardLayoutDemo demo = new CardLayoutDemo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment