Advertisement
go6eto

Untitled

Jun 23rd, 2021
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class JavaCalculator {
  7.  
  8.     private double total1 = 0.0;
  9.     private double total2 = 0.0;
  10.     private char math_operator;
  11.  
  12.     private JPanel JavaCalculator;
  13.     private JTextField textDisplay;
  14.     private JButton btnEquals;
  15.     private JButton btnMultiply;
  16.     private JButton btnTwo;
  17.     private JButton btnThree;
  18.     private JButton btnFive;
  19.     private JButton btnEight;
  20.     private JButton btnPoint;
  21.     private JButton btnPlus;
  22.     private JButton btnSix;
  23.     private JButton btnNine;
  24.     private JButton btnClear;
  25.     private JButton btnMinus;
  26.     private JButton btnDivide;
  27.     private JButton btnOne;
  28.     private JButton btnFour;
  29.     private JButton btnSeven;
  30.     private JButton btnZero;
  31.  
  32.      private void getOperator(String btnText){
  33.          math_operator = btnText.charAt(0);
  34.          total1 = total1 + Double.parseDouble(textDisplay.getText());
  35.          textDisplay.setText("");
  36.      }
  37.  
  38.     public JavaCalculator() {
  39.         btnOne.addActionListener(new ActionListener() {
  40.             @Override
  41.             public void actionPerformed(ActionEvent e) {
  42.                 String btnOneText = textDisplay.getText() + btnOne.getText();;
  43.                 textDisplay.setText(btnOneText);
  44.             }
  45.         });
  46.         btnTwo.addActionListener(new ActionListener() {
  47.             @Override
  48.             public void actionPerformed(ActionEvent e) {
  49.                 String btnTwoText = textDisplay.getText() + btnTwo.getText();
  50.                 textDisplay.setText(btnTwoText);
  51.  
  52.             }
  53.         });
  54.         btnThree.addActionListener(new ActionListener() {
  55.             @Override
  56.             public void actionPerformed(ActionEvent e) {
  57.                 String btnThreeText = textDisplay.getText() + btnThree.getText();
  58.                 textDisplay.setText(btnThreeText);
  59.  
  60.             }
  61.         });
  62.         btnFour.addActionListener(new ActionListener() {
  63.             @Override
  64.             public void actionPerformed(ActionEvent e) {
  65.                 String btnFourText = textDisplay.getText() + btnFour.getText();
  66.                 textDisplay.setText(btnFourText);
  67.  
  68.             }
  69.         });
  70.         btnFive.addActionListener(new ActionListener() {
  71.             @Override
  72.             public void actionPerformed(ActionEvent e) {
  73.                 String btnFiveText = textDisplay.getText() + btnFive.getText();
  74.                 textDisplay.setText(btnFiveText);
  75.  
  76.             }
  77.         });
  78.         btnSix.addActionListener(new ActionListener() {
  79.             @Override
  80.             public void actionPerformed(ActionEvent e) {
  81.                 String btnSixText = textDisplay.getText() + btnSix.getText();
  82.                 textDisplay.setText(btnSixText);
  83.  
  84.             }
  85.         });
  86.         btnSeven.addActionListener(new ActionListener() {
  87.             @Override
  88.             public void actionPerformed(ActionEvent e) {
  89.                 String btnSevenText = textDisplay.getText() + btnSeven.getText();
  90.                 textDisplay.setText(btnSevenText);
  91.  
  92.             }
  93.         });
  94.         btnEight.addActionListener(new ActionListener() {
  95.             @Override
  96.             public void actionPerformed(ActionEvent e) {
  97.                 String btnEightText = textDisplay.getText() + btnEight.getText();
  98.                 textDisplay.setText(btnEightText);
  99.  
  100.             }
  101.         });
  102.         btnNine.addActionListener(new ActionListener() {
  103.             @Override
  104.             public void actionPerformed(ActionEvent e) {
  105.                 String btnNineText = textDisplay.getText() + btnNine.getText();
  106.                 textDisplay.setText(btnNineText);
  107.  
  108.             }
  109.         });
  110.         btnZero.addActionListener(new ActionListener() {
  111.             @Override
  112.             public void actionPerformed(ActionEvent e) {
  113.                 String btnZeroText = textDisplay.getText() + btnZero.getText();
  114.                 textDisplay.setText(btnZeroText);
  115.  
  116.             }
  117.         });
  118.  
  119.         btnPlus.addActionListener(new ActionListener() {
  120.             @Override
  121.             public void actionPerformed(ActionEvent e) {
  122.                String button_text = btnPlus.getText();
  123.                getOperator(button_text);
  124.             }
  125.         });
  126.         btnEquals.addActionListener(new ActionListener() {
  127.             @Override
  128.             public void actionPerformed(ActionEvent e) {
  129.                 switch (math_operator){
  130.                     case '+':
  131.                         total2 = total1 + Double.parseDouble(textDisplay.getText());
  132.                         break;
  133.                     case '-':
  134.                         total2 = total1 - Double.parseDouble(textDisplay.getText());
  135.                         break;
  136.                     case '/':
  137.                         total2 = total1 / Double.parseDouble(textDisplay.getText());
  138.                         break;
  139.                     case '*':
  140.                         total2 = total1 + Double.parseDouble(textDisplay.getText());
  141.                         break;
  142.                 }
  143.                 textDisplay.setText(Double.toString(total2));
  144.                 total1 = 0;
  145.             }
  146.         });
  147.         btnClear.addActionListener(new ActionListener() {
  148.             @Override
  149.             public void actionPerformed(ActionEvent e) {
  150.                 total2 = 0;
  151.                 textDisplay.setText("");
  152.             }
  153.         });
  154.         btnPoint.addActionListener(new ActionListener() {
  155.             @Override
  156.             public void actionPerformed(ActionEvent e) {
  157.                 if(textDisplay.getText().equals(".")){
  158.                     btnPoint.setText("0.");
  159.                 }else if(textDisplay.getText().contains(".")){
  160.                     btnPoint.setEnabled(false);
  161.                 }else{
  162.                     String btnPointText = textDisplay.getText() + btnPoint.getText();
  163.                         textDisplay.setText(btnPointText);
  164.                 }
  165.                 btnPoint.setEnabled(true);
  166.             }
  167.         });
  168.         btnMinus.addActionListener(new ActionListener() {
  169.             @Override
  170.             public void actionPerformed(ActionEvent e) {
  171.                 String button_text = btnMinus.getText();
  172.                 getOperator(button_text);
  173.             }
  174.         });
  175.  
  176.         btnDivide.addActionListener(new ActionListener() {
  177.             @Override
  178.             public void actionPerformed(ActionEvent e) {
  179.                 String button_text = btnDivide.getText();
  180.                 getOperator(button_text);
  181.             }
  182.         });
  183.         btnMultiply.addActionListener(new ActionListener() {
  184.             @Override
  185.             public void actionPerformed(ActionEvent e) {
  186.                 String button_text = btnMultiply.getText();
  187.                 getOperator(button_text);
  188.             }
  189.         });
  190.     }
  191.  
  192.     public static void main(String[] args) {
  193.         JFrame frame = new JFrame("JavaCalculator");
  194.         frame.setContentPane(new JavaCalculator().JavaCalculator);
  195.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  196.         frame.pack();
  197.         frame.setVisible(true);
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement