Lab Manual 15 - GUI-2
Lab Manual 15 - GUI-2
MANUAL
Course: CSC241-Object Oriented Programming
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)
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:
Instructor Note:
The student should have understanding related to basic GUI components and Layouts.
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.
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.*;
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);
label.setText("Bye");
f.setSize(400, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);;
f.setVisible(true);
Import java.awt.FlowLayout;
Import java.awt.event.ActionEvent;
Import java.awt.event.ActionListener;
Import javax.swing.*;
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);
String s=tf1.getText();
label.setText(s);
tf1.setText("");
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.*;
Private JButton b;
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
tf1=new JTextField(8);
this.add(tf1);
this.add(label);
b=new JButton("Change");
b.addActionListener(new myHandler());
add(b);
b1=new JButton("Disappear");
b1.addActionListener(new myHandler());
add(b1);
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) {
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:
Assignment 2:
Make a functional non scientific calculator. Recall the last task of the previous lab .