aakash2310

Basic Cal

Mar 9th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. public class TextFieldAddition extends Applet implements ActionListener
  5. {
  6.     TextField n1,n2,ans;
  7.     int no1=0;
  8.     int no2=0;
  9.     Button a1,a2,a3,a4,clr;
  10.     public void init()
  11.     {
  12.             Label nl1 = new Label("Number 1: ",Label.RIGHT);
  13.             Label nl2 = new Label("Number 2: ",Label.RIGHT);
  14.             Label nl3 = new Label("Answer: ",Label.RIGHT);
  15.             a1 = new Button("ADD");
  16.             a2 = new Button("SUB");
  17.             a3 = new Button("MUL");
  18.             a4 = new Button("DIV");
  19.             clr = new Button("clear");
  20.             n1 = new TextField("0",10);
  21.             n2 = new TextField("0",10);
  22.             ans = new TextField("0",10);
  23.  
  24.             add(nl1);
  25.             add(n1);
  26.             add(nl2);
  27.             add(n2);
  28.             add(nl3);
  29.             add(ans);
  30.             add(a1);
  31.             add(a2);
  32.             add(a3);
  33.             add(a4);
  34.             add(clr);
  35.                 a1.addActionListener(this);
  36.                 a2.addActionListener(this);
  37.                 a3.addActionListener(this);
  38.                 a4.addActionListener(this);
  39.                 clr.addActionListener(this);
  40.  
  41.     }
  42.     public void actionPerformed(ActionEvent ae)
  43.     {
  44.         if(ae.getActionCommand().equals("ADD"))
  45.         {
  46.             no1 = Integer.parseInt(n1.getText());
  47.             no2 = Integer.parseInt(n2.getText());
  48.             ans.setText(String.valueOf(no1+no2));
  49.         }
  50.         else if(ae.getActionCommand().equals("MUL"))
  51.                 {
  52.                     no1 = Integer.parseInt(n1.getText());
  53.                     no2 = Integer.parseInt(n2.getText());
  54.                     ans.setText(String.valueOf(no1*no2));
  55.         }
  56.         else if(ae.getActionCommand().equals("SUB"))
  57.                 {
  58.                     no1 = Integer.parseInt(n1.getText());
  59.                     no2 = Integer.parseInt(n2.getText());
  60.                     ans.setText(String.valueOf(no1-no2));
  61.         }
  62.         else if(ae.getActionCommand().equals("DIV"))
  63.                 {
  64.                     no1 = Integer.parseInt(n1.getText());
  65.                     no2 = Integer.parseInt(n2.getText());
  66.                     ans.setText(String.valueOf(no1/no2));
  67.         }
  68.         else{
  69.                 n1.setText("");
  70.                 n2.setText("");
  71.                 ans.setText("");
  72.         }
  73.  
  74.     }
  75.  
  76. }
Add Comment
Please, Sign In to add comment