Advertisement
MladenPetrov

Untitled

Sep 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.event.KeyEvent;
  5.  
  6.  
  7.  
  8. public class Calculator implements ActionListener {
  9.     JFrame frame;
  10.     JTextField text;
  11.     JButton  b1, b2, b3, b4, b5, b6, b7, b8,b9, b0, bplus, bminus, bmul, bdiv, bclear, bdec, bequal;
  12.     double b;
  13.     double c;
  14.     double result;
  15.     String operator;
  16.  
  17.  
  18.     Calculator(){
  19.  
  20.         frame = new JFrame("Calculator");
  21.         text = new JTextField("");
  22.         frame.getContentPane().setBackground(Color.BLACK);
  23.         b1 = new JButton("1");b1.setBackground(Color.CYAN);
  24.         b2 = new JButton("2");b2.setBackground(Color.CYAN);
  25.         b3 = new JButton("3");b3.setBackground(Color.CYAN);
  26.         b4 = new JButton("4");b4.setBackground(Color.CYAN);
  27.         b5 = new JButton("5");b5.setBackground(Color.CYAN);
  28.         b6 = new JButton("6");b6.setBackground(Color.CYAN);
  29.         b7 = new JButton("7");b7.setBackground(Color.CYAN);
  30.         b8 = new JButton("8");b8.setBackground(Color.CYAN);
  31.         b9 = new JButton("9");b9.setBackground(Color.CYAN);
  32.         b0 = new JButton("0");b0.setBackground(Color.CYAN);
  33.         bplus = new JButton("+");bplus.setBackground(Color.YELLOW);
  34.         bminus = new JButton("-");bminus.setBackground(Color.YELLOW);
  35.         bmul = new JButton("*");bmul.setBackground(Color.YELLOW);
  36.         bdiv = new JButton("/");bdiv.setBackground(Color.YELLOW);
  37.         bclear = new JButton("C");bclear.setBackground(Color.YELLOW);
  38.         bdec = new JButton(".");bdec.setBackground(Color.YELLOW);
  39.         bequal = new JButton("=");bequal.setBackground(Color.YELLOW);
  40.  
  41.         text.setBounds(30,40,280,30);
  42.         b7.setBounds(40,100,50,40);
  43.         b8.setBounds(110,100,50,40);
  44.         b9.setBounds(180,100,50,40);
  45.         bdiv.setBounds(250,100,50,40);
  46.  
  47.         b4.setBounds(40,170,50,40);
  48.         b5.setBounds(110,170,50,40);
  49.         b6.setBounds(180,170,50,40);
  50.         bmul.setBounds(250,170,50,40);
  51.  
  52.         b1.setBounds(40,240,50,40);
  53.         b2.setBounds(110,240,50,40);
  54.         b3.setBounds(180,240,50,40);
  55.         bminus.setBounds(250,240,50,40);
  56.  
  57.         bdec.setBounds(40,310,50,40);
  58.         b0.setBounds(110,310,50,40);
  59.         bequal.setBounds(180,310,50,40);
  60.         bplus.setBounds(250,310,50,40);
  61.  
  62.         bclear.setBounds(180,380,100,40);
  63.  
  64.         frame.add(text);
  65.         frame.add(b7);
  66.         frame.add(b8);
  67.         frame.add(b9);
  68.         frame.add(bdiv);
  69.         frame.add(b4);
  70.         frame.add(b5);
  71.         frame.add(b6);
  72.         frame.add(bmul);
  73.         frame.add(b1);
  74.         frame.add(b2);
  75.         frame.add(b3);
  76.         frame.add(bminus);
  77.         frame.add(bdec);
  78.         frame.add(b0);
  79.         frame.add(bequal);
  80.         frame.add(bplus);
  81.         frame.add(bclear);
  82.  
  83.         frame.setLayout(null);
  84.         frame.setVisible(true);
  85.         frame.setSize(350 , 500);
  86.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  87.  
  88.         b1.addActionListener(this);
  89.         b2.addActionListener(this);
  90.         b3.addActionListener(this);
  91.         b4.addActionListener(this);
  92.         b5.addActionListener(this);
  93.         b6.addActionListener(this);
  94.         b7.addActionListener(this);
  95.         b8.addActionListener(this);
  96.         b9.addActionListener(this);
  97.         b0.addActionListener(this);
  98.         bplus.addActionListener(this);
  99.         bminus.addActionListener(this);
  100.         bmul.addActionListener(this);
  101.         bdiv.addActionListener(this);
  102.         bequal.addActionListener(this);
  103.         bclear.addActionListener(this);
  104.         bdec.addActionListener(this);
  105.  
  106.     }
  107.  
  108.  
  109.     public  void actionPerformed (ActionEvent a){
  110.         if (a.getSource() == b1){
  111.             text.setText(text.getText().concat("1"));
  112.  
  113.         }
  114.         if (a.getSource() == b2){
  115.             text.setText(text.getText().concat("2"));
  116.         }
  117.         if (a.getSource() == b3){
  118.             text.setText(text.getText().concat("3"));
  119.         }
  120.         if (a.getSource() == b4){
  121.             text.setText(text.getText().concat("4"));
  122.         }
  123.         if (a.getSource() == b5){
  124.             text.setText(text.getText().concat("5"));
  125.         }
  126.         if (a.getSource() == b6){
  127.             text.setText(text.getText().concat("6"));
  128.         }
  129.         if (a.getSource() == b7){
  130.             text.setText(text.getText().concat("7"));
  131.         }
  132.         if (a.getSource() == b8){
  133.             text.setText(text.getText().concat("8"));
  134.         }
  135.         if (a.getSource() == b9){
  136.             text.setText(text.getText().concat("9"));
  137.         }
  138.         if (a.getSource() == b0){
  139.             text.setText(text.getText().concat("0"));
  140.         }
  141.         if (a.getSource() == bdec){
  142.             text.setText(text.getText().concat("."));
  143.         }
  144.         if (a.getSource() == bplus){
  145.             b = Double.parseDouble(text.getText());
  146.             text.setText("");
  147.             operator = "+";
  148.         }
  149.         if (a.getSource() == bminus){
  150.             b = Double.parseDouble(text.getText());
  151.             text.setText("");
  152.             operator = "-";
  153.         }
  154.         if (a.getSource() == bmul){
  155.             b = Double.parseDouble(text.getText());
  156.             text.setText("");
  157.             operator = "*";
  158.         }
  159.         if (a.getSource() == bdiv){
  160.             b = Double.parseDouble(text.getText());
  161.             text.setText("");
  162.             operator = "/";
  163.         }
  164.         if (a.getSource() == bequal) {
  165.             c = Double.parseDouble(text.getText());
  166.             if (operator.equals("+")) {
  167.                 result = b + c;
  168.             }
  169.             if (operator.equals("-")){
  170.                 result = b - c;
  171.             }
  172.             if (operator.equals("*")) {
  173.                 result = b * c;
  174.             }
  175.             if (operator.equals("/")) {
  176.                 result = b / c;
  177.             }
  178.             text.setText(result + "");
  179.         }
  180.         if (a.getSource() == bclear){
  181.             text.setText("");
  182.         }
  183.  
  184.     }
  185.  
  186.     public static void main(String[] args) {
  187.         new Calculator();
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement