This document contains links towards the solutions of the exercises, please make genuine efforts at answering the questions before looking at the solutions!
The object that handles the event generated by the click of a button needs to implement the interface ActionListener.
The object that handles the event generated when a window is closed must implement the interface WindowListener;
The source has a method addActionListener who needs as a parameter, an object that implements the interface ActionListener.
Since the handler implements the interface ActionListener, the source knows that the handler has a method actionPerformed( ActionEvent event ).
Here is a simple application that has a display area and two buttons. When the left button is clicked the dot moves to the left; similarly for the right button. Adapted from Decker & Hirshfield (2000) Programming.java
Create a subclass of Canvas, called DisplayArea. “A Canvas component represents a blank rectangular area of the screen onto which the application can draw (…)”. Later, we’ll add the necessary implementation details so that a red dot is displayed in the canvas and moved to the left or to the right whenever the user clicks on the left or the right button. Add a constructor that sets the size of the DisplayArea to 200 by 200 pixels.
Create a subclass of Frame, called GUI. This will be the main window of the application.
Create a constructor that will add all the necessary graphical components to create the layout.
There are two ways to start this application. In a shell (dos/command window) type:
Or create a new instance of GUI in the interactions window of DrJava:
Your application should look something like this.
Clicking the closing button of the title bar will have no effect, this is because we have not associated any action with the button yet. If you are using DrJava, then you can make the window invisible by typing the following statement.
Clicking on the closing button of the title bar has no effect. Our main window is an object of the class GUI (a Frame). A frame has a method addWindowListener. It allows to register objects that will asked to perform the required actions when the user generates such event. The method addWindowListener requires an object that implements the interface WindowListener. The interface lists the following 7 methods.
The relevant method here is windowClosing, when the user clicks on the window closing button, your application should call System.exit( 0 ). It is not possible to implement a single method, you are forced to provide an implementation for all the methods listed in the interface. You could provide empty definitions for all the other methods but in fact this is such a commonly occurring scenario that the makers of Java created the following abstract class, called an adapter:
It provides an empty definition for all the methods of the interface. Whenever you want to create a window listener that only implements a subset of the methods of the interface, you simply have to create a subclass of WindowAdapter and override the relevant methods.
Compile and test the application. You should now be able to exit the application by clicking the closing button.
Similarly, we need to tell the application how to handle the user events. For this, we need to modify the constructor of the class GUI and add an ActionListener to each button. Which class will be responsible for this? When a button is pressed, the application needs to re-draw a red circle onto the canvas, because of this let’s modify the canvas so that it becomes the ActionListener for the two buttons.
You can now compile and test the application. Each time one of the two buttons is pressed, a message will be printed into the console.
Each time AWT needs to paint a Canvas it calls its method paint( Graphics g ).
You can now compile and test the application. You should see a red circle at the centre of the display area.
All that is left to do is making the dot move, to the left or to the right, when the user clicks onto the left or right button.
You can now compile and test the application.
You can either use a pencil and paper approach or use a drawing/painting program. If you are drawing on a piece of paper, then you may take a picture and upload the picture to blackboard. If this is not possible, then you have to submit a texfile that states the drawing will be handed in to the TA and give your drawing to your TA who will record the information (your mark for the quiz). You need to upload a file (either the diagram or text file explanation in order to get a mark).
Here is one more example to help you better understand graphical user interfaces. You can work on this example during the laboratory, if time allows, or take it home. The solutions are provided.
Follow the step-by-step approach proposed for the red dot application, and create an RPN calculator as displayed here.
Call this class Calculator. The display region is a TextArea. I used a panel with a GridLayout manager, 4 by 4 cells, to create the key panel.
Make sure to add a WindowListener that implements the windowClosing method.
Which class will be responsible for the handling the user generated events? It could be Calculator. How to determine which button was clicked? Use the class RPN below to implement the actions.
Last Modified: June 22, 2015