Java Reference
In-Depth Information
Frame
Titlebar
Down
Up
14
Button
Label
Button
Figure 3.1 The specification of the layout for the counter GUI
increment
- increments the value of the counter by 1.
■
decrement
- decrements the value of the counter by 1.
■
reset
- sets the value of the counter to 0.
■
getValue
- returns the current value of the counter.
■
Next, we specify what the graphics should look like and how it is to work. The GUI
we have in mind should display the current value of the counter and allow the user
to increment or decrement it. To this end the GUI has two
buttons
. Pressing the
first one (labelled 'Up'), e.g. clicking it with the mouse, will increment the counter.
Pressing the other one (labelled 'Down') will decrement the counter. Figure 3.1
shows how we want this GUI to look. This concludes the specification of the GUI's
functionality and layout.
■
3.2
The counter model
We now implement a counter in the non-graphical class
CounterModel
.Acounter
has only one integer variable
value
which is initialized to 0 in the constructor as
required by the specification. The variable
value
is
private
so that other classes
can only manipulate it through the methods of
CounterModel
.Weimplemented
the four operations requested by the specification in four methods with obvious
names:
increment()
,
decrement()
,
reset()
and
getValue()
. Here is the listing
of the class:
File:
its/CounterGUI/CounterModel.java
1.
package
its.CounterGUI;
2.
public class
CounterModel {
3.
private int
value;
4.
Search WWH ::
Custom Search