Vous êtes sur la page 1sur 40

The Robert Gordon University

Object Oriented Programming


Techniques (CMM005)
Lecture #7
Graphical User Interface & Event
Handling
The Robert Gordon University
K. Hui 2010-2011
Content
GUI Programming
AWT & Swing
Creating Frames
Drawing in a Frame
Event Handling
The Java Event Delegation Model
Using Interfaces
Using Adapter Classes
The Robert Gordon University
K. Hui 2010-2011
AWT
the Abstract Windowing Toolkit
supplied with JDK 1.1
provides basic functionality to build
GUI
strives to be platform independent
i.e. the same GUI will run on different
platforms without re-design/re-
compilation
The Robert Gordon University
K. Hui 2010-2011
Problems of AWT
implement the common features of native
window systems but not everything
more advanced graphical facilities have to be built
from AWT
heavy-weight components that rely on native
implementation
also inherits the bugs
look-and-feel is different on different platform
The Robert Gordon University
K. Hui 2010-2011
Swing
supplied with JDK 1.2 and above
built on AWT but less-reliant on
underlying window system
use light-weight components
implemented in Java
has the same look-and-feel on different
platforms
has a richer set of UI components
The Robert Gordon University
K. Hui 2010-2011
AWT or Swing?
we will use Swing
but you still need to know about AWT
you need to apply OOP concepts:
classes, objects, relationships
class modelling techniques:
inheritance, interface, etc.
you are familiarising yourself with some
Swing/AWT classes
you need to know how to read API doc
The Robert Gordon University
K. Hui 2010-2011
AWT & Swing Class Hierarchy
AWT & Swing are collections of
related classes
use OO relationship of:
composition
association
inheritance
The Robert Gordon University
K. Hui 2010-2011
Packages Needed
AWT Classes
in java.awt package
Swing Classes
in javax.swing package
main packages/ classes to import
java.awt.*
java.awt.event.*
javax.swing.*
javax.swing.event.*
AWT
stuff
Swing
stuff
The Robert Gordon University
K. Hui 2010-2011
The javax.swing.JFrame
Class
the top-level window
a frame
can contain other graphic components
JFrame has some basic functionalities
usually define your own application-specific
JFrame by inheritance
i.e. extends JFrame
The Robert Gordon University
K. Hui 2010-2011
Super Classes of JFrame
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
all UI components are
subclasses of Component
components
that can
contain/store
other
components
a heavy-
weight
interface to
the
underlying
window
system
a window
with title
& border
The Robert Gordon University
K. Hui 2010-2011
The Simplest GUI Application
contains 2 classes:
MyGuiFrame:a specialised frame
TestMyGuiFrame: uses MyGuiFrame
javax.swing.JFrame
MyGuiFrame
TestMyGuiFrame

main(argv:String[])
The Robert Gordon University
K. Hui 2010-2011
MyGuiFrame &
TestMyGuiFrame
import javax.swing.*;
public class MyGuiFrame extends JFrame
{
} //end class MyGuiFrame

public class TestMyGuiFrame
{
public static void main(String argv[])
{
MyGuiFrame f=new MyGuiFrame();
f.setSize(200,200);
f.setTitle("TestMyGuiFrame App");
f.setVisible(true);
} //end method main
} //end class TestMyGuiFrame
MyGuiFrame.java
TestMyGuiFrame.j
ava
Create frame
object, set
size & title,
then display
frame.
The Robert Gordon University
K. Hui 2010-2011
MyGuiFrame &
TestMyGuiFrame Version 2
import javax.swing.*;
public class MyGuiFrame2 extends JFrame
{
public MyGuiFrame2()
{
this.setSize(200,200);
this.setTitle("TestMyGuiFrame App");
this.setVisible(true);
} //end constructor
} //end class MyGuiFrame2

public class TestMyGuiFrame2
{
public static void main(String argv[])
{
new MyGuiFrame2();
} //end method main
} //end class TestMyGuiFrame2
Having a
constructor.
Fix window size,
set title and
display frame as
soon as frame
object is created.
Just create frame
object.
The Robert Gordon University
K. Hui 2010-2011
MyGuiFrame &
TestMyGuiFrame Version 3
import javax.swing.*;
public class MyGuiFrame3 extends JFrame
{
public MyGuiFrame3(int width,int height,String title)
{
this.setSize(width,height);
this.setTitle(title);
this.setVisible(true);
} //end constructor
} //end class MyGuiFrame3


public class TestMyGuiFrame3
{
public static void main(String argv[])
{
new MyGuiFrame3(200,200,"TestMyGuiFrame App");
} //end method main
} //end class TestMyGuiFrame3
Constructor
allows different
sizes & title. Still
display frame by
default.
Create frame with
given parameter
values.
The Robert Gordon University
K. Hui 2010-2011
Another Design
have "main" defined in the frame class
make the frame class runnable
javax.swing.JFrame
MyGuiApp

main(argv:String[])
The Robert Gordon University
K. Hui 2010-2011
The MyGuiApp Class
import javax.swing.*;
public class MyGuiApp extends JFrame
{
public static void main(String argv[])
{
MyGuiApp f=new MyGuiApp();
f.setSize(200,200);
f.setTitle("My GUI Application");
f.setVisible(true);
} //end main
} //end class MyGuiApp
create an
MyGuiApp object
(i.e. a frame)
inside the main
The Robert Gordon University
K. Hui 2010-2011
Details of MyGuiApp
it is a subclass of JFrame
the constructor is implicit
it has a "main"
the static/class-level method "main" creates an
instance of MyGuiApp
it calls
setSize(int,int) from java.awt.Component
setTitle(string) from java.awt.Frame
setVisible(boolean) from java.awt.Component
The Robert Gordon University
K. Hui 2010-2011
Questions
How do you know which UI component (class)
to use?
How do you know which method to call to do
what?
experience!
look at the API
some IDE have a built-in GUI builder
e.g. NetBeans
*** NOT in your assessment!
The Robert Gordon University
K. Hui 2010-2011
Drawing Graphics inside a
JFrame
override the method
public void paint(Graphics g)
paint() is called whenever a re-painting of
the frame is needed, including:
resize
move
obscured & revealed
the call to paint() is automatically done
by the system, not by you!
The Robert Gordon University
K. Hui 2010-2011
Drawing Text (Graphically) in
a JFrame
import javax.swing.*;
import java.awt.*;
public class HelloWorldFrame extends
JFrame
{
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.BLUE);
g.drawString("Hello World!",75,100);
} //end method paint
// main method here
} //end class HelloWorldFrame
a very important call to
the superclass paint()
method
The Robert Gordon University
K. Hui 2010-2011
Drawing Text (Graphically) in
a JFrame
override public void paint(Graphics g) from
java.awt.Container
need to call super.paint(g)
Why? See the API!
the Graphics object allows you to:
set colour by: setColor()
set font by: setFont()
NB: Graphics is an abstract class in java.awt
The Robert Gordon University
K. Hui 2010-2011
The Story So Far
re-use existing UI components by
inheritance
your new class will inherit functionalities in
the form of attributes & methods
customise UI components behaviour by
overriding methods
the frame cannot close!
it does not handle user interaction
needs to handle "window closing" events
The Robert Gordon University
K. Hui 2010-2011
Java Event Delegation Model
events
something happened
involve 3 kinds of object:
event sources
event listeners
events
source listener
event
The Robert Gordon University
K. Hui 2010-2011
Event Sources
generates events (object)
usually existing GUI components
have methods to register event
listeners to events
an event source notifies all
registered event listeners when an
event occurs
The Robert Gordon University
K. Hui 2010-2011
Event Listeners
objects that implements a listener
interface
so that it qualifies to handle events
respond to events
listen to/catch events
implement all (abstract) methods of
listener interface to handle the events
customise these methods to change
listener objects behaviour
The Robert Gordon University
K. Hui 2010-2011
Different Kinds of Event
action events
java.awt.event.ActionEvent
when a component is activiated (e.g. button)
adjustment events
java.awt.AdjustmentEvent
when a scroll bar is moved
container events
java.awt.event.ContainerEvent
when the content of a container is altered
focus events
java.awt.event.FocusEvent
when a component gains/loses the keyboard focus
The Robert Gordon University
K. Hui 2010-2011
Different Kinds of Event
(cont'd)
window events
java.awt.WindowEvent
when a window is changed
key events
java.awt.KeyEvent
when a key is pressed
mouse events
java.awt.MouseEvent
when the mouse is moved/clicked
all are subclasses of
java.awt.AWTEvent
The Robert Gordon University
K. Hui 2010-2011
To Handle Events in a GUI
1. create event source object
usually a GUI component (e.g. a button)
2. create event listener object
must implement a listener interface
must implement event handling method(s) in the
event listener to handle the events (object)
3. register event listener object as an event
listener of event source object on a type of
event
The Robert Gordon University
K. Hui 2010-2011
The WindowListener
Interface
defined in the java.awt.event package
listen to window events
declare abstract methods:
public void windowActivated(WindowEvent e)
public void windowClosing(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowDeactivated(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowOpened(WindowEvent e)
The Robert Gordon University
K. Hui 2010-2011
Class Hierarchy of the
CloseableHelloWorld Frame
javax.swing.JFrame
CloseableHelloWorld

main(argv:String[])
windowActivated(e:WindowEvent)
windowClosing(e:WindowEvent)
windowClosed(e:WindowEvent)

<<interface>>
java.awt.event.WindowListener
The Robert Gordon University
K. Hui 2010-2011
A Closeable Frame
implement the WindowListener
interface to handle window events
// import statements
public class
CloseableHelloWorld extends
JFrame implements
WindowListener
{
//methods here
}
make sure you
know what to
import
The Robert Gordon University
K. Hui 2010-2011
The Constructor
when a CloseableHelloWorld (also
a JFrame) object is created,
addWindowListener(this)
registers the object itself as its own
window event listener
public CloseableHelloWorld()
{
this.addWindowListener(this);
}
The Robert Gordon University
K. Hui 2010-2011
Handling Window Events
implement the abstract method in the
WindowListener interface
public void
windowClosing(WindowEvent e)
{
System.exit(0);
}
also need to implement other abstract
methods in the interface
with empty body
The Robert Gordon University
K. Hui 2010-2011
Adapter Classes
instead of having an object as its
own event listener, it can have a
dedicate object to handle the
events
extend the adapter classes
java.awt.event.WindowAdapter
The Robert Gordon University
K. Hui 2010-2011
Class Hierarchy
javax.swing.JFrame
CloseableHelloWorld2

main(argv:String[])
java.awt.event.WindowAdapter
HelloWorldCloser

windowClosing(e:WindowEvent)
The Robert Gordon University
K. Hui 2010-2011
The HelloWorldCloser
Class
//import statements
public class HelloWorldCloser
extends WindowAdapter
{
public void
windowClosing(WindowEvent e)
{
System.exit(0);
} //end method windowClosing
} //end class HelloWorldCloser
exit program
when this method
is invoked
event handler
for window-
closing event
The Robert Gordon University
K. Hui 2010-2011
CloseableHelloWorld
Version 2
//import statements
public class CloseableHelloWorld2
extends javax.swing.JFrame
{
public CloseableHelloWorld2()
{
this.addWindowListener(new
HelloWorldCloser());
}
//main, etc.
} //end class HellowWorldApp
Create event listener
object & register it as
the event handler of this
event source object.
In constructor
of frame,
create and
register event
handler object
The Robert Gordon University
K. Hui 2010-2011
Interface vs Adapter Classes
adapter class advantage:
empty methods already defined in the
superclass (WindowAdapter)
only need to override methods you
want
don't have to implement all methods
(as in an interface)
The Robert Gordon University
K. Hui 2010-2011
What now?
add UI components to the frame
e.g. buttons, menu items, etc.
How to respond when button is
pressed?
components (e.g. buttons) generate
events
implement the appropriate event
handlers
The Robert Gordon University
K. Hui 2010-2011
Summary
develop GUI by extending JFrame
handling events involves
the event source object
the event object
the event listener/handler object
implement event handler by:
implement interfaces
extending adapter classes

Vous aimerez peut-être aussi