Wednesday, January 22, 2014

Event Handling Example(2)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Event3 implements ActionListener
{
JFrame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
TextField t;
Event3(String s)
{
f=new JFrame(s);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
t=new TextField();
t.setBounds(100,40,280,40);
f.add(t);

b1=new Button("1");
b1.setBounds(100,100,40,40);

b2=new Button("2");
b2.setBounds(180,100,40,40);

b3=new Button("3");
b3.setBounds(260,100,40,40);

b4=new Button("4");
b4.setBounds(340,100,40,40);

b5=new Button("5");
b5.setBounds(140,180,40,40);

b6=new Button("6");
b6.setBounds(220,180,40,40);

b7=new Button("7");
b7.setBounds(300,180,40,40);

b8=new Button("8");
b8.setBounds(180,260,40,40);

b9=new Button("9");
b9.setBounds(260,260,40,40);

b0=new Button("0");
b0.setBounds(220,340,40,40);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);

f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b0);

f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText("1");
if(e.getSource()==b2)
t.setText("2");
if(e.getSource()==b3)
t.setText("3");
if(e.getSource()==b4)
t.setText("4");
if(e.getSource()==b5)
t.setText("5");
if(e.getSource()==b6)
t.setText("6");
if(e.getSource()==b7)
t.setText("7");
if(e.getSource()==b8)
t.setText("8");
if(e.getSource()==b9)
t.setText("9");
if(e.getSource()==b0)
t.setText("0");
}
public static void main(String surat[])
{
new Event3("MaHi");
}
}

Output:

No comments:

Post a Comment