LAB 41 – GUI (Buttons, events, and listeners)

Lab Exercises

Topics

n      To introduce the concepts needed to create an interactive graphical user interfaces.

n      To write an interactive GUI using buttons, events, and listeners

n      To use the BlueJ IDE to write and test the application

 

Using BlueJ to start a new application

1)       Starting BlueJ - double-click icon.

2)       Creating a new project - select New Project from the Project menu and save the project as GUI2.

3)       Click on New Class and type VoteCounter as the class name and select Class as the class type

4)       Right-click on the VoteCounter class and select Open Editor

5)       Replace the code generated by BlueJ with the following; update the comments.

//*********************************************************
// VoteCounter.java
//
// Demonstrates a graphical user interface and event listeners to
// tally votes for two candidates, Richardson and Paul.
//*********************************************************
import javax.swing.JFrame;
 
public class VoteCounter
{
    //----------------------------------------------
    // Creates the main program frame.
    //----------------------------------------------
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Vote Counter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        frame.getContentPane().add(new VoteCounterPanel());      
        frame.pack();
        frame.setVisible(true);
    }
}

6)       Click on New Class and type VoteCounterPanel as the class name and select Class as the class type

7)       Right-click on the VoteCounterPanel class and select Open Editor

8)       Replace the code generated by BlueJ with the following; update the comments

//*********************************************************
// VoteCounterPanel.java  
//
// Demonstrates a graphical user interface and event listeners to
// tally votes for two candidates, Richardson and Paul.
//*********************************************************
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class VoteCounterPanel extends JPanel
{
    private int votesForRichardson;
    private JButton richardson;
    private JLabel labelRichardson;
 
    //----------------------------------------------
    // Constructor: Sets up the GUI.
    //----------------------------------------------
    public VoteCounterPanel()
    {
        votesForRichardson = 0;
 
        richardson = new JButton("Vote for Richardson");
        richardson.addActionListener(new RichardsonButtonListener());
 
        labelRichardson = new JLabel("Votes for Richardson: " + votesForRichardson);
 
        add(richardson);
        add(labelRichardson);
 
        setPreferredSize(new Dimension(300, 40));
        setBackground(Color.cyan);
    }
 
    //***************************************************
    // Represents a listener for button push (action) events
    //***************************************************
    private class RichardsonButtonListener implements ActionListener
    {
        //----------------------------------------------
        // Updates the counter and label when Vote for Richardson 
        // button is pushed
        //----------------------------------------------
        public void actionPerformed(ActionEvent event)
        {
            votesForRichardson++;
            labelRichardson.setText("Votes for Richardson: " + votesForRichardson);
        }
    }
}

9)       To run the program, right-click on the VoteCounter class and select and select void main (String[] arguments)

a)       This should pop-up a terminal window with results

10)  Modify the program so that there are two candidates to vote for—Richardson and Paul. To do this you need to do the following:

a)      Add variables for Paul—a vote counter, a button, and a label.

b)      Add a new inner class named PaulButtonListener to listen for clicks on the button for Paul. Instantiate an instance of the class when adding the ActionListener to the button for Paul.

c)      Add the button and label for Paul to the panel.

11)    Compile and run the modified program.

QUESTIONS: Submit VoteCounter.java and VoteCounterPanel.java to the DropBox in WebCT.