[go: up one dir, main page]

0% found this document useful (0 votes)
6 views6 pages

Week 2 Handout 1

Event-driven programming is a paradigm where program flow is determined by events like user interactions, with key concepts including event sources, listeners, and handlers. Java is highlighted for its platform independence, rich libraries, and ease of use in GUI applications, utilizing components and containers for layout management. The document also covers event handling in Java, providing examples of implementing event listeners and handling button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Week 2 Handout 1

Event-driven programming is a paradigm where program flow is determined by events like user interactions, with key concepts including event sources, listeners, and handlers. Java is highlighted for its platform independence, rich libraries, and ease of use in GUI applications, utilizing components and containers for layout management. The document also covers event handling in Java, providing examples of implementing event listeners and handling button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Event-Driven Programming Concepts

What is Event-Driven Programming?


Event-driven programming is a paradigm where the program's flow is determined by events
such as user interactions, sensor outputs, or messages from other programs. Events trigger
specific actions, enabling applications to respond dynamically to user inputs.
Key Concepts:
Event:
An action or occurrence, such as a button click, mouse movement, or key press.
Event Source:
The component that generates the event (e.g., a button, text field, or window).
Event Listener:
An interface that listens for events and defines the actions to perform when an event occurs.
Event Handler:
The method that executes in response to an event.

Example of Event-Driven Programming:


A simple Java program that changes the label of a button when clicked:

import java.awt.*;
import java.awt.event.*;

public class EventExample {


public static void main(String[] args) {
Frame frame = new Frame("Event-Driven Example");
Button button = new Button("Click Me");

// Set button size and position


button.setBounds(100, 100, 80, 30);

// Add event listener to the button


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked!");
}
});

frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}

Introduction to Java and Its Importance in GUI Applications


Why Use Java for GUI Applications?
Platform Independence:
Java’s Write Once, Run Anywhere (WORA) capability ensures that GUI applications can run on
any platform with a Java Virtual Machine (JVM).
Rich Libraries:
Java provides built-in libraries such as AWT, Swing, and JavaFX for creating robust GUI
applications.
Multithreading Support:
Java’s multithreading capabilities make it suitable for interactive applications.
Ease of Use:
Java simplifies GUI development with intuitive libraries and reusable components.

Components and Containers


Components:
Components are the individual elements of a GUI, such as buttons, text fields, and labels.

Common AWT Components:


Button: A clickable button.
Label: Displays text.
TextField: Allows user input.
Checkbox: Represents a toggle option.
Containers:
Containers hold and organize components. They can be nested to create complex layouts.

Types of Containers:
Top-Level Containers:
Frame: A window with a title bar and border.
Dialog: A pop-up window for user interaction.
Intermediate Containers:
Panel: A generic container used for organizing components.

Example: Components and Containers


A basic program with a Frame containing a Button and a TextField:

import java.awt.*;

public class ComponentsExample {


public static void main(String[] args) {
Frame frame = new Frame("AWT Components Example");

// Create components
Button button = new Button("Submit");
TextField textField = new TextField("Enter text here");
Label label = new Label("This is a label");

// Set positions
label.setBounds(50, 50, 150, 20);
textField.setBounds(50, 100, 150, 20);
button.setBounds(50, 150, 80, 30);

// Add components to frame


frame.add(label);
frame.add(textField);
frame.add(button);

// Frame settings
frame.setSize(300, 300);
frame.setLayout(null);
frame.setVisible(true);
}
}
Layout Managers
Layout managers control the arrangement of components within a container.

Types of Layout Managers:


FlowLayout:
Arranges components in a row, wrapping to the next row if necessary.
Default layout for Panel.
BorderLayout:
Divides the container into five regions: North, South, East, West, and Center.
Default layout for Frame.
GridLayout:
Arranges components in a grid of rows and columns.
CardLayout:
Allows multiple components to share the same display space.

Example: Using Layout Managers


A program that demonstrates FlowLayout:

import java.awt.*;

public class LayoutExample {


public static void main(String[] args) {
Frame frame = new Frame("FlowLayout Example");

// Set layout
frame.setLayout(new FlowLayout());

// Add components
frame.add(new Button("Button 1"));
frame.add(new Button("Button 2"));
frame.add(new Button("Button 3"));

frame.setSize(300, 200);
frame.setVisible(true);
}
}

Introduction to Event Handling


What is Event Handling?
Event handling is the process of responding to events such as button clicks, mouse movements,
or key presses. Java provides a robust mechanism for handling events through the delegation
event model.

Key Components of Event Handling:


Event Source:
The component that generates the event.

Event Listener:
An object that listens for events.

Event Object:
Contains information about the event.

Steps for Event Handling:


Implement an event listener interface (e.g., ActionListener, MouseListener).
Register the listener with the event source using methods like addActionListener.
Example: Event Handling in AWT
A program that handles a button click event:

import java.awt.*;
import java.awt.event.*;

public class EventHandlingExample {


public static void main(String[] args) {
Frame frame = new Frame("Event Handling Example");
Button button = new Button("Click Me");

// Set button position


button.setBounds(100, 100, 80, 30);

// Add event listener to the button


button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}

Lab Activity 3
Create an AWT application with:
A TextField for user input.
Two buttons labeled "Show" and "Clear."
When "Show" is clicked, display the text in the console.
When "Clear" is clicked, clear the TextField.

You might also like