Académique Documents
Professionnel Documents
Culture Documents
Context of Unit 8
The purpose of this unit is to present the philosophy behind Java's graphical
user interface model, and to explore the classes required to implement
programs that exploit the model.
Objectives
Understand and implement applets that use the interface event model
Study Planning
You should expect to spend approximately 9 hours studying this unit. You may
find it convenient to break up your study as follows:
Disk-based Content: 2¼
hours
Application: 3 hours
Set textbook Content: 1 hour
Reflection (On-line discussions, review 1 hour
questions):
Tutorial Work: 1 hour
Related Coursework: ¼ hour
Extension Work: ½ hour
Equipment/software required
Online Resources:
1. Frequently-asked questions
2. Java 2D graphics overview on Sun's Java Web site
3. Additional Source Code Examples
Introduction to Unit 8
Such interfaces are often called WIMP interfaces (Windows, Icons, Menus and
Pointers).
The graphical interfaces provided by most modern operating systems look and
work similarly, but not identically. Technically, their implementation is totally
different. So how can Java programs run on any platform?
Most people expect the programs they use to have a graphical user interface
(GUI), particularly if the human-computer interaction is complex. Typical GUI
methods include selecting functions from menus, using a mouse to initiate
methods, and having a number of programs visible on the same screen.
Java currently supports two related sets of classes for creating a graphical user
interface. The early scheme, called 'AWT' (abstract window toolkit) is gradually
being supplanted by a more sophisticated scheme called 'Swing'.
Throughout the world, most people using desktop computers are using IBM-
compatible PCs. Indeed, for many people this will be the only desktop computer
with which they are familiar. However, there are other desktop computers that
are used widely enough to form a substantial market for software.
Of the people who use PCs, many will be using Microsoft Windows. Many
people are surprised to find that this is not the only graphical user interface
available, even for PCs. People using alternatives to Microsoft Windows on PCs
are a minority, but an increasing one. These people will need to be supplied
with software as well as the Windows users.
One of the design goals in the development of Java was to provide a system
where the same program would work on all these different systems, with
minimal effort on the part of the programmer. Therefore, for each program
developed, the potential market for the product would be increased, perhaps by
a factor of ten. This is good for the developers, as it can lead to increased
profitability; it is good for the consumers, as it makes a wider range of software
available.
The mechanism that Java uses to make a program portable is to abstract the
user interface. In a sense, the Java programmer works with 'simplified models'
of user interface elements - the Java runtime system is responsible for mapping
these onto the real user interface.
What is the 'Abstract Window
Toolkit'?
When thinking about Java one must remember that Java was designed so that
programs developed using it would work identically on any computer with a
Java system, irrespective of hardware and operating system.
This means that the Java programmer must never be allowed to communicate
directly with the computer's operating system, or with the 'real' user interface;
this would make the program platform-dependent. All user interface actions are
mediated by the AWT (or Swing) classes. The Java program creates and
manipulates AWT objects, and these objects manipulate the 'real' user
interface. The way this is done should be of no concern to the Java programmer;
it is automatic.
This is the origin of the word 'abstract' in AWT. In the same way that an abstract
class provides a specification, but not implementation, and abstract window
specifies how a screen window should behave, but does not define how it does
this.
The figure below illustrates how a Java program use AWT (or Swing classes)
that hide, from the Java programmer, the actual interface elements on
whichever computer system the Java program is being executed on.
For example, to the AWT a screen window that has a border and can be
manipulated by the user with a mouse is abstracted as the 'Frame' class. It is
just a rectangle with given size and position, which receives events (more of
which later). The Frame can contain other objects. The exact appearance of a
Frame on the user's computer will depend on the type of computer and the
operating system it uses. The action the user has to take to close the window
will vary from one system to another.
This scheme provides platform independence by separating the 'abstract'
concepts of the graphical user interface from their concrete implementation on
a given system. Technically the 'real' user interface element that the user sees
is called the 'peer' of the abstract element the Java program manipulates.
AWT classes
There is an AWT class (or a Swing class) for each user interface element that
Java understands. AWT classes provide functionality that is common to all 'real'
elements of that class - for example an AWT 'button' has a label and can be
'pushed'.
AWT classes form a hierarchy (discussed later in this unit). The hierarchy can
be expanded through subclassing by the programmer to extend functionality (to
some degree).
For every user interface element that the user will see on the screen, there will
be an object of a particular class in the Java program. These classes may be
standard ones provided by the AWT, or created by the programmer. For
example, to create a button to display, we could write:
(The argument supplied to the button constructor here is the text that will appear
on the button).
A simple HTML file can be used to make the appletviewer run the applet:
<BODY>
<APPLET CODE="Button1.class" height=100 width=200></APPLET>
</BODY>
Of course, we have not coded any actions to respond to the event of a user
clicking on the button with their mouse - so if the button is clicked nothing
happens.
Objects of these classes have a familiar appearance on the screen. They are have the
property that it is unusual to display another object inside them (in fact, in Java it is impossible
to put an object inside one of these basic objects). All these objects (except Label) can
respond to mouse clicks and keypresses by sending events to their listeners. This technique
is described in more detail below.
The exact appearance of these objects depends to a large extent on the computer system
you are using, but they should all work in the same way. The pictures shown below were
taken from a Java program running on a Windows NT system. The appearance on other
system will be slightly different, as illustrated below.
Button
List
A List is a simple list of text Strings. The user can select a particular item. Note
that the program must use the List's add() method to put the text items in the
list.
Choice
A Choice functions in the same way as a List, except that normally only the
selected item is visible. The other items appear when the Choice object is
selected by the user.
Label
A simple line of text. The user can't normally interact with this item.
TextField
TextArea
A TextArea is like a text field except that multiple lines can be entered.
CheckBox
For completeness, the following diagram shows the relationships between the
classes we have discussed so far. Remember the large arrow is the standard
UML symbol for the subclass relationship.
It is not necessary for you to remember the details of this model; it has been
included so that you can see how the various parts discussed so far, fit together.
So far we have seen how to create a user interface which could be quite
complex. However, as yet the user interface has no means of interacting with
the rest of the program.
Consider a simple program: an applet that has one button. When the button is
clicked it does something (it doesn't matter what, at this stage). For the button
to do anything at all, we must define a listener for it. The listener is the object
that will be notified when the user clicks the button.
The type of listener to create will depend on the type of object. For buttons we
require an action listener (the full Java documentation describes the listeners
that are appropriate for each object). The method that defines a listener for the
button is addActionLister(). So to create a button, put it in the applet's display,
and define a listener we would write:
Button myButton = new Button ("Click me!");
add(myButton);
myButton.addActionListener(...);
where the dots (&) indicate something missing. What are we going to put in
place of the dots? It can be any object that is a subclass of ActionListener. Note
that ActionListener is an interface, not a class. We can make any object a
subclass of ActionListener, provided that it provides an implementation for the
methods that ActionListener specifies. If you refer to the on-line documentation,
you will find that it only specifies one method:
public void actionPerformed (ActionEvent e);
In simple cases, we may define the applet itself to be the action listener for the
buttons it contains. This method is not very flexible, but works well for simple
applets. So the outline of the applet so far is:
import java.awt.*
import java.awt.event.*;
import java.applet.Applet;
public MyApplet()
super();
add(myButton);
myButton.addActionListener(this);
} // class
The line
myButton.addActionListener(this);
means make 'this' (i.e., this applet) the listener for the button called 'myButton'.
Whenever the button is clicked, Java will call the method actionPerformed. The
ActionEvent object which is passed into the method can be used to distinguish
which button was clicked if the same listener is listening to a number of different
buttons.
} // class
When the user enters the centigrade temperature e.g. an integer, followed by
the enter key, the Farenheit conversion is calculated and displayed .
1 import java.awt.*;
2
3 import java.applet.Applet;
4
5
6 import java.awt.event.*;
7
8 public class TempConversion extends Applet implements ActionListener {
9
10 Label CentrigradeLabel, FarenheitLabel;
11
12 TextField CentIn, FarOut;
13
14
int f, c, answer;
15
16
17 public void init(){
18
19 CentigradeLabel = new Label("Enter Centigrade temperature ");
20
21
CentIn = new TextField(5);
22
23
24 FarenheitLabel = new Label ("Temperature Farenheit ");
25
26 FarOut = new TextField(5);
27
28 add(CentigradeLabel);
29
30
31 add(CentIn);
CentIn.addActionListener(this);
add(FarenheitLabel);
add(FarOut);
return answer;
if (e.getSource()==CentIn)
c = Integer.parseInt(CentIn.getText());
f = convert(c);
FarOut.setText(Integer.toString(f));
Container layouts
The objects described above (Button, Menu. etc.) do not normally contain other
objects. However, AWT supports classes whose objects can contain other
objects. You will have seen at least one of these already: the Applet class. In
the AWT, all classes of this type are subclasses of Container. A container object
has no display of its own, but exists to contain other objects.
One of the most important of these is the Panel. A Panel is simply a blank piece
of screen into which other objects can be inserted. Panels are often used in
conjuction with the layout manager to create complex displays, as described
later.
When working with applets, the applet viewer or Web browser automatically
creates a screen window for your program's output to appear in. However, if
you are writing a stand-alone application (described in the next unit) your
program must create its own main window. Typically an object of class Frame
does this. The figure below shows the appearance of an empty Frame object
on a Windows system.
This can be created by:
myFrame.show();
Of course, an empty Frame is not particularly useful. The next unit describes
the role of the Frame class in providing stand-alone Java applications.
The following applet will create a separate, frame window, apart from the applet
window:
import java.applet.Applet;
import java.awt.*;
public class Frame1 extends Applet
{
public Frame1()
{
Frame myFrame = new Frame("A frame created by an applet");
myFrame.setSize( 300, 200 );
myFrame.show();
}
} // class
As with the menu frame example, since no code has been written to deal with
actions, you cannot close this frame window. However, when you close the
applet window, the frame object will be destroyed and its window removed.
The use of a layout manager relieves the programmer of the effort of managing
the screen layout when large numbers of components are in use, but it does
take some time to get used to. The great advantage of the layout manager is
that it usually responds sensibly to changes in the size of a program's main
window, by re-organizing the components so that they are all visible (if
possible). The disadvantage of a layout manager is that it is difficult to specify
exactly how the display should appear.
The default layout manager is called the 'flow' layout manager. As each new
object is added, it positions it to the right of the previous one. If there is no room
to do this, it positions it below and at the left-hand edge of the window. Thus the
order of placement of objects is the same as the order of reading English text.
There are other types of layout manager, as will be shown below.
Types of layout manager
The 'flow' layout manager places objects from left to right and top to bottom.
The 'grid' is similar, but it attempts to line up objects on a grid of a fixed size.
This will usually result in objects being spaced more widely than the 'flow' layout.
A 'border' layout has five regions: the centre and the four borders. Objects can
be placed specifically into any of these regions.
The 'card' layout is different in that it only displays one object at a time (like a
stack of cards). Methods are provided to select which object is currently on 'top',
and therefore visible.
Changing the layout manager for a particular AWT object is straightforward: use
the setLayout() method
setLayout should be given the name of a new object of the appropriate layout
manager
For example, here is how to create a new Panel and spacify that it has a 'border'
layout.
Panel p = new Panel();
P.setLayout (new BorderLayout());
Complex layouts
Panel objects are invisible, and exist only to contain other objects
Often some trial and error is required to achieve the desired effect
The example below shows how to achieve a more complex layout than is
offered by the standard layout managers. It uses a two-layer structure. The
overall layout is provided by a Panel with a border layout. The Panel has no
'real' (user-accessible) objects, but contains two other Panels. These in turn
contain the objects that the user sees.
An alternative approach is to use the 'grid bag' layout.
User interface design tools are useful for designing complex layouts; they
quickly show how the display will appear without compiling the program. This
allows the programmer to make changes to the layout structure and see the
effect immediately.
As with the concept map from the earlier units, you can use the map below as
a way to retrieve glossary entries. You might wish to re-visit this concept map
(and perhaps create / extend it for yourself on paper) as you progress through
the module.
Activity 1 - ButtonCount applet
Write a Java applet that creates and displays a Button object and a Label with
a message displaying the number of times the Button has been pressed. The
display should look something like this:
Discussion of Activity 1
Implement a prototype for a fruit machine simulation using Java AWT similar to
the one displayed :
Discussion of Activity 2
There are many possible ways in which this can be done, the solution given below uses two types
of layout managers: the Grid and the Border layouts.
import java.awt.*;
import java.io.*;
import java.applet.Applet;
import java.awt.event.*;
// prototype display
public class FruitMachine extends Applet {
private Button go,stop,query;
private TextField one,two,three;
private Panel buttons, textfields, message, display, welcome;
private Label prompt, response;
public void init(){
buttons = new Panel();
welcome = new Panel();
textfields = new Panel();
message = new Panel();
display = new Panel();
response = new Label(" You won £ 3.00 ");
prompt = new Label (" The CMT4120 Fruit Machine ");
welcome.add(prompt);
add(welcome);
display.setLayout(new BorderLayout());
buttons.setLayout(new GridLayout(1,3,5,5));
go = new Button(" GO ");
buttons.add(go);
buttons.add(query);
stop = new Button("Stop");
buttons.add(stop);
display.add("North", buttons);
one = new TextField(3);
textfields.add(one);
one.setText("5");
two = new TextField(3);
textfields.add(two);
two.setText("5");
three = new TextField(3);
textfields.add(three);
three.setText("5");
display.add("Center", textfields);
message.add( response);
display.add("South", message);
add(display);
}
}
2. How many AWT objects are there in this example, and what are they?
4. When the user clicks the right hand button in the caption bar of the window
will the window close?
5. How would the applet behave if the statement add(myButton); was missing?
Discussion of Exercise 1
1. ActionListener
6. this is a special read only variable that contains a reference to the current object. In the
given example it ensures that the CentIn object will be registered with the
ActionListener.
8. The applet would not compile, the actionPerfomed method must be implemented
because of the ActionListener interface.
9. The actionPerformed method will be invoked when an event takes place passing this
information to the ActionEvent object e. The ActionEvent class has useful methods for
identifying the particular object where the event occurred such as getSource making
it possible to instruct the application to perform the specific tasks required.
10. The repaint method will call the paint method when an event takes place so that the
applet redraws itself.
Answer each of the following questions, giving a full explanation to justify your
answer.
4. List the statements which are messages to the text field objects?
7. Explain how the convert(int x) method works i.e. where it is declared, where
invoked etc. giving the line numbers
8. Would the applet work if the two labels: CentigradeLabel, FarehneitLabel
were not used at all?
9. What would the display look like if the size of the window was 600 x 500?
10. What would happen if the input was not integer e.g if the user enters 25.5?
Discussion of Exercise 2
2. The two Labels CentigradeLabel, FarenheitLabel the two text fields CentIn, FarOut,
and the applet TemperatureConversion.
5. All input and output in Java is string, and in this example as we need numeric integer
input, the string from the text field has to be converted to an integer.
6. The reverse operation is needed to display the numeric result in the text field so the
integer result i is passed as an argument (parameter) to the toString method.
8. The applet would work and perform the requested tasks but with no labels.
10. The calculation would not be performed, in other word unless the input was integer it
would be ignored.
Analyse the BallWorld.java example from Unit 2 and answer the following
questions:
1. How many AWT component types are there in the example and what are
they?
2. Are there any containers used in this example, if so what are they?
8. Will the BallWorld Frame window close if the top right hand button is clicked?
Discussion of Exercise 3
1. Assuming that we are not using version 8 there are two. The Ball object(s) and the
BallWorld frame.
Review Question 1
What visible graphical user interface elements does the AWT principally
support?
Review Question 2
Why does the Java AWT not provide support for round buttons or oval
windows?
AWT provides functionality which is likely to be common across the whole range of
computers on which a Java program is likely to run. Few computers have built in support
for round screen objects, so this facility cannot be provided by the AWT. In any case, the
AWT developers could not provide support for every conceivable user interface element;
sometimes the programmer has to do some work as well!
Review Question 3
The ability to create programs with graphical user interfaces is clearly very
important in modern practice. Yet the Sun JDK tools have no graphical user
interfaces (with the exception of the applet viewer). Why? Under what
circumstances might it be better not to provide a GUI?
There are at least two good reasons not to provide a GUI for a specific program. First, it
may be unnecessary. The program may be sufficiently simple that it doesn't need one.
The philosophy behind Microsoft Windows (rightly or wrongly) is that all programs,
however trivial, have a graphical user interface. However, to find out the amount of free
space on all the hard disks on a Unix system requires only the typing of the command df
(three keystrokes including 'enter'). The same method on a Windows '95 system requires
three mouse clicks for each disk, plus several to find and launch the 'My Computer'
window. A graphical user interface undoubtedly makes a program easier to learn to use;
however, in many cases it is not efficient for an expert user. Second, not all computers
have a GUI facility. Think in particular about the small computers that are embedded in
devices that use electronic equipment like video recorders and car engines. To develop
and debug applications on a computer without a GUI, the development tools have to work
without a GUI.
Review Question 4
Why can a menu bar not be added to an Applet, when it can to a Frame?
Despite the apparent reasonableness of this explanation, it would have been nice to have
the facility to display a menu in an applet.
Discussion Topics
There are many new concepts in this unit. If you want to discuss with your fellow
students or make comments about the concepts to them, use the on-line
facilities.
The Apple Macintosh range of computers has traditionally had a mouse with
only one button. This means that to be fully portable, Java programmers cannot
rely on the user having access to a two-button or three-button mouse. However,
users who do have a mouse with more than one button ought not to be
constrained by this. How can the Java programmer attempt to satisfy both
groups of users?
Answer to Discussion Topic 1
Mouse buttons send events. Each mouse button sends a different event. It is possible to offer
multiple ways of generating a particular event, so for example an alternative to clicking the right
mouse button on a PC might be to be pressing <SHIFT> while clicking the single mouse button
on a Mac. Or perhaps a key could be pressed on the keyboard, that would result in some action
on the object over which the mouse pointer sits.
So general purpose Java programs can be written, to take advantage of local hardware, without
disadvantaging those with standard hardware. Such programs must be written to deal with
'abstract' event messages, for example DISPLAY_POPUP_MENU_REQUEST(), and the
program has been written to process multiple ways of generating these messages - so there might
be methods to process right button clicks, and clicks when <SHIFT> is held, both of which result
in the sending of a DISPLAY_POPUP_MENU_REQUEST() message.
Manufacturers of some Java compiler products include class libraries that are
not platform-independent. Programmers that use these libraries run the risk of
their programs not being portable. For example, Microsoft's 'Visual J++' product
is shipped with class libraries that interact directly with the Windows user
interface. This allows the programmer to create displays that take advantage of
Windows elements that are not supported by AWT or Swing.
Advantages:
speed
loss of portability - the program can only run on one type of system
difficulty to maintain/add to program - only Java programmers with a knowledge of the
system specific features can work with the code
multiple versions and duplication of work for distribution to multiple systems - to offer a
program that takes advantage of local system features to more that one type of system
means that system specific code has to be written for each system to ship to - i.e. all of
the portability issues are lost, and multiple versions of the program have to be maintained
(so one change to program functionality means updating all different versions of the
software), possibly each version has to be developed and compiled on a system the same
as the destination system (and each program version has to be tested on each destination
system)
In your Learning Journal write up your experience of your learning on this unit.
Say what you thought was good or bad, what you had difficulty understanding,
and how you resolved your problems.
AWT uses three main classes to manage and display menus, as shown in the
figure below. There is also a 'PopupMenu' class. A PopupMenu is like a Menu,
but usually appears when the user clicks the mouse and disappears after an
item has been selected.
The procedure for constructing a complete menu in Java will be described later.
Note: In Java, applets can't have menu bars. A menu bar can be added to a
Frame window (described later), so stand-alone applications can have menus.
Applets can have a 'pop-up' menu, that is, a menu which becomes visible when
the user clicks the mouse in the applet.
The frame of this window has2 menus: 'File' and 'Help'. The 'File' menu has the
following items:
New
Open
Save
-------
Exit
About
The Java code to generate this application is in listing: FrameMenu1.java.
NOTE
When running this application, since no actions have been created to deal
with events, in addition to the menus having no behaviour, you will have
to terminate the application by pressing CTRL-C in the console window
where you ran the application.
The easiest way to create a new user interface element is to define a subclass
of an AWT class that is similar.
For example, you could create a class called RedButton which was identical to
the standard Button class except for having a red background (the default is
grey). The code for this might look like:
public class RedButton extends Button
{
public RedButton (String label)
{
super (label);
setBackground(Color.red);
}
}
But we could not use this method to create, say, a round button, or a button that
pops up a message when the mouse moves over it. Why not? Class Button is
an abstraction of the 'real' button that appears on the display. It only offers
functionality that is common to all or most real computer systems. Few real
graphical user interfaces allow circular buttons to be created automatically, so
class Button cannot provide this. This is a necessary price to pay for portability,
but it can be very frustrating.
So how could you create a circular button? Probably the easiest way is to create
a subclass of a different AWT element. A good candidate here would be
Canvas. A Canvas object is simply a blank, rectangular region of the screen.
Your program would have to draw the button as it would appear, and take
appropriate action when the user clicks on it. This is significantly more complex
than subclassing Button.
Look back to the Button1.java applet - perhaps try changing it to use the
RedButton class above.
One object is placed inside another, or attached to another, by using the add()
method. Objects can be added inside other objects which in turn are inside
other objects, giving a nested structure.
The overal layour is controlled by the Layout Manager (discussed later in this
unit).
This example shows how a Menu object (labelled 'File') is created, and menu
items 'Open', 'Save' and 'Exit' placed inside it.
Menu fileMenu = new Menu("File");
MenuItem openMenuItem = new MenuItem ("Open");
MenuItem saveMenuItem = new MenuItem ("Save");
MenuItem exitMenuItem = new MenuItem ("Exit");
FileMenu.add(openMenuItem);
FileMenu.add(saveMenuItem);
FileMenu.add(exitMenuItem);
If we want an applet to contain a text field and a list box we could write in the
applet's constructor:
TextField textField = new TextField();
List list = new List();
add(textField);
add(list);
The positions of the text field and the list box will depend on the size of the
applet in which they are being placed. If there is room to show them side-by-
side, the standard layout manager will place them like this. However, if there is
not room it will place one above the other. Each AWT object that can contain
other objects is assisted by a layour manager whose job it is to keep the objects
in an orderly arrangement.
For comparison, the figures below show how the different AWT objects appear
on two different systems.
The first display was obtained on a Unix system. The second from a Windows
system.
Note that the components appear similar to the Windows versions shown
above, but not identical.
The Swing user interface model is a development of the AWT model. It has
more classes and supports a greater range of user interface elements. The
event handling process is similar in Swing and AWT.
progress bars
Write a Java applet that creates and displays an object of each of these types
Button
Label
Choice
List
Checkbox
TextArea
TextField
Don't change the layout manager from the default 'flow' layout.
Resize the applet viewer's window and watch what happens to the elements on
the display.
Resize the applet viewer's window and watch what happens to the elements
on the display now.
to
When you resize the window now, the components stay in their positions on the grid.
Additional Exercises
With this in mind, write the Java statements that create a menu with the
following structure.
File
New
------
Open
Save
Save as…
-----
Exit
Edit
Cut
Copy
Paste
Help
Contents
Search…
-----
About…
Discussion/Solution to Exercise 4
Note that the order of appearance of the items in each Menu depends on the order they were
added using the add() operator. It does not depend on the order in which they were created.
It has been stated that Button objects send an event when they are clicked. This
is intuitive: the only purpose of a button is to be clicked, and then something
must happen. Using your intuition and experience of how graphical user
interfaces work, suggest what events will be sent by each of these objects.
Describe the events in English; don't try to guess the Java names. You may
have to refer back to the text to remind yourself what these objects represent.
Hint: there is at least one for each class. Because this is an exercise in intuition,
not knowledge, there are many possible correct answers.
List
Choice
Checkbox
TextField
TextArea
When you have made your list, look in the Java documentation for each class
and see if your intuition agrees with that of the Java developers. In the
documentation, look for methods with names like 'addXXXlistener', which 'XXX'
will tell you the type of listener being used.
Discussion/Solution to Exercise 5
Here is a list of possible events; it is not exhaustive and you may have others.
List
Choice
Check box
Text field
Text area
Same as TextField
Study the example program Calculator.java and answer the questions in it.
Discussion/Solution to Exercise 6
1. The applet implements ActionListener and KeyListener because it receives events from its
buttons. These events arise when the user clicks the mouse (ActionListener) or presses the space
bar when a button is selected (KeyListener).
2. These constants are used in several different places in the program. If the program had to be
modified and these values had to be changed, it would be very difficult to figure out which part of
the program to change.
3. 'North' specifies to the BorderLayout class that the component is to be added in the top border
of the layout (It might have been better to use the word 'Top' but the Java developers decided on
'North')
4. lastOperator will contain the character '+', as this was set when the user clicked on the '+' button
(in the method 'processOperator()'. lastNumber will contain the number '2', as this was also set
when the user clicked on the '+' button. (When the user clicked '+', the number '2' would have
been in the display). The variable numberInDisplay will be set to the number currently in the
display, which is '3'. So we have all the information we need to carry out the calculation.
5. This is quite a subtle point. Well done if you got this right! If you are entering a number, you
might be planning to enter a number that starts with a zero (e.g., 0.1, 0.05). If you press the sign-
change key, the program should change the sign even if you have entered 0.0 (because you are
planning to enter a proper number). But 0.0 is the same as 0 if you are working in numbers, so
your typing would be erased if this was read as a number. So when the user enters a number it
is read as a string, to be sure that the users entry is not lost.
6. If you have just finished a calculation, then when you enter a digit it should replace whatever is
in the display and start a new number. The attribute clearOnNextDigit is set to true whenever the
next digit should cause the display to be cleared.
7. The method processButton() requires a String as a parameter. For example, if you click the
button marked '1' it expects to get a String containing the text "1". However, when the user presses
a key, the user sends an event that specifies the single byte corresponding to that key. The two
lines indicated construct a String containing this one byte.
http://www.yahoo.co.uk/Computers_and_Internet/Programming_Languages/JavaScript
Review Question 5
In the AWT menu model, what objects contain what other objects?
Review Question 6
Objects of which AWT classes send events to an 'item listener'? (There are at
least two, and if you've done activity 2 fully you'll know what they are).
An ItemListener is used by List and Choice objects. The ItemListener receives events when the
user selects an item in the list.
Review Question 7
In the AWT menu model, what objects contain what other objects?
Answer to Review Question 7
Review Question 8
Objects of which AWT classes send events to an 'item listener'? (There are at
least two, and if you've done activity 2 fully you'll know what they are).
An ItemListener is used by List and Choice objects. The ItemListener receives events
when the user selects an item in the list.
Your performance with these questions will not affect your grade for the module,
but may be monitored so that your tutor can be alerted if you are having
difficulty.
Please contact your tutor if you feel you have not done as well as you expected.
Also please provide a contents page for each unit's program listings.
Listing of Calculator.java
/*==========================================================
Calculator.java
sort that can only add, divide, subtract and multiply). Your
<img src=calculator.gif>
<p>
(If you aren't looking at the HTML version of this file, you won't see
the picture)
To signal that the user has tried to do something that is mathematically
==========================================================*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*==========================================================
Calculator class
The Calculator class provides most of the functionality for this program. After
==========================================================*/
/*==========================================================
==========================================================*/
// the user can be. A Java `double' can store a value with a precision of
/*
Display modes
The variable `displayMode' can take one of three values. For ease of reading
will either be the number being entered by the user (INPUT_MODE), the
the program. For example, if the user types a digit when the program is
should be cleared and the new digit put at the start. In fact,
value of displayMode
*/
/// QUESTION: why have I defined constants for these values? In the program
int displayMode;
// displayLabel is the area of the applet in which all display will be shown.
// I have used a Java `Label' object for this.
Label displayLabel;
// typed. This causes the next digit entered to be the start of a new number.
boolean clearOnNextDigit;
// lastNumber is the number that was entered by the user before the last
operator.
// for example, if the user type '2' followed by '+', then lastNumber will be
// set to '2'
double lastNumber;
// lastOperator is the operator last typed by the user. If the user types a
// '+' sign (or clicks on the '+' button), then lastOperator is set to
char lastOperator;
/*==========================================================
Calculator constructor
This operator (as with all constructors) is executed when the new object
is created. In this case we create all the buttons (and the panels that
The layout of buttons in this Applet is quite complex. We have three groups
of buttons, one for digits, one for operators and one for 'controls'
(`=', `AC' and `C'). I'm not sure `controls' is the right word here, but
layout manager. The job of the layout manager is to group the buttons in
the right order. For example, the button panel has a grid of buttons, with
7 8 9
4 5 6
1 2 3
. 0 +/-
So we set the layout manager for this panel to a new GridLayout object.
which means `create a new GridLayout with four rows of three columns'.
Having created the panels, we add the buttons. Finally we add all the
panels into a single panel (called `buttonPanel') and add this to the
super();
buttonPanel.add(numberPanel);
buttonPanel.add(operatorPanel);
buttonPanel.add(controlPanel);
displayLabel.setAlignment(Label.CENTER);
add(displayLabel, "North");
displayResult(0);
add(buttonPanel);
addKeyListener(this);
requestFocus();
clearAll();
/*==========================================================
setDisplayString
This method sets the text in the display area to the specified string.
it is called many many other methods, and using a method for this
have to be modified.
==========================================================*/
void setDisplayString(String s)
displayLabel.setText(s);
/*==========================================================
getDisplayString
This method gets the text currently in the display area. See the note for
==========================================================*/
String getDisplayString ()
return displayLabel.getText();
/*==========================================================
clearAll
Sets the state of the calculator to the `just switched on' state, that is,
`0' in the display, ready to input digits, and the last operator equal to
zero (that is, there is no last operator). This method is called by the
clicks on `AC'
==========================================================*/
void clearAll()
setDisplayString("0");
lastOperator = 0;
lastNumber = 0;
displayMode = INPUT_MODE;
clearOnNextDigit = true;
}
/*==========================================================
clearLastEntry
Clears the number currently in the display. This is called when the user
==========================================================*/
void clearLastEntry()
setDisplayString("0");
clearOnNextDigit = true;
displayMode = INPUT_MODE;
/*==========================================================
displayResult
Displays the specified number in the display area, and sets the variables to
a digit, the display will be cleared to make space for a new number.
==========================================================*/
void displayResult(double result)
setDisplayString(Double.toString(result));
lastNumber = result;
displayMode = RESULT_MODE;
clearOnNextDigit = true;
/*==========================================================
displayError
Displays the specified error message in the display area, and sets the
as the user types a digit, the display will be cleared to make space
error message and not a number. This is important because the user might
press a button that modifies the current number (like `+/-'). Clearly
we can't change the sign of an error message (or take its square root, etc).
==========================================================*/
void displayError(String errorMessage)
setDisplayString(errorMessage);
lastNumber = 0;
displayMode = ERROR_MODE;
clearOnNextDigit = true;
}
/*==========================================================
addButtonToPanel
number of lines in the program (and therefore make it more convenient for the
a new button to the display. It sets the buttons colour, and sets the
key listener and action listener to the appropriate methods (this has
to be done for every button, as we can't predict which button will have
==========================================================*/
void addButtonToPanel(Panel panel, Button button, Color backgroundColour)
panel.add(button);
button.setBackground(backgroundColour);
button.addKeyListener(this);
button.addActionListener(this);
/*==========================================================
actionPerformed
This method is called whenever the user clicks a button. This happens
`processButton'
==========================================================*/
public void actionPerformed (ActionEvent e)
processButton(e.getActionCommand());
/*==========================================================
processButton
This method takes action according to the user's input. It is called from
keyPressed (when the user presses a key on the keyboard). This method
examines the text on the button (the parameter `command') and calls the
==========================================================*/
void processButton(String command)
if (command.equals("0")) addDigit(0);
if (command.equals("1")) addDigit(1);
if (command.equals("2")) addDigit(2);
if (command.equals("3")) addDigit(3);
if (command.equals("4")) addDigit(4);
if (command.equals("5")) addDigit(5);
if (command.equals("6")) addDigit(6);
if (command.equals("7")) addDigit(7);
if (command.equals("8")) addDigit(8);
if (command.equals("9")) addDigit(9);
if (command.equals(".")) addDecimalPoint();
if (command.equals("*")) processOperator('*');
if (command.equals("-")) processOperator('-');
if (command.equals("/")) processOperator('/');
if (command.equals("+")) processOperator('+');
if (command.equals("=")) processEquals();
if (command.equals("+/-")) processSignChange();
if (command.equals("AC")) clearAll();
if (command.equals("C")) clearLastEntry();
/*==========================================================
getNumberInDisplay
Returns a double value indicating the number in the display. This method
should never be called if the display does not contain a number. When an
return Double.parseDouble(input);
/*==========================================================
processLastOperator
Carries out the arithmetic method specified by the last operator, the last
If the operator causes an error condition (i.e., the user tries to divide
==========================================================*/
/// QUESTION: if the user clicks on the buttons `2', `+' and `3', what values
/// will found in the variables `lastOperator' and `lastNumber', and the
double result = 0;
case '*':
break;
case '+':
break;
case '-':
break;
case '/':
if (numberInDisplay == 0)
break;
return result;
/*==========================================================
processOperator
_last_ operator typed (not this one), and store this current variable
==========================================================*/
void processOperator(char op)
if (displayMode != ERROR_MODE)
if (lastOperator != 0)
try
displayResult(result);
lastNumber = result;
catch (DivideByZeroException e)
displayError("Division by zero!");
else
lastNumber = numberInDisplay;
clearOnNextDigit = true;
lastOperator = op;
}
}
/*==========================================================
processEquals
Deals with the user clicking the `=' button. This method finishes the
==========================================================*/
void processEquals()
if (displayMode != ERROR_MODE)
try
displayResult(result);
catch (DivideByZeroException e)
displayError("Division by zero!");
lastOperator = 0;
}
/*==========================================================
processSignChange
This method is called when the user clicks on the `+/-' (sign change)
the number in the display is zero, nothing happens (as `-0' is meaningless).
==========================================================*/
/// QUESTION: Why is this? Why does this method have to behave differently
/// is quite a subtle problem. The program would work correctly most of the
void processSignChange()
if (displayMode == INPUT_MODE)
if (input.indexOf("-") == 0)
setDisplayString(input.substring(1));
else
setDisplayString("-" + input);
if (numberInDisplay != 0)
displayResult(-numberInDisplay);
/*==========================================================
addDigit
This method is called when the user clicks a digit button, or types a
digit key. If the number currently being entered is less than 17 digits long,
==========================================================*/
void addDigit(int digit)
{
if (clearOnNextDigit)
setDisplayString("");
// like `000'. If the number in the display is `0', and the user
setDisplayString(inputString + digit);
displayMode = INPUT_MODE;
clearOnNextDigit = false;
/*==========================================================
addDecimalPoint
Called when the user clicks on the decimal point button. Puts a decimal
point on the end of the number currently being entered. If the number
void addDecimalPoint()
{
displayMode = INPUT_MODE;
if (clearOnNextDigit)
setDisplayString("");
// do anything to it.
if (inputString.indexOf(".") < 0)
/*==========================================================
keyPressed
This method is called when the user presses a key, when any button has the
nothing.
==========================================================*/
public void keyPressed(KeyEvent e)
{
}
/*==========================================================
keyReleased
Called automatically whenever the user presses and then releases a key. See
==========================================================*/
public void keyReleased(KeyEvent e)
/*==========================================================
keyTyped
This method is called when the user presses a key, and then releases it
when any button has the input focus. This happens because the method
about.
This method converts the key press to the text of the corresponding button,
presses `enter', this should have the same effect as clicking `='. So this
presses `escape' this should be treated the same as the `C' button. All other
keys are treated as digits. If the user presses a letter rather than
==========================================================*/
public void keyTyped(KeyEvent e)
String command;
if (keyChar == KeyEvent.VK_ENTER)
else
processButton(command);
}
} // class
/*==========================================================
DivideByZeroException class
This class is used to indicate that the user has tried to divide a number
==========================================================*/
class DivideByZeroException extends Exception
DivideByZeroException()
super("Divide by zero");
Listing of FrameMenu1.java
// Menu1.java
import java.awt.*;
public FrameMenu1()
setVisible( true );
// set up menus
fileMenu.add(newMenuItem);
fileMenu.add(separator);
fileMenu.add(openMenuItem);
fileMenu.add(separator);
fileMenu.add(exitMenuItem);
helpMenu.add(aboutMenuItem);
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setMenuBar( menuBar );
} // class
Listing of Button1.java
// Button1.java
import java.applet.Applet;
import java.awt.*;
public Button1()
add (myButton);
} // class
Listing of Listener1.java
// Swing implementation
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Listener1()
super();
add(myButton);
myButton.addActionListener(this);
JOptionPane.showMessageDialog(null, "ouch!");
} // class