Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple Calculator (using AWT)
- * Date : 10.08.19
- */
- package AWT;
- import java.awt.*;
- import java.awt.event.*;
- public class Calculator implements ActionListener {
- TextField tf1, tf2, tf3;
- Button b1, b2, b3, b4;
- Calculator(){
- Frame f = new Frame("Calculator");
- f.setSize(300, 300);
- f.setLayout(null);
- f.setVisible(true);
- Label l1 = new Label("Input 1: ");
- l1.setBounds(50, 50, 40, 20);
- Label l2 = new Label("Input 2: ");
- l2.setBounds(50, 110, 40, 20);
- Label l3 = new Label("Output: ");
- l3.setBounds(50, 170, 40, 20);
- tf1 = new TextField();
- tf1.setBounds(100, 50, 100, 20);
- tf2 = new TextField();
- tf2.setBounds(100, 110, 100, 20);
- tf3 = new TextField();
- tf3.setBounds(100, 170, 100, 20);
- tf3.setEditable(false);
- b1 = new Button("+");
- b1.setBounds(50, 200, 30, 30);
- b1.addActionListener(this);
- b2 = new Button("-");
- b2.setBounds(90, 200, 30, 30);
- b2.addActionListener(this);
- b3 = new Button("*");
- b3.setBounds(130, 200, 30, 30);
- b3.addActionListener(this);
- b4 = new Button("/");
- b4.setBounds(170, 200, 30, 30);
- b4.addActionListener(this);
- f.add(tf1); f.add(tf2); f.add(tf3);
- f.add(l1); f.add(l2); f.add(l3);
- f.add(b1); f.add(b2); f.add(b3); f.add(b4);
- }
- public void actionPerformed(ActionEvent e){
- String in1 = tf1.getText();
- String in2 = tf2.getText();
- double a = Double.parseDouble(in1);
- double b = Double.parseDouble(in2);
- double c = 0;
- if(e.getSource() == b1){ c = a + b; }
- else if(e.getSource() == b2){ c = a - b; }
- else if(e.getSource() == b3){ c = a * b; }
- else if(e.getSource() == b4){ c = a / b; }
- String result = String.valueOf(c);
- tf3.setText(result);
- }
- public static void main(String[] args){
- new Calculator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement