Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

 

    Java Exercises

Event Handling

 

This exercise should be carried out after reading the Event Handling Tutorial

You should do both of the exercises listed below.

Exercise 1: Handling the Button Action Events

Exercise 2: Handling the Menu Action Events


Exercise 1: Handling the Button Action Events

Examine the picture of the applet below.  Hopefully you have created this user interface in previous exercises.  If this is not the case then go and do that first.  

Add event handling code so that when a button is clicked the letter displayed on the button caption is displayed in 'Guess The Word' text field.  

(You don't need to make every button work yet, just make a few of them respond to action events.)

 

 

Hint

  1. Declare your class an ActionListener

  2. Register an ActionListener on each button using the addActionListener method

  3. Use the setActionCommand method to enable easy identification of an event happening to a button.

  4. Write event handling code in the actionPerformed method so that the text field displays the appropriate letter when a button is clicked.

 

Here is  a little bit of code just to get you started.  

You must create the code for the graphical user interface first.  I have just shown you some code dealing with action events for two buttons only.

 

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import javax.swing.event.*;

public class Hangman extends JApplet implements ActionListener {

  // create the text field here so I can refer to it in any method

  JTextField wordField = new JTextField(24);


  public void init() {
     // it is up to you to write code here that creates the

     //graphical user interface, the buttons, menu etc.

     // create some buttons
    JButton buttonA = new JButton ("A");

    JButton buttonB = new JButton ("B");

     // register an event listener on each button
    buttonA.addActionListener(this)

    buttonB.addActionListener(this)

     // set an action command to identify each button
    buttonA.setActionCommand("buttonA")

    buttonB.setActionCommand("buttonB")

  }

     // method for handling Action Events
  public void actionPerformed (ActionEvent e) {

   if e.getActionCommand == "buttonA" {

       wordField.setText("A");

   }

   if e.getActionCommand == "buttonB" {

       wordField.setText("B");

   }
 }
}

 

 


Exercise 2: Handling The Menu Action Events

The expanded applet menu looks like this:-

Hopefully you have already created a menu like this in the Containers exercise.  If not then go and do that now.

  1. Add event handling code so that when the user clicks the 'Start' menu item, the 'Guess The Word' text field displays 6 underscore characters.  I.e.

 

Hint

  1. Declare your class an ActionListener if you haven't done this from the previous exercise.

  2. Register an ActionListener on your Start menu item using the addActionListener method

  3. Use the setActionCommand method to enable easy identification of an event happening to this menu item.

  4. Write event handling code in the actionPerformed method so that the text field displays the underscore characters when the Start menu item is clicked.

 

  1. Add event handling code so that when the user clicks the 'Answer' menu item, the 'Guess The Word' text field displays the word 'Memory'.  I.e.

 

Hint

  1. Register an ActionListener on your Answer menu item using the addActionListener method if you haven't done this from the previous exercise.

  2. Use the setActionCommand method to enable easy identification of an event happening to this menu item.

  3. Write event handling code in the actionPerformed method so that the text field displays the word Memory  when the Answer menu item is clicked.

 

Have fun!

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002