Frame with ActionEvent

import java.awt.*;
import java.awt.event.*;

public class TestEventFrame extends Frame implements ActionListener
{
Button bt, clearbt,addbt;
TextField tf1, tf2, tf3;
Label lno1, lno2;

public TestEventFrame() {
//creating components
bt = new Button("Close");
clearbt = new Button("Clear");
addbt = new Button("Add");
tf1 = new TextField(3);
tf2 = new TextField(3);
tf3 = new TextField(3);
tf3.setEditable(false);
lno1 = new Label("No1 :");
lno2 = new Label("No2 :");
//Adding Components
add(lno1); add(tf1); add(lno2); add(tf2); add(tf3);
add(bt);
add(addbt);
add(clearbt);
//registering listener
bt.addActionListener(this);
addbt.addActionListener(this);
clearbt.addActionListener(this);
//setting properties of Frame
setSize(400,500);
setVisible(true);
setTitle("First Frame");
setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent ae) {

if (ae.getSource().equals(bt))
{   System.exit(0); }
else if (ae.getSource().equals(addbt) )
{
String sno1 = tf1.getText();
String sno2 = tf2.getText();

int sum = Integer.parseInt(sno1) + Integer.parseInt(sno2);

//Integer iobj = Integer.valueOf(sum);
//String sums = iobj.toString();
tf3.setText(String.valueOf(sum));

}
else  {
tf1.setText(""); tf2.setText(""); tf3.setText("");
}

}


public static void main(String args[]) {

//TestEventFrame tf =
new TestEventFrame();




}


}

Comments

Popular posts from this blog

Custom Exception example for BankAccount

HashMap HashSet example

Annotation