Friday, January 24, 2014

Example of getSource() Event Handling in Java.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mahi1 implements ActionListener
{
JFrame f;
JButton b1,b2,b3;
Label l1,l2;
Panel p;

mahi1()
{
f=new JFrame("MaHi");
p=new Panel();
f.setLayout(new GridLayout(1,1));
f.setSize(400,400);
f.setVisible(true);

l1=new Label();
l1.setAlignment(Label.CENTER);
l1.setText("Students of LCVIT");
f.add(l1);

b1=new JButton("MaHi");
b2=new JButton("Bips");
b3=new JButton("Anju");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

p.add(b1);
p.add(b2);
p.add(b3);
f.add(p);

l2=new Label();
l2.setAlignment(Label.CENTER);
l2.setSize(350,100);
f.add(l2);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
l2.setText("Mahi comes in Action");
if(e.getSource()==b2)
l2.setText("Bipin comes in Action");
if(e.getSource()==b3)
l2.setText("Anjali comes in Action");
}
public static void main(String surat[])
{
new mahi1();
}
}


No comments:

Post a Comment