Event Driven Programming
EXNO:
DATE:
AIM:
To execute the given event driven programming program using net beans
13. a. Write a Java program to toggle the background color on every click of button
ALGORITHM:
Step1: Start
Step2: Initialize the variables
Step3: create three button say red, green and yellow.
Step4: check the condition for every click on applet and changes happens in the applet
Step5: display the result using applet
Step6: Stop
PROGRAM:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorFrame {
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel();
RAAM SEDHU RR 18CSE111
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
class Listener extends JPanel implements ActionListener{
public void actionPerformed(ActionEvent event) {
Color color;
if (event.getSource() == redButton){
color = Color.red;
redButton.setBackground(color);
panel.setBackground(color);//To set panel background instead of frames background
} else if (event.getSource() == greenButton){
color = Color.green;
greenButton.setBackground(color);
panel.setBackground(color);
} else {
color = Color.blue;
blueButton.setBackground(color);
panel.setBackground(color);
setBackground(color);
System.out.println(color);
RAAM SEDHU RR 18CSE111
repaint();
redButton.addActionListener(new Listener());
greenButton.addActionListener(new Listener());
blueButton.addActionListener(new Listener());
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
frame.add(panel);
frame.setVisible(true);
OUTPUT:
RAAM SEDHU RR 18CSE111
13.b. Write a java program to create a register form.
ALGORITHM:
Step1: Start
Step2: Initialize and declare the variables
Step3: Create a frame using swing and create a registration from
Step4: Create different panel layouts like name, last name, email, password and confirm
password and appropriate buttons
Step5: Display the result using applet
Step6: Stop
PROGRAM:
package javaapplication21;
import java.awt.*;
import java.awt.event.*;
public class JavaApplication21 {
public static void main(String[] args) {}}
class Loginform extends Frame implements ActionListener
{ Label l1=new Label("First name");
Label l2=new Label("Last name");
Label l4=new Label("E-mail");
Label l5=new Label("password");
Label l6=new Label("Confirm Password");
Label l3=new Label(" ");
TextField t1=new TextField();
TextField t4=new TextField();
RAAM SEDHU RR 18CSE111
TextField t2=new TextField();
TextField t5=new TextField();
TextField t6=new TextField();
Button b= new Button("Register Now");
public Loginform()
{ add(l1);
add(t1);
add(l4);
add(t4);
add(l5);
add(t5);
add(l6);
add(t6);
add(l2);
add(t2);
add(b);
add(l3);
l1.setBounds(20,45,70,20);
t1.setBounds(180,45,200,20);
l2.setBounds(20,95,70,20);
t2.setBounds(180,95,200,20);
l4.setBounds(20,135,70,20);
t4.setBounds(180,135,200,20);
l5.setBounds(20,175,70,20);
t5.setBounds(180,165,200,20);
RAAM SEDHU RR 18CSE111
l6.setBounds(20,195,70,20);
t6.setBounds(180,195,200,20);
b.setBounds(100,225,70,20);
b.addActionListener(this);
t5.setEchoChar('*');
t6.setEchoChar('*');
addWindowListener(new mwa());
public void actionPerformed(ActionEvent e)
{ l3.setText("Welcome "+t1.getText()+t2.getText());}
public static void main(String s[])
{ Loginform l=new Loginform();
l.setSize(new Dimension(600,600));
l.setTitle("Login");
l.setVisible(true);
}}
class mwa extends WindowAdapter
{ public mwa(){}
public void windowClosing(WindowEvent e)
{ System.exit(0);
}}
OUTPUT:
RAAM SEDHU RR 18CSE111
OBSERVATION(20)
RECORD(5)
TOTAL(25)
RESULT:
Thus the given program is executed successfully using net beans.
RAAM SEDHU RR 18CSE111