Java Reference
In-Depth Information
A first GUI
3
The purpose of graphical interfaces is to display data and to allow a communication
between the user and a program at runtime. In this chapter we shall learn how to
design such graphical interfaces. In this example the user can communicate with the
program by pressing buttons. The program updates the display in reaction to that.
In this chapter we shall see the first example of a real GUI ( graphical user in-
terface ). 'Real' here means that we have a user-program interaction. The inter-
action is realized using buttons in the graphical display. Although the program
is very simple we would like to introduce the concept of a model-view-control
approach at this point. The (non-graphical) model part of the program deals with
storing, maintaining or manipulating the data. The graphical view part displays
the data and provides the components for user interaction, e.g. buttons. The (again
non-graphical) control part ensures that the user actions result in the desired re-
sponses by the program. The control part is the bridge between the model and view
parts.
The separation of the model, view and control structures is often the crucial
concept to a successful save and fast implementation. It also helps beginners to
better recognize the essential concepts and their interplay. In Java such a sep-
aration is easily possible by using object-orientation, i.e. different classes or at
least different methods for the different parts. In complex applications separate
packages might be used for the different parts.
In the following we start by specifying the GUI we want to implement. Then we
construct the non-graphical model and test that it works correctly. This is followed
by the design of the graphical display. Finally we implement the control structure,
i.e. the user-program interaction.
3.1
The specification of the application
We want to implement a counter .Itisspecified as an abstract data structure.
The counter has a variable value which is an integer. Initially the value is 0. The
counter allows four operations (of course, more operations might be useful in
some applications):
Search WWH ::




Custom Search