Wednesday, January 22, 2014

Event Handling Example:1 (textField)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Event2 implements ActionListener
{
JFrame f;
Button b1,b2;
TextField t;
Event2(String s)
{
f=new JFrame(s);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

b1=new Button("Ok");
b2=new Button("Cancel");

b1.setBounds(20,100,50,40);
b2.setBounds(20,180,50,40);

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

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

t=new TextField();
t.setBounds(20,40,250,40);
f.add(t);

f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText("Okyzzz");
if(e.getSource()==b2)
t.setText("Cancelzz");
}
public static void main(String surat[])
{
new Event2("MaHi");
}
}

Output:

No comments:

Post a Comment