Lecture-07-GUI and Event Handling
Transcript of Lecture-07-GUI and Event Handling
-
8/12/2019 Lecture-07-GUI and Event Handling
1/40
-
8/12/2019 Lecture-07-GUI and Event Handling
2/40
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
-
8/12/2019 Lecture-07-GUI and Event Handling
3/40
-
8/12/2019 Lecture-07-GUI and Event Handling
4/40
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 builtfrom AWT
heavy-weight components that rely on nativeimplementation
also inherits the bugs
look-and-feel is different on different platform
-
8/12/2019 Lecture-07-GUI and Event Handling
5/40
-
8/12/2019 Lecture-07-GUI and Event Handling
6/40
-
8/12/2019 Lecture-07-GUI and Event Handling
7/40
The Robert Gordon University K. Hui 2010-2011
AWT & Swing Class HierarchyAWT & Swing are collections of
related classes
use OO relationship of:
composition
association inheritance
-
8/12/2019 Lecture-07-GUI and Event Handling
8/40
The Robert Gordon University K. Hui 2010-2011
Packages Needed AWT Classes
in java.awtpackage
Swing Classes in javax.swingpackage
main packages/ classes to import java.awt.*
java.awt.event.* javax.swing.*
javax.swing.event.*
AWT
stuff
Swingstuff
-
8/12/2019 Lecture-07-GUI and Event Handling
9/40
The Robert Gordon University K. Hui 2010-2011
The javax.swing.JFrame
Class the top-level window
a frame
can contain other graphic components
JFramehas some basic functionalities
usually define your own application-specificJFrameby inheritance
i.e. extends JFrame
-
8/12/2019 Lecture-07-GUI and Event Handling
10/40
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 aresubclasses of Component
components
that cancontain/storeother
components
a heavy-weightinterface to
theunderlying
windowsystem
a windowwith title& border
-
8/12/2019 Lecture-07-GUI and Event Handling
11/40
The Robert Gordon University K. Hui 2010-2011
The Simplest GUI Application contains 2 classes:
MyGuiFrame:a specialised frame
TestMyGuiFrame: usesMyGuiFrame
javax.swing.JFrame
MyGuiFrame
TestMyGuiFrame
main(argv:String[])
-
8/12/2019 Lecture-07-GUI and Event Handling
12/40
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.java
Create frameobject, setsize & title,then display
frame.
-
8/12/2019 Lecture-07-GUI and Event Handling
13/40
The Robert Gordon University K. Hui 2010-2011
MyGuiFrame &
TestMyGuiFrame Version 2import 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 aconstructor.
Fix window size,
set title anddisplay frame assoon as frame
object is created.
Just create frameobject.
-
8/12/2019 Lecture-07-GUI and Event Handling
14/40
The Robert Gordon University K. Hui 2010-2011
MyGuiFrame &
TestMyGuiFrame Version 3import 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
Constructorallows differentsizes & title. Stilldisplay frame by
default.
Create frame withgiven parameter
values.
-
8/12/2019 Lecture-07-GUI and Event Handling
15/40
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[])
-
8/12/2019 Lecture-07-GUI and Event Handling
16/40
-
8/12/2019 Lecture-07-GUI and Event Handling
17/40
The Robert Gordon University K. Hui 2010-2011
Details ofMyGuiApp
it is a subclass of JFrame
the constructor is implicit
it has a "main" the static/class-level method "main" creates an
instance ofMyGuiApp
it calls setSize(int,int)from java.awt.Component
setTitle(string)from java.awt.Frame
setVisible(boolean)from java.awt.Component
-
8/12/2019 Lecture-07-GUI and Event Handling
18/40
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 dowhat?
experience!
look at the API
some IDE have a built-in GUI builder
e.g. NetBeans
*** NOT in your assessment!
-
8/12/2019 Lecture-07-GUI and Event Handling
19/40
The Robert Gordon University K. Hui 2010-2011
Drawing Graphics inside aJFrame
override the methodpublic void paint(Graphics g)
paint()is called whenever a re-painting ofthe frame is needed, including:
resize
move
obscured & revealed
the call topaint()is automatically done
by the system, not by you!
-
8/12/2019 Lecture-07-GUI and Event Handling
20/40
The Robert Gordon University K. Hui 2010-2011
Drawing Text (Graphically) ina JFrameimport javax.swing.*;
import java.awt.*;
public class HelloWorldFrame extendsJFrame
{
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 importantcall tothe superclass paint()
method
-
8/12/2019 Lecture-07-GUI and Event Handling
21/40
The Robert Gordon University K. Hui 2010-2011
Drawing Text (Graphically) ina JFrame
overridepublic void paint(Graphics g)fromjava.awt.Container
need to call super.paint(g) Why? See the API!
the Graphicsobject allows you to:
set colour by: setColor()
set font by: setFont()
NB: Graphicsis an abstract class in java.awt
-
8/12/2019 Lecture-07-GUI and Event Handling
22/40
-
8/12/2019 Lecture-07-GUI and Event Handling
23/40
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
-
8/12/2019 Lecture-07-GUI and Event Handling
24/40
The Robert Gordon University K. Hui 2010-2011
Event Sources
generates events(object)
usually existing GUI components
have methods to register eventlistenersto events
an event sourcenotifies allregistered event listenerswhen aneventoccurs
-
8/12/2019 Lecture-07-GUI and Event Handling
25/40
The Robert Gordon University K. Hui 2010-2011
Event Listeners
objects that implements a listenerinterface
so that it qualifies to handle events respond to events
listen to/catch events
implement all (abstract) methods oflistener interface to handle the events customise these methods to change
listener objects behaviour
-
8/12/2019 Lecture-07-GUI and Event Handling
26/40
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
-
8/12/2019 Lecture-07-GUI and Event Handling
27/40
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
-
8/12/2019 Lecture-07-GUI and Event Handling
28/40
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 theevent listenerto handle the events(object)
3. register event listener objectas an eventlistener of event source objecton a type ofevent
-
8/12/2019 Lecture-07-GUI and Event Handling
29/40
The Robert Gordon University K. Hui 2010-2011
TheWindowListener
Interface
defined in the java.awt.eventpackage
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)
-
8/12/2019 Lecture-07-GUI and Event Handling
30/40
The Robert Gordon University K. Hui 2010-2011
Class Hierarchy of theCloseableHelloWorldFrame
javax.swing.JFrame
CloseableHelloWorld
main(argv:String[])
windowActivated(e:WindowEvent)windowClosing(e:WindowEvent)windowClosed(e:WindowEvent)
java.awt.event.WindowListener
-
8/12/2019 Lecture-07-GUI and Event Handling
31/40
The Robert Gordon University K. Hui 2010-2011
A Closeable Frame
implement theWindowListenerinterface to handle window events
// import statementspublic class
CloseableHelloWorld extendsJFrame implements
WindowListener{
//methods here
}
make sure youknow what toimport
-
8/12/2019 Lecture-07-GUI and Event Handling
32/40
The Robert Gordon University K. Hui 2010-2011
The Constructor
when a CloseableHelloWorld (alsoa JFrame) object is created,
addWindowListener(this)registers the object itself as its ownwindow event listener
public CloseableHelloWorld()
{
this.addWindowListener(this);
}
-
8/12/2019 Lecture-07-GUI and Event Handling
33/40
-
8/12/2019 Lecture-07-GUI and Event Handling
34/40
The Robert Gordon University K. Hui 2010-2011
Adapter Classes
instead of having an object as itsown event listener, it can have a
dedicate object to handle theevents
extend the adapter classes java.awt.event.WindowAdapter
-
8/12/2019 Lecture-07-GUI and Event Handling
35/40
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)
-
8/12/2019 Lecture-07-GUI and Event Handling
36/40
The Robert Gordon University K. Hui 2010-2011
The HelloWorldCloser
Class
//import statements
public class HelloWorldCloserextends WindowAdapter
{public voidwindowClosing(WindowEvent e)
{
System.exit(0);
} //end method windowClosing
} //end class HelloWorldCloser
exit program
when this methodis invoked
event handler
for window-closing event
-
8/12/2019 Lecture-07-GUI and Event Handling
37/40
The Robert Gordon University K. Hui 2010-2011
CloseableHelloWorld
Version 2
//import statements
public class CloseableHelloWorld2extends javax.swing.JFrame
{public CloseableHelloWorld2()
{
this.addWindowListener(newHelloWorldCloser());
}
//main, etc.
} //end class HellowWorldApp
Create event listenerobject & register it as
the event handler of thisevent source object.
In constructor
of frame,create and
register eventhandler object
-
8/12/2019 Lecture-07-GUI and Event Handling
38/40
The Robert Gordon University K. Hui 2010-2011
Interface vs Adapter Classes
adapter class advantage:
empty methods already defined in thesuperclass (WindowAdapter)
only need to override methods youwant
don't have to implement all methods(as in an interface)
-
8/12/2019 Lecture-07-GUI and Event Handling
39/40
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 ispressed?
components (e.g. buttons) generateevents
implement the appropriate event
handlers
-
8/12/2019 Lecture-07-GUI and Event Handling
40/40
K H i 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