Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculator 2.0 (Java Swing)
- * Date : 13.08.19
- */
- package Swing;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.font.*;
- import java.awt.Font;
- public class Calculator implements ActionListener {
- JTextField tf;
- JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
- JButton plus, minus, mult, div, equal, point, sqrt, power, reset;
- String display = "0", temporary = "0";
- double dis = 0, temp = 0;
- int operation = 0;
- boolean pointonof = false;
- boolean poweronof = false;
- Font f1 = new Font("Consolas", Font.BOLD, 40);
- Calculator(){
- JFrame f = new JFrame("Calculator");
- tf = new JTextField("0");
- tf.setBounds(50, 50, 300, 50);
- tf.setHorizontalAlignment(JTextField.RIGHT);
- tf.setFont(f1);
- f.add(tf);
- b7 = new JButton("7");
- b7.setBounds(50, 110, 50, 50);
- b7.addActionListener(this);
- f.add(b7);
- b8 = new JButton("8");
- b8.setBounds(110, 110, 50, 50);
- b8.addActionListener(this);
- f.add(b8);
- b9 = new JButton("9");
- b9.setBounds(170, 110, 50, 50);
- b9.addActionListener(this);
- f.add(b9);
- div = new JButton("/");
- div.setBounds(230, 110, 50, 50);
- div.addActionListener(this);
- f.add(div);
- sqrt = new JButton("Sqrt");
- sqrt.setBounds(290, 110, 60, 50);
- sqrt.addActionListener(this);
- f.add(sqrt);
- b4 = new JButton("4");
- b4.setBounds(50, 170, 50, 50);
- b4.addActionListener(this);
- f.add(b4);
- b5 = new JButton("5");
- b5.setBounds(110, 170, 50, 50);
- b5.addActionListener(this);
- f.add(b5);
- b6 = new JButton("6");
- b6.setBounds(170, 170, 50, 50);
- b6.addActionListener(this);
- f.add(b6);
- mult = new JButton("X");
- mult.setBounds(230, 170, 50, 50);
- mult.addActionListener(this);
- f.add(mult);
- power = new JButton("X^Y");
- power.setBounds(290, 170, 60, 50);
- power.addActionListener(this);
- f.add(power);
- b1 = new JButton("1");
- b1.setBounds(50, 230, 50, 50);
- b1.addActionListener(this);
- f.add(b1);
- b2 = new JButton("2");
- b2.setBounds(110, 230, 50, 50);
- b2.addActionListener(this);
- f.add(b2);
- b3 = new JButton("3");
- b3.setBounds(170, 230, 50, 50);
- b3.addActionListener(this);
- f.add(b3);
- minus = new JButton("-");
- minus.setBounds(230, 230, 50, 50);
- minus.addActionListener(this);
- f.add(minus);
- reset = new JButton("RC");
- reset.setBounds(290, 230, 60, 50);
- reset.addActionListener(this);
- f.add(reset);
- b0 = new JButton("0");
- b0.setBounds(50, 290, 50, 50);
- b0.addActionListener(this);
- f.add(b0);
- point = new JButton(".");
- point.setBounds(110, 290, 50, 50);
- point.addActionListener(this);
- f.add(point);
- equal = new JButton("=");
- equal.setBounds(170, 290, 50, 50);
- equal.addActionListener(this);
- f.add(equal);
- plus = new JButton("+");
- plus.setBounds(230, 290, 50, 50);
- plus.addActionListener(this);
- f.add(plus);
- f.setSize(420,400);
- f.setLayout(null);
- f.setVisible(true);
- }
- public void actionPerformed(ActionEvent e){
- if(e.getSource() == reset){
- display = "0"; temporary = "0";
- dis = 0; temp = 0;
- operation = 0;
- pointonof = false;
- poweronof = false;
- tf.setText(display);
- }
- else if(e.getSource() == point){
- if(!pointonof){
- pointonof = true;
- display = display + '.';
- tf.setText(display);
- }
- }
- else if(e.getSource() == b0){
- if(display != "0"){
- display = display + '0';
- }
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b1){
- if(display == "0"){ display = "1"; }
- else display = display + '1';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b2){
- if(display == "0"){ display = "2"; }
- else display = display + '2';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b3){
- if(display == "0"){ display = "3"; }
- else display = display + '3';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b4){
- if(display == "0"){ display = "4"; }
- else display = display + '4';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b5){
- if(display == "0"){ display = "5"; }
- else display = display + '5';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b6){
- if(display == "0"){ display = "6"; }
- else display = display + '6';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b7){
- if(display == "0"){ display = "7"; }
- else display = display + '7';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b8){
- if(display == "0"){ display = "8"; }
- else display = display + '8';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == b9){
- if(display == "0"){ display = "9"; }
- else display = display + '9';
- dis = Double.parseDouble(display);
- tf.setText(display);
- }
- else if(e.getSource() == equal){
- if(operation == 1){
- temp += dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 2){
- temp -= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 3){
- temp *= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 4){
- temp /= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- tf.setText(display);
- display = "0";
- operation = 0;
- pointonof = false;
- }
- else if(e.getSource() == plus){
- if(operation == 1){
- temp += dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 2){
- temp -= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 3){
- temp *= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 4){
- temp /= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else{
- temp = Double.parseDouble(display);
- }
- operation = 1;
- tf.setText(display);
- display = "0";
- pointonof = false;
- }
- else if(e.getSource() == minus){
- if(operation == 1){
- temp += dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 2){
- temp -= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 3){
- temp *= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 4){
- temp /= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else{
- temp = Double.parseDouble(display);
- }
- operation = 2;
- tf.setText(display);
- display = "0";
- pointonof = false;
- }
- else if(e.getSource() == mult){
- if(operation == 1){
- temp += dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 2){
- temp -= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 3){
- temp *= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 4){
- temp /= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else{
- temp = Double.parseDouble(display);
- }
- operation = 3;
- tf.setText(display);
- display = "0";
- pointonof = false;
- }
- else if(e.getSource() == div){
- if(operation == 1){
- temp += dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 2){
- temp -= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 3){
- temp *= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else if(operation == 4){
- temp /= dis;
- dis = 0;
- display = String.valueOf(temp);
- }
- else{
- temp = Double.parseDouble(display);
- }
- operation = 4;
- tf.setText(display);
- display = "0";
- pointonof = false;
- }
- else if(e.getSource() == sqrt){
- if(operation == 0){
- dis = Math.sqrt(dis);
- display = String.valueOf(dis);
- tf.setText(display);
- }
- else{
- display = "ERROR";
- tf.setText(display);
- }
- display = "0";
- //dis = 0;
- pointonof = false;
- operation = 0;
- }
- else if(e.getSource() == power){
- if(operation == 0){
- if(poweronof){
- dis = Math.pow(temp, dis);
- display = String.valueOf(dis);
- tf.setText(display);
- dis = 0;
- display = "0";
- //dis = 0;
- pointonof = false;
- operation = 0;
- pointonof = false;
- }
- else{
- poweronof = true;
- temp = dis;
- dis = 0;
- display = "0";
- }
- }
- else{
- display = "ERROR";
- tf.setText(display);
- display = "0";
- //dis = 0;
- pointonof = false;
- operation = 0;
- }
- }
- }
- public static void main(String[] args){
- new Calculator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement