// CIS 226 Chapter 7 Arrays Example // DeckOfCardsApplet.java - Applet for DeckOfCards // Jerry Zhou - 07/22/2006 import java.awt.*; // Container, FlowLayout import java.awt.event.*; // ActionEvent, ActionListener import javax.swing.*; // JApplet, JButton, JTextField public class DeckOfCardsApplet extends JApplet implements ActionListener { // my deck of cards DeckOfCards myDeckOfCards; // graphical user interface components JButton dealButton, shuffleButton; JTextField cardField; // set up GUI components public void init() { // obtain content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // create buttons and text fields dealButton = new JButton( "Deal card" ); dealButton.addActionListener(this); container.add( dealButton ); shuffleButton = new JButton( "Shuffle cards" ); shuffleButton.addActionListener(this); container.add( shuffleButton ); cardField = new JTextField(20); cardField.setEditable(false); container.add( cardField ); // create mydeckOfCards myDeckOfCards = new DeckOfCards(); } // end method init // process button click public void actionPerformed( ActionEvent actionEvent ) { if( actionEvent.getSource() == dealButton ) { // Deal card Card myCard = myDeckOfCards.dealCard(); cardField.setText( myCard == null? "NO MORE CARD": myCard.toString() ); } else if( actionEvent.getSource() == shuffleButton ) { // Shuffle cards myDeckOfCards.shuffle(); cardField.setText( "DECK IS SHUFFLED" ); } } // end method actionPerformed } // end class DeckOfCardsApplet