Guest User

Untitled

a guest
Jan 25th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Calculator implements ActionListener{
  6.  
  7.     JFrame frame;
  8.     JTextField textfield;
  9.     JButton[] numberButtons = new JButton[10];
  10.     JButton[] functionButtons = new JButton[9];
  11.     JButton addButton,subButton,mulButton,divButton;
  12.     JButton decButton, equButton, delButton, clrButton, negButton;
  13.     JPanel panel;
  14.  
  15.     Font myFont = new Font("Serif",Font.BOLD,20);
  16.  
  17.     double num1=0,num2=0,result=0;
  18.     char operator;
  19.  
  20.     Calculator(){
  21.  
  22.         frame = new JFrame("Calculator");
  23.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         frame.setSize(420, 550);
  25.         frame.setLayout(null);
  26.  
  27.         textfield = new JTextField();
  28.         textfield.setBounds(50,25,300,50);
  29.         textfield.setFont(myFont);
  30.         textfield.setEditable(false);
  31.  
  32.         addButton = new JButton("+");
  33.         subButton = new JButton("-");
  34.         mulButton = new JButton("*");
  35.         divButton = new JButton("/");
  36.         decButton = new JButton(".");
  37.         equButton = new JButton("=");
  38.         delButton = new JButton("Delete");
  39.         clrButton = new JButton("Clear");
  40.         negButton = new JButton("(-)");
  41.  
  42.         functionButtons[0] = addButton;
  43.         functionButtons[1] = subButton;
  44.         functionButtons[2] = mulButton;
  45.         functionButtons[3] = divButton;
  46.         functionButtons[4] = decButton;
  47.         functionButtons[5] = equButton;
  48.         functionButtons[6] = delButton;
  49.         functionButtons[7] = clrButton;
  50.         functionButtons[8] = negButton;
  51.  
  52.         for(int i =0;i<9;i++){
  53.             functionButtons[i].addActionListener(this);
  54.             functionButtons[i].setFont(myFont);
  55.             functionButtons[i].setFocusable(false);
  56.         }
  57.  
  58.         for(int i =0;i<10;i++){
  59.             numberButtons[i] = new JButton(String.valueOf(i));
  60.             numberButtons[i].addActionListener(this);
  61.             numberButtons[i].setFont(myFont);
  62.             numberButtons[i].setFocusable(false);
  63.         }
  64.  
  65.         negButton.setBounds(50,430,100,50);
  66.         delButton.setBounds(150,430,100,50);
  67.         clrButton.setBounds(250,430,100,50);
  68.  
  69.         panel = new JPanel();
  70.         panel.setBounds(50,100,300,300);
  71.         panel.setLayout(new GridLayout(4,4,10,10));
  72.  
  73.         panel.add(numberButtons[1]);
  74.         panel.add(numberButtons[2]);
  75.         panel.add(numberButtons[3]);
  76.         panel.add(addButton);
  77.         panel.add(numberButtons[4]);
  78.         panel.add(numberButtons[5]);
  79.         panel.add(numberButtons[6]);
  80.         panel.add(subButton);
  81.         panel.add(numberButtons[7]);
  82.         panel.add(numberButtons[8]);
  83.         panel.add(numberButtons[9]);
  84.         panel.add(mulButton);
  85.         panel.add(decButton);
  86.         panel.add(numberButtons[0]);
  87.         panel.add(equButton);
  88.         panel.add(divButton);
  89.  
  90.         frame.add(panel);
  91.         frame.add(negButton);
  92.         frame.add(delButton);
  93.         frame.add(clrButton);
  94.         frame.add(textfield);
  95.         frame.setVisible(true);
  96.     }
  97.  
  98.     public static void main (String[] args){
  99.  
  100.         Calculator calc = new Calculator();
  101.     }
  102.  
  103.     public void actionPerformed(ActionEvent e){
  104.  
  105.         for(int i=0;i<10;i++){
  106.             if(e.getSource() == numberButtons[i]){
  107.                 textfield.setText(textfield.getText().concat(String.valueOf(i)));
  108.             }
  109.         }
  110.         if(e.getSource() == decButton){
  111.             textfield.setText(textfield.getText().concat("."));
  112.         }
  113.         if(e.getSource() == addButton){
  114.             num1 = Double.parseDouble(textfield.getText());
  115.             operator = '+';
  116.             textfield.setText("");
  117.         }
  118.         if(e.getSource() == subButton){
  119.             num1 = Double.parseDouble(textfield.getText());
  120.             operator = '-';
  121.             textfield.setText("");
  122.         }
  123.         if(e.getSource() == mulButton){
  124.             num1 = Double.parseDouble(textfield.getText());
  125.             operator = '*';
  126.             textfield.setText("");
  127.         }
  128.         if(e.getSource() == divButton){
  129.             num1 = Double.parseDouble(textfield.getText());
  130.             operator = '/';
  131.             textfield.setText("");
  132.         }
  133.         if(e.getSource() == equButton){
  134.             num2 = Double.parseDouble(textfield.getText());
  135.  
  136.             switch(operator){
  137.                 case'+':
  138.                     result=num1+num2;
  139.                     break;
  140.                 case'-':
  141.                     result=num1-num2;
  142.                     break;
  143.                 case'*':
  144.                     result=num1*num2;
  145.                     break;
  146.                 case'/':
  147.                     result=num1/num2;
  148.                     break;
  149.             }
  150.             textfield.setText(String.valueOf(result));
  151.             num1=result;
  152.         }
  153.         if(e.getSource() == clrButton){
  154.             textfield.setText("");
  155.         }
  156.         if(e.getSource() == delButton){
  157.             String string = textfield.getText();
  158.             textfield.setText("");
  159.             for(int i=0;i<string.length()-1;i++){
  160.                 textfield.setText(textfield.getText()+string.charAt(i));
  161.             }
  162.         }
  163.         if(e.getSource() == negButton){
  164.             double temp = Double.parseDouble(textfield.getText());
  165.             temp *= -1;
  166.             textfield.setText(String.valueOf(temp));
  167.         }
  168.  
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment