TestCardLayout2.java
Select all
/* 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<pnlCard.length; i++) { pnlCard[i] = new JPanel(); pnlCard[i].setLayout(new GridLayout(2, 2, 3, 3)); pnlCards.add("Card"+(i+1), pnlCard[i]); } for (i=0; i<btn.length; i++) { btn[i] = new JButton("Button #" + (i+1)); pnlCard[i/4].add(btn[i]); } ButtonListener lstnr = new ButtonListener(); btnNext = new JButton("Next"); btnNext.addActionListener(lstnr); btnPrev = new JButton("Previous"); btnPrev.addActionListener(lstnr); pnlControl = new JPanel(); pnlControl.setLayout(new GridLayout(1, 2, 3, 3)); pnlControl.add(btnPrev); pnlControl.add(btnNext); currentCard = 1; btnPrev.setEnabled(false); pnlTab = new JPanel(); pnlTab.setLayout(new GridLayout(1, 3, 0, 3)); btnC1 = new JButton("Card 1"); btnC2 = new JButton("Card 2"); btnC3 = new JButton("Card 3"); btnC1.setActionCommand("Card1"); btnC2.setActionCommand("Card2"); btnC3.setActionCommand("Card3"); TabListener tlstnr = new TabListener(); btnC1.addActionListener(tlstnr); btnC2.addActionListener(tlstnr); btnC3.addActionListener(tlstnr); pnlTab.add(btnC1); pnlTab.add(btnC2); pnlTab.add(btnC3); Container c = getContentPane(); c.setLayout(new BorderLayout(10, 10)); c.add(pnlControl, BorderLayout.NORTH); c.add(pnlCards, BorderLayout.CENTER); c.add(pnlTab, BorderLayout.SOUTH); pack(); setVisible(true); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == btnNext) { if (currentCard < pnlCard.length) { currentCard++; ((CardLayout) pnlCards.getLayout()).next(pnlCards); } } else if (e.getSource() == btnPrev) { if (currentCard > 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); } } }