0% found this document useful (0 votes)
79 views13 pages

Lab Manual 15 - GUI-2

This lab manual discusses event-based programming in Java GUI interfaces, with students learning to understand event generation and handling, and completing activities to create simple GUI programs that respond to button clicks and text changes using events and listeners. The manual provides code examples for students to run and understand how events update a label's text based on button presses or text field content.

Uploaded by

saif habib
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)
79 views13 pages

Lab Manual 15 - GUI-2

This lab manual discusses event-based programming in Java GUI interfaces, with students learning to understand event generation and handling, and completing activities to create simple GUI programs that respond to button clicks and text changes using events and listeners. The manual provides code examples for students to run and understand how events update a label's text based on button presses or text field content.

Uploaded by

saif habib
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/ 13

LAB

MANUAL
Course: CSC241-Object Oriented Programming

Department of Computer Science

Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
3) Stage v (Verify the accuracy)
4) Stage a2 (Assess your work)

CCS241 –Lab Manual 1


COMSATS Institute of Information Technology (CIIT)
Islamabad
-

CCS241 –Lab Manual 2


LAB # 15

Statement Purpose:
In this lab, student will learn and practice the basic concepts of events based programming
in GUI based interfaces in Java. They will learn event generation and event handling.

Activity Outcomes:
After completing this lesson, you should be able to do the following:

• Understand why events are needed in GUI

• Understand the mechanics of event generation and event handling

• Practice simple event based programming

• Create a simple but useful GUI based program

Instructor Note:
The student should have understanding related to basic GUI components and Layouts.

CCS241 –Lab Manual 3


1) Stage J (Journey)
Introduction
Any program that uses GUI (graphical user interface) such as Java application written for
windows, is event driven. Event describes the change of state of any object.

Example :Pressing a button, Entering a character in Textbox

Event handling has three main components,

 Events : An event is a change of state of an object.


 Events Source : Event source is an object that generates an event.
 Listeners : A listener is an object that listens to the event. A listener gets notified when
an event occurs.

A source generates an Event and sends it to one or more listeners registered with the source.
Once event is received by the listener, they processe the event and then return. Events are
supported by a number of Java packages, like java.util, java.awt and java.awt.event.

Important Event Classes and Interface

Event Classes Description Listener Interface

ActionEvent generated when button is pressed, menu-item is ActionListener


selected, list-item is double clicked

MouseEvent generated when mouse is dragged, MouseListener


moved,clicked,pressed or released also when the
enters or exit a component

KeyEvent generated when input is received from keyboard KeyListener

ItemEvent generated when check-box or list item is clicked ItemListener


TextEvent generate when value of textarea or textfield is TextListener
d
changed

MouseWheelEvent generated when mouse wheel is moved MouseWheelListener

WindowEvent generated when window is activated, deactivated, WindowListener


deiconified, iconified, opened or closed

ComponentEvent generated when component is hidden, moved, resized ComponentEventListener


or set visible

ContainerEvent generated when component is added or removed ContainerListener


from container

AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener

FocusEvent generated when component gains or loses keyboard FocusListener


focus

2) Stage a1 (apply)
Lab Activities:
Activity 1:
Run the below code. It should create a label and a button. The label should have text “Hello”
but when the button the pressed the text changes to “Bye”

Import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class test extends JFrame {

privateJLabel label;

privateJButton b;
public test(){

this.setLayout(newFlowLayout(FlowLayout.LEFT,10,20));

label=newJLabel("Hello");

this.add(label);

b=newJButton("Toggle");

b.addActionListener(new myHandler());

add(b);

Class myHandler implements ActionListener{

Public void actionPerformed(ActionEvent e){

label.setText("Bye");

Public static void main(String[] args) {

// TODO Auto-generated method stub

test f=new test();

f.setTitle("Hi and Bye");

f.setSize(400, 150);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);;

f.setVisible(true);

CCS241 –Lab Manual 110


CCS241 –Lab Manual 111
Activity 2:
Run and understand the below code. Basically this code sets the text in label on button press
event. Any text entered in the textfield is copied into the label. Ensure that it is so and
understand how it works.

Import java.awt.FlowLayout;

Import java.awt.event.ActionEvent;

Import java.awt.event.ActionListener;

Import javax.swing.*;

Public class test extendsJFrame {

Private JTextField tf1;

Private JLabel label;

Private JButton b;

public test(){

this.setLayout(newFlowLayout(FlowLayout.LEFT,10,20));

tf1=newJTextField(8);

this.add(tf1);

label=newJLabel("New Text");

this.add(label);

b=newJButton("Change");

b.addActionListener(new myHandler());

add(b);

Class myHandler implements ActionListener{

Public void actionPerformed(ActionEvent e){

String s=tf1.getText();

label.setText(s);
tf1.setText("");

Public static void main(String[] args) {

// TODO Auto-generated method stub

test f=new test();

f.setTitle("Copy Text");

f.setSize(400, 150);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);;

f.setVisible(true);

Activity 3:
Run and understand the below code. We now first see which button triggered the event
through the getSource event and then either disappear one button or copy text in the
TextField into the label.

Import java.awt.FlowLayout;

Import java.awt.event.ActionEvent;

Import java.awt.event.ActionListener;

Import javax.swing.*;

Public class test extends JFrame {

Private JTextField tf1;

Private JLabel label;

Private JButton b;

Private JButton b1;


public test(){

this.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));

tf1=new JTextField(8);

this.add(tf1);

label=new JLabel("New Text");

this.add(label);

b=new JButton("Change");

b.addActionListener(new myHandler());

add(b);

b1=new JButton("Disappear");

b1.addActionListener(new myHandler());

add(b1);

Class myHandler implements ActionListener{

Public void actionPerformed(ActionEvent e){

if(e.getSource()==b)

String s=tf1.getText();

label.setText(s);

tf1.setText("");

if(e.getSource()==b1)

b.setVisible(false);

}
Public static void main(String[] args) {

// TODO Auto-generated method stub

test f=new test();

f.setTitle("Copy Text");

f.setSize(400, 150);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);;

f.setVisible(true);

3) Stage v (verify)
Home Activities:
Activity 1:

Create a frame with one label, one textbox and a button. Display the information entered in
textbox on button click.

Activity 2:

Create frames as follows:


4) Stage a2 (assess)
Assignment 1:
Create Frames as follows:

Assignment 2:
Make a functional non scientific calculator. Recall the last task of the previous lab .

You might also like