Addition of Two Numbers - Java AWT Example - Internet & Web Programming Lab

Program:

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

class ex1 extends Frame implements ActionListener
{
 Label l1,l2,l3,l4;
 TextField t1,t2;
 Button b1,b2;
 ex1()
 {
   super("Addition of two numbers");
   setLayout(null);
   setSize(500,400);
   setVisible(true);
   l1=new Label("Enter the first no:");
   l2=new Label("Enter the second no:");
   l3=new Label("The sum is:");
   l4=new Label(null);
   t1=new TextField();
   t2=new TextField();
   b1=new Button("ADD");
   b2=new Button("Clear");
   l1.setBounds(100,50,120,20);
   add(l1);
   t1.setBounds(240,50,50,20);
   add(t1);
   l2.setBounds(100,80,130,20);
   add(l2);
   t2.setBounds(240,80,50,20);
   add(t2);
   l3.setBounds(100,110,100,20);
   add(l3);
   l4.setBounds(210,110,60,20);
   add(l4);
   b1.setBounds(200,150,50,20);
   add(b1);
   b2.setBounds(270,150,50,20);
   add(b2);
   b1.addActionListener(this);
   b2.addActionListener(this);
  
   addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent we)
    {
     System.exit(0);
    }
   });
 }

 public void actionPerformed(ActionEvent ae)
 {
  float a,b,c;
  if(ae.getSource()==b1)
  {
   a=Float.parseFloat(t1.getText().trim());
   b=Float.parseFloat(t2.getText().trim());
   c=a+b;
   l4.setText(Float.toString(c));
  }
  else
  {
   t1.setText(null);
   t2.setText(null);
   l4.setText(null);
  }
 }

 public static void main(String s[])
 {
  ex1 ob=new ex1();
 }
}

Output:

nn@linuxmint ~/Desktop/java 7 $ javac ex1.java
nn@linuxmint ~/Desktop/java 7 $ java ex1
nn@linuxmint ~/Desktop/java 7 $


4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  3. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

    selenium training in Chennai
    selenium training in Tambaram
    selenium training in Velachery
    selenium training in Omr
    selenium training in Annanagar

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    python Training institute in Chennai
    python Training institute in Bangalore
    python Training in Pune

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...