Code /* TestCardLayout2.java CIS 260 2/22/2006 David Klick Demonstration of CardLayout using direct access buttons (as well as next/previous buttons). */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestCardLayout2 extends JFrame { JButton btnNext, btnPrev; JButton btnC1, btnC2, btnC3; JPanel pnlCard[], pnlCards, pnlControl, pnlTab; JButton btn[]; int currentCard = 0; public static void main(String[] args) { TestCardLayout2 app = new TestCardLayout2(); app.setDefaultCloseOperation(EXIT_ON_CLOSE); app.init(); } public void init() { int i; pnlCard = new JPanel[3]; btn = new JButton[12]; pnlCards = new JPanel(); pnlCards.setLayout(new CardLayout()); for (i=0; i 1) { currentCard--; ((CardLayout) pnlCards.getLayout()).previous(pnlCards); } } if (currentCard <= 1) btnPrev.setEnabled(false); else btnPrev.setEnabled(true); if (currentCard >= pnlCard.length) btnNext.setEnabled(false); else btnNext.setEnabled(true); } } class TabListener implements ActionListener { public void actionPerformed(ActionEvent e) { String cardName = ((JButton) e.getSource()).getActionCommand(); ((CardLayout) pnlCards.getLayout()).show(pnlCards, cardName); currentCard = Integer.parseInt(cardName.substring(cardName.length()-1)); if (currentCard <= 1) btnPrev.setEnabled(false); else btnPrev.setEnabled(true); if (currentCard >= pnlCard.length) btnNext.setEnabled(false); else btnNext.setEnabled(true); } } }