Lecture 09 GUI Part I
Lecture 09 GUI Part I
Lecture 09 GUI Part I
Chapter 7
Graphical User Interface(GUI) and
Event Driven Programming
By: Mequanent Argaw
Debre Markos University
unfriendly.
Look like the native GUI components of the platform on which a java program executes.
Heavyweight components – they rely on the local platform’s OS to determine their functionality and
Most GUI components are Lightweight components- they are written, manipulated and displayed
completely in Java.
JDialog
Container
JComponent (Swing)
JButton JColorChooser JFileChooser JPanel
Constructors:
public JFrame()
Call setVisible(true) to make a frame appear on the screen after creating it.
if this is not set, the program will never exit even if the frame is closed.
import java.awt.*;
}
12/31/2015 Department of ECE, College of Technology 11
Containers and Layout
Place components in a container; add the container to a frame.
container: An object that stores components and governs their positions,
sizes, and resizing behavior.
the container, possibly giving extra information about where to place it.
public FlowLayout()
myFrame.setLayout(new FlowLayout());
myFrame.setLayout(new BorderLayout());
BorderLayout.NORTH);
Feel refers to the interaction between the GUI and the USER.
How does one interact with GUI ?
The question is “ How to get the button to do something specific when a user clicks it? ”
click).
A way to know when to trigger that method. In other words a way to know when the user clicks
the button!
In Java, the process of getting and handling a user event is called event-handling.
Interface.
An event source is an object that can turn user actions into objects called
An event source creates an event object when the user performs some
action on it.
You will act as a listener receiving events rather than creating events.
12/31/2015 Department of ECE, College of Technology 21
Listener Interface…
Every event type has a matching listener interface.
To tell the button you are interested, you register with the button by calling its
addActionListener(this) method and since you are the ActionListener you pass
The button needs a way to call you back when the event happens:
So it calls the method in the ActionListener interface.
actionPerformed().
12/31/2015 Department of ECE, College of Technology 23
Getting a button’s Action Event
1. Implement the ActionListener Interface.
When a user clicks the button, the button ‘fires’ the event by calling
The listener will be notified when the event occurs (e.g. button click).
The overall flow of what code is executed is determined by the series of events
EventObject EventListener
AWTEvent (AWT) AWTEventListener
ActionEvent ActionListener
TextEvent TextListener
ComponentEvent ComponentListener
FocusEvent FocusListener
WindowEvent WindowListener
KeyEvent KeyListener
MouseListener
MouseEvent
Attaches the given listener to be notified of clicks and events that occur on
this component.
12/31/2015 Department of ECE, College of Technology 31
Exercise
Write a simple calculator Graphical Interface using GUI
Programming.