Download GUI Basics and Event-Driven Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Name mangling wikipedia , lookup

Java performance wikipedia , lookup

Structured programming wikipedia , lookup

Class (computer programming) wikipedia , lookup

Design Patterns wikipedia , lookup

Application Interface Specification wikipedia , lookup

Aqua (user interface) wikipedia , lookup

C Sharp syntax wikipedia , lookup

C++ wikipedia , lookup

Object-oriented programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
GUI Basics and EventDriven Programming
Feb 7, 2000
What are provided by GUI
library?
Creation of user interface components (often
called controls)
Giving a specific behaviour to the controls by
coupling with GUI events.
Grouping and arranging(layout) the controls on
the screen.
Graphics operation
Ex: Polygon drawing/filling, Clipping, etc.
Procedural versus
Graphical programming
Graphical Programs are WAY different from
procedural programs.
A procedural program is a sequential flow of
control. We DO know the start and end.
Windowing(graphical) programs are
unpredictable(asynchronous).
Who knows when a button will be clicked?
Therefore we use event-driven programming.
Event-Driven Programming
In event-driven programming, we need a
outermost loop which is constantly waiting for
user input. (Indefinite loop)
When an user input is occurred(eg. Mouse
click), the window manager creates an event
and passes it onto an event handler that is
provided by programmer. This is known as
callback.
In other words, it’s your job to provide such a
event handler
Java Event Model
Java event model is primarily for GUI
programming.
Generally it is used to connect your code to any
kind of asynchronous events.
To receive a specific event, a Java class should
tell the window system(register) its interest in
the event.
In other words, we connect the controls(event
source) by registering a callback with your
event-handling class.
Delegation Model
Event source
object
Listener
object
Event source
object
Listener
object
Event source
object
Listener
object
Register handler
Fire events
import javax.swing.*;
public class FrameDemo {
public static void main(String args[]) {
JFrame frame =
new JFrame(“Hoony Wrote this”);
frame.setSize(400,100);
frame.setVisible(true);
}
}
minimize
maximize
close
• JFrame is capable of receiving events from Windows. In other words
it is a Window. All three buttons are working as usual.
• However even after clicking close button, FrameDemo.java is not
killed. (Becomes ZOMBIE!!!)
• Need to register handler!!!
import javax.swing.*;
public class FrameDemo {
public static void main(String args[]) {
JFrame frame =
new JFrame(“Hoony Wrote this”);
frame.setSize(400,100);
frame.setVisible(true);
MyHandler hand = new MyHandler();
frame.addWindowListener(hand);
}
}
class MyHandler implements WindowListener {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(Window Event e){}
public void windowDeIconified(Window Event e){}
public void windowActivated(Window Event e) {}
public void windowDeactivated(Window Event e) {}
}
Basic framework for event
handling in Java
 Write a class that implements the interface associated
with a certain event. Generally this takes the form of
SomethingListener eg) WindowListener, MouseListener
 Creata an object of the type(class) above.
 Call registering method provided by underlying
component. Generally this takes the form of
addSomethingListern(listener)
Note: listener is SomethingListener type.
 Shortcut: Inner class, Listener Adapter class