VoteCounterPanel - winnerLabel = new JLabel""...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// *********************************************************************** // // // Panel for the GUI that tallies votes for two candidates, Joe and Sam. // *********************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class VoteCounterPanel extends JPanel { private int votesForJoe; private int votesForSam; private JLabel labelJoe; private JLabel labelSam; private JLabel winnerLabel; private JButton joe, sam; // ------------------------------------------------------- // Constructor: Sets up the GUI // ------------------------------------------------------- public VoteCounterPanel() { votesForJoe = 0; votesForSam = 0; joe = new JButton ("Vote for Joe!"); VoteButtonListener voteListener = new VoteButtonListener(); joe.addActionListener (voteListener); labelJoe = new JLabel ("Votes for Joe: " + votesForJoe); sam = new JButton ("Vote for Sam!"); sam.addActionListener (voteListener); labelSam = new JLabel ("Votes for Sam: " + votesForSam);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: winnerLabel = new JLabel ("---"); add (joe); add (labelJoe); add (sam); add (labelSam); add (winnerLabel); setPreferredSize (new Dimension(300, 80)); setBackground (Color.cyan); } // ******************************************************************* // Represents a listener for button push (action) events. // ******************************************************************* private class VoteButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { if (event.getSource() == joe) { votesForJoe++; labelJoe.setText ("Votes for Joe: " + votesForJoe); } else { votesForSam++; labelSam.setText ("Votes for Sam: " + votesForSam); } if (votesForJoe > votesForSam) winnerLabel.setText ("Joe is ahead in the vote count!!"); else if (votesForJoe < votesForSam) winnerLabel.setText ("Sam is ahead in the vote count!!"); else winnerLabel.setText ("Joe and Sam are tied!!"); } } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Page1 / 2

VoteCounterPanel - winnerLabel = new JLabel""...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online