Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Basics and Event- Driven Programming Feb 7, 2000.

Similar presentations


Presentation on theme: "GUI Basics and Event- Driven Programming Feb 7, 2000."— Presentation transcript:

1 GUI Basics and Event- Driven Programming Feb 7, 2000

2 What are provided by GUI library? zCreation of user interface components (often called controls) zGiving a specific behaviour to the controls by coupling with GUI events. zGrouping and arranging(layout) the controls on the screen. zGraphics operation Ex: Polygon drawing/filling, Clipping, etc.

3 Procedural versus Graphical programming zGraphical Programs are WAY different from procedural programs. zA procedural program is a sequential flow of control. We DO know the start and end. zWindowing(graphical) programs are unpredictable(asynchronous). Who knows when a button will be clicked? zTherefore we use event-driven programming.

4 Event-Driven Programming zIn event-driven programming, we need a outermost loop which is constantly waiting for user input. (Indefinite loop) zWhen 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. zIn other words, it’s your job to provide such a event handler

5 Java Event Model zJava event model is primarily for GUI programming. zGenerally it is used to connect your code to any kind of asynchronous events. zTo receive a specific event, a Java class should tell the window system(register) its interest in the event. zIn other words, we connect the controls(event source) by registering a callback with your event-handling class.

6 Delegation Model Event source object Event source object Event source object Event source object Event source object Event source object Listener object Listener object Listener object Listener object Listener object Listener object Register handler Fire events

7 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); } 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!!! minimize maximize close

8 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) {} }

9 Basic framework for event handling in Java zWrite a class that implements the interface associated with a certain event. Generally this takes the form of SomethingListener eg) WindowListener, MouseListener zCreata an object of the type(class) above. zCall registering method provided by underlying component. Generally this takes the form of addSomethingListern(listener) Note: listener is SomethingListener type. zShortcut: Inner class, Listener Adapter class


Download ppt "GUI Basics and Event- Driven Programming Feb 7, 2000."

Similar presentations


Ads by Google