GUIs

kitewormsSoftware and s/w Development

Nov 3, 2013 (4 years and 1 month ago)

89 views


GUIs
1

GUI programming


Interfaces, subclasses, and abstract classes not only facilitate re
-
use of code we write ourselves,
but also facilitate code re
-
use on a large scale by making it easier to write libraries

of related and
interacting classes
.
Two important e
xamples are libraries for (i) collections, and (ii) GUIs
(graphical user interfaces). Collection classes (such as
ArrayList
,
LinkedList

etc.) make
it easy for the programmer to manage collections of objects.

GUI classes allow the user to write
graphical us
er interfaces. Java’s GUI
classes are located in two packages called the AWT and
Swing packages (it is usual to see
import java.awt.*;

and

import java.swing.
*;

in programs that use GUIs).


AWT and Swing

make huge use of inheritance, interfaces, and abstr
act classes. Abstract classes
are used where it is possible to
include

most of the
underlying
code
for some concept (such as a
visible window
)

in the library
,
but the final details must be supplied by the user to suit the
context

(such as the action to be
taken when a mo
u
se
-
click is made on the window)
. Interfaces
are used where a method in the library requires that an object
passed as a parameter has certain features
. F
or example, a
library method to sort an array of objec
ts will need to insist that
the

ob
jects
can be compared with one another

by

belong
ing

to
a class that implements
Comparable
.


As an example of GUI programming, consider the

well
-
known
15
-
puzzle with the
user
interface shown. T
he
outline
shape of
the program is:


import java.awt.*;

import
javax.swing.*;

import java.awt.event.*;


class Puzzle15 extends JFrame {



private JButton[] pb = ...// numbered buttons


...




Puzzle15() {


super(" The 15 Puzzle");



...


}


...



class Standard implements ActionListener
{


// for click of a standard
-
of
-
play button


...




}




class PlayButton implements ActionListener {



// for click of a numbered button


...


GUIs
2


}




class WindowHandler extends WindowAdapter {


// for window

closing click



...


}




public static void main(String[] s) {


new Puzzle15();


}

}

.

See the complete code on the module web page.



As an example of the class hierarchy in the Java library, class
JFrame

used above extends
anoth
er library class
Frame
, which in turn extends
Window
, which extends
Container
,
which extends
Component

(
and
which, of course, extends
Object
). On the other hand,
WindowAdapter

used above
is
an abstract class, and the purpose of
WindowHandler

introduced in
the program
as an extension
is to supply the missing details appropriate to the
context of the 15
-
puzzle.

Finally,
ActionListener

is an interface


You know
all
the Java you need to write GUIs. Study how to do so
with the help of

any Java
textbook, and expe
riment on some small programs.


GUI programming is not examinable.