programme:- to make calculator addition application through form using windows action listner.
import java.awt.*;
import java.awt.event.*;
public class calci extends Frame implements ActionListener
{
TextField t1,t2,t3;
public static void main(String args[])
{
calci t = new calci();
}
public calci()
{
super("Event in Java awt");
setLayout(new BorderLayout());
try
{
t1=new TextField();
t2=new TextField();
add(t1, BorderLayout.NORTH);
add(t2, BorderLayout.CENTER);
Button button1 = new Button("add");
button1.addActionListener(this);
add(button1, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
catch (Exception e){}
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button button1 = (Button)e.getSource();
t2.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
}
Save it by calci.java
Run it on cmd
1) javac calci.java
2) java calci
import java.awt.*;
import java.awt.event.*;
public class calci extends Frame implements ActionListener
{
TextField t1,t2,t3;
public static void main(String args[])
{
calci t = new calci();
}
public calci()
{
super("Event in Java awt");
setLayout(new BorderLayout());
try
{
t1=new TextField();
t2=new TextField();
add(t1, BorderLayout.NORTH);
add(t2, BorderLayout.CENTER);
Button button1 = new Button("add");
button1.addActionListener(this);
add(button1, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
catch (Exception e){}
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button button1 = (Button)e.getSource();
t2.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
}
Save it by calci.java
Run it on cmd
1) javac calci.java
2) java calci
No comments:
Post a Comment