Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: code doesn't compile/run. It's supposed to show the total votes for both candidates, regardless of which button the user clicks on.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default code doesn't compile/run. It's supposed to show the total votes for both candidates, regardless of which button the user clicks on.

    Actually: It needs to Increment the number of votes by one when you click on the candidate's name, and show the total of both candidates.
    import javax.swing.*;
    import java.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
     
    public class LabE extends JFrame implements PartyLabels {
     
    	public LabE() {
    		JPanel jp = new JPanel();
    		jp.setLayout( new GridLayout( 1, 2, 5, 5 ));
    		AmericanButton abMcCain = new AmericanButton( REPUBLICAN, "McCain" );
                    AmericanButton abObama = new AmericanButton( DEMOCRAT, "Obama" );                
     
    		jp.add( abMcCain );
    		jp.add( abObama );
     
    		add( jp );
     
                    theHandler handler = new theHandler();
                    abMcCain.addActionListener(handler);
                    abObama.addActionListener(handler);           
            } 
     
     
            private class theHandler implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
                    int a;
                    a = 0;
                    int b; 
                    b = 0;
     
                 if(event.getSource()== abMcCain)               
                    b++;
                    System.out.println("McCain: " + b + 
                    event.getActionCommand());
     
                 else if(event.getSource()== abObama)
                    a++;
                    System.out.println("Obama: " + a + event.getActionCommand());
            }      
     
            }
     
     
     
    	public static void main(String[] args) {
    		LabE frame = new LabE();
    		frame.setTitle( "Candidates" );
    		frame.setLocationRelativeTo( null );
    		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		frame.setSize( 400, 200 );
    		frame.setVisible( true );
     
     
            }
     
    }

    class AmericanButton extends JButton implements PartyLabels {
     
    	int party;
    	String text;
     
    	public AmericanButton( int party, String text ) {
    		this.party = party;
    		this.text = text;
    	}
     
    	protected void paintComponent( Graphics g ) {
    		super.paintComponent( g );
    //		Color c = new Color( Color.white );
    		if ( party == REPUBLICAN ) {
    			g.setColor( Color.red );
    		}
    		else if ( party == DEMOCRAT ) {
    			g.setColor( Color.blue );
    		}
     
    		g.fillRect( 0, 0, getWidth(), getHeight() );
    		g.setColor( Color.white );
    		g.fillRect( 30, 30, getWidth()-60, getHeight()-60 );
     
    		Font f = new Font( "SansSerif", Font.BOLD, 20 );
     
    		if ( party == REPUBLICAN ) {
    			g.setColor( Color.red );
    		}
    		else if ( party == DEMOCRAT ) {
    			g.setColor( Color.blue );
    		}
     
    		g.setFont( f );
     
    	    // Get font metrics for the current font
    	    FontMetrics fm = g.getFontMetrics();
     
    	    // Find the center location to display
    	    int stringWidth = fm.stringWidth(text);
    	    int stringAscent = fm.getAscent();
     
    	    // Get the position of the leftmost character in the baseline
    	    int xCoordinate = getWidth() / 2 - stringWidth / 2;
    	    int yCoordinate = getHeight() / 2 + stringAscent / 2;
     
    	    g.drawString(text, xCoordinate, yCoordinate);
    	}
    }
    // this interface holds the labels for the parties.
     
    interface PartyLabels {
     
    	public static final int DEMOCRAT = 0;
    	public static final int REPUBLICAN = 1;
    }



    thanks everyone!
    Last edited by helloworld922; November 2nd, 2012 at 08:54 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: code doesn't compile/run. It's supposed to show the total votes for both candidates, regardless of which button the user clicks on.

    If it doesn't compile, it would be extremely helpful if you posted your compiler errors and indicate which line in your code causes these errors. Also, look at the forum FAQ to learn how to use code tags so we can more easily read your code.

Similar Threads

  1. counting mouse button clicks
    By bluewhale in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 25th, 2014, 08:17 AM
  2. [SOLVED] Why doesn't my Applet show up in my browser?
    By SendBorg in forum Java Applets
    Replies: 3
    Last Post: January 29th, 2012, 08:30 AM
  3. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  4. bingo ticket loop problem (doesn't compile like it should...)
    By mv740 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2011, 04:21 PM
  5. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 12:46 PM