Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.event.*;
- import java.awt.*;
- public class TextFieldAddition extends Applet implements ActionListener
- {
- TextField n1,n2,ans;
- int no1=0;
- int no2=0;
- Button a1,a2,a3,a4,clr;
- public void init()
- {
- Label nl1 = new Label("Number 1: ",Label.RIGHT);
- Label nl2 = new Label("Number 2: ",Label.RIGHT);
- Label nl3 = new Label("Answer: ",Label.RIGHT);
- a1 = new Button("ADD");
- a2 = new Button("SUB");
- a3 = new Button("MUL");
- a4 = new Button("DIV");
- clr = new Button("clear");
- n1 = new TextField("0",10);
- n2 = new TextField("0",10);
- ans = new TextField("0",10);
- add(nl1);
- add(n1);
- add(nl2);
- add(n2);
- add(nl3);
- add(ans);
- add(a1);
- add(a2);
- add(a3);
- add(a4);
- add(clr);
- a1.addActionListener(this);
- a2.addActionListener(this);
- a3.addActionListener(this);
- a4.addActionListener(this);
- clr.addActionListener(this);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getActionCommand().equals("ADD"))
- {
- no1 = Integer.parseInt(n1.getText());
- no2 = Integer.parseInt(n2.getText());
- ans.setText(String.valueOf(no1+no2));
- }
- else if(ae.getActionCommand().equals("MUL"))
- {
- no1 = Integer.parseInt(n1.getText());
- no2 = Integer.parseInt(n2.getText());
- ans.setText(String.valueOf(no1*no2));
- }
- else if(ae.getActionCommand().equals("SUB"))
- {
- no1 = Integer.parseInt(n1.getText());
- no2 = Integer.parseInt(n2.getText());
- ans.setText(String.valueOf(no1-no2));
- }
- else if(ae.getActionCommand().equals("DIV"))
- {
- no1 = Integer.parseInt(n1.getText());
- no2 = Integer.parseInt(n2.getText());
- ans.setText(String.valueOf(no1/no2));
- }
- else{
- n1.setText("");
- n2.setText("");
- ans.setText("");
- }
- }
- }
Add Comment
Please, Sign In to add comment