Lab 11: Pre-lab Assignment

  1. The following is the same as the PushCounter example on pages 186 and 187 of the textbook except it assumes that pushing the button is a vote for Blue (and all identifiers have been changed to reflect this). Also rather than instantiating the listener object as a parameter in the invocation of addActionListener, a listener object named voteListener has been instantiated and it has been passed as the parameter.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class VoteCounterPanel extends JPanel
    {
        private int votesForBlue;
        private JLabel labelBlue;
        private JButton blue;
        
    
    
        // ------------------------------------------------------------
        //  Sets up the GUI
        // ------------------------------------------------------------
        public VoteCounterPanel ()
        {
    	votesForBlue = 0;
    	
    	blue = new JButton ("Vote for Blue!");
    	VoteButtonListener voteListener = new VoteButtonListener();
    	blue.addActionListener (voteListener);
    	
    	labelBlue = new JLabel ("Votes for Blue: " + votesForBlue);
    
    
    
    
    
    
    	
    	add (blue);
    	add (labelBlue);
    
    
    
    
    
    
    	setBackground (Color.white);
        }
    
    
        // *******************************************************************
        //  Represents a listener for button push actions
        // *******************************************************************
        private class VoteButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            {
                votesForBlue++;
                labelBlue.setText ("Votes for Blue: " + votesForBlue); 
            }
        }
    }
    
    Suppose we wanted to add a second button to the panel, one for candidate Red. We would need three new instance variables - a vote counter for Red, a button, and a label. Add code to the above to:

    1. declare the 3 new instance variables
    2. initialize/instantiate them in the constructor
    3. add them to the panel
    4. add the voteListener object to the new button so it will now listen for both buttons being clicked (NOTE: don't instantiate a new listener object - use the one already there)

  2. If nothing else changes in the example above, what will happen when the user presses the button for Red?

     

     

  3. The actionPerformed method in the VoteButtonListener class needs to be changed so it updates the count for Red when the button for Red is pressed. Hence it needs to know which button was pressed. This information can be determined by using the getSource method and the event parameter - event.getSource() is a reference to the component that generated the event. An example of getting the source of an event is on page 259-260. Fill in the if statement to update the correct vote counter and label in the shell for actionPerformed below.
      public void actionPerformed (ActionEvent event)
      {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      }
    
  4. The Fahrenheit to Celsius conversion example on pages 190 - 192 uses a component of type JTextField, a text field (or text box) for the user to type a temperature into.
    1. What does the 5 in the instantiation of the JTextField (page 191) represent?

       

       

    2. In the actionPerformed method (page 192) the getText method is used to get the contents of the fahrenheit text field. Then the result is sent to the parseInt method of the Integer class. What does the parseInt method do and why is it needed in this example?

       

       

       

  5. What is the relationship between an event and a listener?

     

     

     

  6. What is the difference between a frame and a panel?