Lecture15: Interface, GUI

CS 51
Lecture15: Interface, GUI
Interfaces
Occasionally we wish to have a variable in a program that can refer to objects from several
different classes.
e.g. variable can refer sometimes to a framed square and at other times to a framed circle.
- cannot declare the variable to have both types FramedRect and FramedOval
- type of variable something that includes objects from both classes: INTERFACE
Interface – allows programmer to describe exactly what features are in a particular programming
context and to declare variables and parameters that can refer to values from more than one class.
The objectdraw library uses interfaces to classify the graphics objects that can be drawn on the
screen.
Interface DrawableInterface, includes methods

move()

moveTo()

hide()

show()

contains()

setColor()

getColor()

SendToFront()
All of the geometric classes from the objectdraw library implement DrawableInterface interface






FramedRect
FilledRect
FramedOval
FilledOval
Line
Text
private DrawableInterface lastShape;
public DrawableInterface getLastShape(){
return lastShape;
}
if ( shapeSelector.nextValue() == 1 ){
lastShape = new FilledRect(x, y, w, h, canvas);
} else {
lastShape = new FilledOval(x, y, w, h, canvas);
}
LastShape.setColor(Color.RED);
GUI – Graphical User Interface
Text field - Class JtextField
private JtextField input;
Input = new JtextField(“Enter Text here”);
Input = new JtextField();
Appearance on the window:
Add it to the “content pane” of the window ( = interior of the window)
To obtain the content pane of a class extending WindowController
Container contentPane = getContentPane();
Container object can hold GUI components – text field, buttons, menus .
Container object’s layout manager by default is BorderLayout (center, north,
south, east, west) (center is canvas)
Adding a component to a container that uses BorderLayout
contentPane.add(input, BorderLayout.NORTH);
The validate method is used to
cause a container to lay out its
subcomponents again.
Java events and event-handling methods
Buttons - clicks on buttons trigger program actions
Jbutton clearButton = new Jbutton(“Clear Canvas”);
Button clicked NO onMouseCLick() is executed, instead actionPerformed() is
executed
public void actionPerformed( ActionEvent evt ){
canvas.clear();
}
The object that
triggered the event
evt.getSource() - returns GUI component that triggered an event evt
Listener
A program to respond to an event generated by a GUI component we must designate objects
to be notified when an event is generated by that component.
The object to be informed when an event is generated by a component is event listener
Different kind of events require different type of listeners
Listener requirement – promises to provide an appropriate method to be triggered when the
event occurs.
Pressing button - any listener for a button must implement the method actionPerformed()
Associate a listener with a button
clearButton.addActionListener( actionLisenerObject );
As a result actionPerformed() will be executed each time the button is pressed.
Any object that implements ActionListener interface can be used as a parameter for addActionListener
TextButtonController object will do the listening – must implement the interface ActionListener and
include method actionPerformed()
Inform the system that we want our applet to gain “focus”
Compnent that has “focus“ the key events are directed to it