Fundamentals of Software Development CT010-3-1 Event Handling
Fundamentals of Software Development CT010-3-1 Event Handling
Development
CT010-3-1
Event Handling
Prepared by: GTK First Prepared on:27 th July 2005 Last Modified on:19th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation
Sample programs
Event Handling
Learning Outcomes
At the end of this module, YOU should be able to:
Understand the differences between a frame
and an applet.
Explain the structure of an event-handling
programme.
Create, edit, compile, run and debug simple
event-driven Java programs implementing GUI
components and layout managers
incorporating event-handling techniques.
CT010-3-1 Fundamentals of Software Design
Event Handling
Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
Frames
Dialog
Panel
ActionListener
Event Handling
Event Handling
import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args) {
MyFrame me = new MyFrame();
}
public MyFrame() {
setSize(150,150);
setLocation(100,100);
setTitle("My First java.awt.Frame!");
setLayout(new FlowLayout());
setVisible(true);
}
}
CT010-3-1 Fundamentals of Software Design
Event Handling
Event Handling
Event Handling
Event Handling
Event Handling
Event Handling
Event Handling
Panels
The Panel object is located in the following API
hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--java.awt.Panel
Event Handling
Panels
North Panel
(textPanel)
South Panel
(buttonPanel)
Event Handling
Panels
The code that
generate the
previous GUI :
setLayout(new BorderLayout());
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.blue);
buttonPanel.setLayout(new FlowLayout());
Button memo1Button = new Button("Save Memo 1");
buttonPanel.add(memo1Button);
Button memo2Button = new Button("Save Memo 2");
buttonPanel.add(memo2Button);
Button clearButton = new Button("Clear");
buttonPanel.add(clearButton);
Button getMemo1Button = new Button("Get Memo 1");
buttonPanel.add(getMemo1Button);
Button getMemo2Button = new Button("Get Memo 2");
buttonPanel.add(getMemo2Button);
add(buttonPanel,"South");
Panel textPanel = new Panel();
theText = new TextArea(10,40);
textPanel.add(theText);
add(textPanel, "North");
Event Handling
Event-Handling Application
GUIs are event-driven (generate events when user
interacts with the GUI components (event source))
Common interactions include moving the mouse,
clicking the mouse, clicking a button, typing in a
textfield, selecting an item from a menu etc.
event-driven program is a program that runs the event
loop that waits for input and then responds accordingly
event-driven programming is a technique used in
software that operates a program in response to events
as they occur
CT010-3-1 Fundamentals of Software Design
Event Handling
Event-Handling Application
The basic model for a GUI program requires the following:
1. Declaring application as event listener (action listener)
(e.g. implements ActionListener)
2. Register the reactive components
(e.g. tFahr.addActionListener (this))
3. Define the action to be performed (methods)
(e.g. action Performed(ActionEvent e))
2 key tasks to process a GUI event:
Register an event listener for the GUI component that is
expected to generate the event
Implement an event-handling method (event handlers)
CT010-3-1 Fundamentals of Software Design
Event Handling
Event-Handling Application
Event Class
ActionEvent
WindowEvent
AdjustmentEvent
ItemEvent
e.g. import java.awt.ActionEvent
Common types of event listener
ActionListener (clicking, selecting items)
WindowListener (closing, opening windows)
AdjustmentListener (scrollbars)
ItemListener (selecting checkboxes)
e.g. implements ActionListener
CT010-3-1 Fundamentals of Software Design
Event Handling
Event-Handling Application
1. Import the java.awt.event package
Event Handling
Event-Handling Application
4. Define the action to be performed when a reactive
component is activated
eg.
public void actionPerformed (ActionEvent e) {
if (e.getSource() == tFahr) {
} else {
}
}
Event Handling
textPanel
buttonPanel
CT010-3-1 Fundamentals of Software Design
Event Handling
Event Handling
Event Handling
Event Handling
Event Handling
Example: Temperature
Application
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ConvertTemp extends Frame implements ActionListener {
// Declare and allocate Components
TextField
tFahr = new TextField(9),
tCent = new TextField(9);
private Panel textPanel;
private TextArea theText;
private String memo1 = "No Memo 1";
rivate String memo2 = "No Memo 2";
public static void main(String[] args){
ConvertTemp me = new ConvertTemp();
}
Event Handling
Example: Temperature
Application
public ConvertTemp() {
setSize(250,300);
setLocation(100,100);
setTitle("Converting Temperature");
setLayout(new FlowLayout());
addWindowListener(new WindowDestroyer());
// Arrange Component layout
add(new Label("Fahrenheit"));
add(tFahr);
add(new Label("Centigrade"));
add(tCent);
// Register Component Listeners
tFahr.addActionListener(this);
tCent.addActionListener(this);
setVisible(true);
}
CT010-3-1 Fundamentals of Software Design
Event Handling
Example: Temperature
Application
public void actionPerformed (ActionEvent e) {
// Respond to Action Events:
// 1. tFahr TextField
// 2. tCent TextField
double fahr, cent;
if (e.getSource()==tFahr) {
fahr = new Double(tFahr.getText()).doubleValue();
cent = 5.0 * (fahr - 32.0) / 9.0;
tCent.setText(cent + "");
}
else {
cent = new Double(tCent.getText()).doubleValue();
fahr = 9.0 * cent / 5.0 + 32.0;
tFahr.setText(fahr + "");
}
}
}
CT010-3-1 Fundamentals of Software Design
Event Handling
Event Handling
Sample programs
Event Handling
Next Lesson
Object-Oriented Programming
Introduction to classes and objects
Defining a class
Adding variables and methods to a class
Defining an object
Visibility control
public, private, protected, default
Static/Instance members
CT010-3-1 Fundamentals of Software Design
Event Handling