Advertisement
TermSpar

Java Calculator

Sep 7th, 2016
2,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.49 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextArea;
  8. import javax.swing.SwingUtilities;
  9.  
  10.  
  11. public class Calculator {
  12.    
  13.     private JFrame frame = new JFrame("Calculator");
  14.     private JTextArea txtScreen = new JTextArea();
  15.     private JLabel lblCredit = new JLabel("Created by Ben Bollinger");
  16.    
  17.    
  18.     //Calculation Variables:
  19.     String strNum1 = "";
  20.     String strNum2 = "";
  21.     String[] statement;
  22.     double num1 = 0;
  23.     double num2 = 0;
  24.     String strTotal = "";
  25.     double totalVal = 0;
  26.    
  27.     private JButton btnEqual = new JButton("=");
  28.    
  29.     //Number Buttons:
  30.     private JButton btn0 = new JButton("0");
  31.     private JButton btn1 = new JButton("1");
  32.     private JButton btn2 = new JButton("2");
  33.     private JButton btn3 = new JButton("3");
  34.     private JButton btn4 = new JButton("4");
  35.     private JButton btn5 = new JButton("5");
  36.     private JButton btn6 = new JButton("6");
  37.     private JButton btn7 = new JButton("7");
  38.     private JButton btn8 = new JButton("8");
  39.     private JButton btn9 = new JButton("9");
  40.    
  41.     //Operator Buttons:
  42.     private JButton btnDiv = new JButton("/");
  43.     private JButton btnMul = new JButton("*");
  44.     private JButton btnSub = new JButton("-");
  45.     private JButton btnAdd = new JButton("+");
  46.     private JButton btnDec = new JButton(".");
  47.     private JButton btnPower = new JButton("^");
  48.    
  49.     public Calculator() {
  50.         //Frame Attributes:
  51.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         frame.setVisible(true);
  53.         frame.setSize(400, 560);
  54.         frame.setResizable(false);
  55.         frame.setLayout(null);
  56.        
  57.         //txtScreen Attributes:
  58.         txtScreen.setSize(380, 150);
  59.         txtScreen.setLocation(7, 15);
  60.         txtScreen.setEditable(false);
  61.        
  62.         //lblCredit Attributes:
  63.         lblCredit.setSize(200, 100);
  64.         lblCredit.setLocation(12, 150);
  65.        
  66.         //btn0:
  67.         btn0.setSize(80, 50);
  68.         btn0.setLocation(105, 470);
  69.         btn0.addActionListener(new ActionListener(){
  70.             public void actionPerformed(ActionEvent arg0) {
  71.                 txtScreen.append("0");
  72.             }
  73.         });
  74.        
  75.         //btn1:
  76.         btn1.setSize(80, 50);
  77.         btn1.setLocation(10, 395);
  78.         btn1.addActionListener(new ActionListener(){
  79.             public void actionPerformed(ActionEvent arg0) {
  80.                 txtScreen.append("1");
  81.             }
  82.         });
  83.        
  84.         //btn2:
  85.         btn2.setSize(80, 50);
  86.         btn2.setLocation(105, 395);
  87.         btn2.addActionListener(new ActionListener(){
  88.             public void actionPerformed(ActionEvent arg0) {
  89.                 txtScreen.append("2");
  90.             }
  91.         });
  92.        
  93.         //btn3:
  94.         btn3.setSize(80, 50);
  95.         btn3.setLocation(200, 395);
  96.         btn3.addActionListener(new ActionListener(){
  97.             public void actionPerformed(ActionEvent arg0) {
  98.                 txtScreen.append("3");
  99.             }
  100.         });
  101.        
  102.         //btn4:
  103.         btn4.setSize(80, 50);
  104.         btn4.setLocation(10, 310);
  105.         btn4.addActionListener(new ActionListener(){
  106.             public void actionPerformed(ActionEvent arg0) {
  107.                 txtScreen.append("4");
  108.             }
  109.         });
  110.                
  111.         //btn5:
  112.         btn5.setSize(80, 50);
  113.         btn5.setLocation(105, 310);
  114.         btn5.addActionListener(new ActionListener(){
  115.             public void actionPerformed(ActionEvent arg0) {
  116.                 txtScreen.append("5");
  117.             }
  118.         });
  119.                
  120.         //btn6:
  121.         btn6.setSize(80, 50);
  122.         btn6.setLocation(200, 310);
  123.         btn6.addActionListener(new ActionListener(){
  124.             public void actionPerformed(ActionEvent arg0) {
  125.                 txtScreen.append("6");
  126.             }
  127.         });
  128.        
  129.         //btn7:
  130.         btn7.setSize(80, 50);
  131.         btn7.setLocation(10, 230);
  132.         btn7.addActionListener(new ActionListener(){
  133.             public void actionPerformed(ActionEvent arg0) {
  134.                 txtScreen.append("7");
  135.             }
  136.         });
  137.                        
  138.         //btn8:
  139.         btn8.setSize(80, 50);
  140.         btn8.setLocation(105, 230);
  141.         btn8.addActionListener(new ActionListener(){
  142.             public void actionPerformed(ActionEvent arg0) {
  143.                 txtScreen.append("8");
  144.             }
  145.         });
  146.                        
  147.         //btn9:
  148.         btn9.setSize(80, 50);
  149.         btn9.setLocation(200, 230);
  150.         btn9.addActionListener(new ActionListener(){
  151.             public void actionPerformed(ActionEvent arg0) {
  152.                 txtScreen.append("9");
  153.             }
  154.         });
  155.        
  156.         //btnDiv:
  157.         btnDiv.setSize(70, 60);
  158.         btnDiv.setLocation(310, 180);
  159.         btnDiv.addActionListener(new ActionListener(){
  160.             public void actionPerformed(ActionEvent arg0) {
  161.                 txtScreen.append("/");
  162.             }
  163.         });
  164.        
  165.         //btnMul:
  166.         btnMul.setSize(70, 60);
  167.         btnMul.setLocation(310, 250);
  168.         btnMul.addActionListener(new ActionListener(){
  169.             public void actionPerformed(ActionEvent arg0) {
  170.                 txtScreen.append("*");
  171.             }
  172.         });
  173.        
  174.         //btnSub
  175.         btnSub.setSize(70, 60);
  176.         btnSub.setLocation(310, 320);
  177.         btnSub.addActionListener(new ActionListener(){
  178.             public void actionPerformed(ActionEvent arg0) {
  179.                 txtScreen.append("-");
  180.             }
  181.         });
  182.        
  183.         //btnAdd:
  184.         btnAdd.setSize(70, 60);
  185.         btnAdd.setLocation(310, 390);
  186.         btnAdd.addActionListener(new ActionListener(){
  187.             public void actionPerformed(ActionEvent arg0) {
  188.                 txtScreen.append("+");
  189.             }
  190.         });
  191.        
  192.         //btnDec:
  193.         btnDec.setSize(80, 50);
  194.         btnDec.setLocation(200, 470);
  195.         btnDec.addActionListener(new ActionListener(){
  196.             public void actionPerformed(ActionEvent arg0) {
  197.                 txtScreen.append(".");
  198.             }
  199.         });
  200.        
  201.         //btnPower:
  202.         btnPower.setSize(80, 50);
  203.         btnPower.setLocation(10, 470);
  204.         btnPower.addActionListener(new ActionListener(){
  205.             public void actionPerformed(ActionEvent arg0) {
  206.                 txtScreen.append("^");
  207.             }
  208.         });
  209.        
  210.         //btnEqual:
  211.         btnEqual.setSize(70, 60);
  212.         btnEqual.setLocation(310, 460);
  213.         btnEqual.addActionListener(new ActionListener(){
  214.             public void actionPerformed(ActionEvent arg0) {
  215.                 if(txtScreen.getText().contains("+")){
  216.                     statement = txtScreen.getText().split("\\+");
  217.                    
  218.                     strNum1 = statement[0];
  219.                     strNum2 = statement[1];
  220.                    
  221.                     num1 = Double.parseDouble(strNum1);
  222.                     num2 = Double.parseDouble(strNum2);
  223.                    
  224.                     totalVal = num1 + num2;
  225.                    
  226.                     strTotal = Double.toString(totalVal);
  227.                    
  228.                     txtScreen.setText(strTotal);
  229.                 }
  230.                 else if(txtScreen.getText().contains("-")){
  231.                     statement = txtScreen.getText().split("\\-");
  232.                    
  233.                     strNum1 = statement[0];
  234.                     strNum2 = statement[1];
  235.                    
  236.                     num1 = Integer.parseInt(strNum1);
  237.                     num2 = Integer.parseInt(strNum2);
  238.                    
  239.                     totalVal = num1 - num2;
  240.                    
  241.                     strTotal = Double.toString(totalVal);
  242.                    
  243.                     txtScreen.setText(strTotal);
  244.                 }
  245.                 else if(txtScreen.getText().contains("/")){
  246.                     statement = txtScreen.getText().split("\\/");
  247.                    
  248.                     strNum1 = statement[0];
  249.                     strNum2 = statement[1];
  250.                    
  251.                     num1 = Double.parseDouble(strNum1);
  252.                     num2 = Double.parseDouble(strNum2);
  253.                    
  254.                     totalVal = num1 / num2;
  255.                    
  256.                     strTotal = Double.toString(totalVal);
  257.                    
  258.                     txtScreen.setText(strTotal);
  259.                 }
  260.                 else if(txtScreen.getText().contains("*")){
  261.                     statement = txtScreen.getText().split("\\*");
  262.                    
  263.                     strNum1 = statement[0];
  264.                     strNum2 = statement[1];
  265.                    
  266.                     num1 = Double.parseDouble(strNum1);
  267.                     num2 = Double.parseDouble(strNum2);
  268.                    
  269.                     totalVal = num1 * num2;
  270.                    
  271.                     strTotal = Double.toString(totalVal);
  272.                    
  273.                     txtScreen.setText(strTotal);
  274.                 }
  275.                 else if(txtScreen.getText().contains("^")){
  276.                     statement = txtScreen.getText().split("\\^");
  277.                    
  278.                     strNum1 = statement[0];
  279.                     strNum2 = statement[1];
  280.                    
  281.                     num1 = Double.parseDouble(strNum1);
  282.                     num2 = Double.parseDouble(strNum2);
  283.                    
  284.                     totalVal = 1;
  285.                     for(int i = 0; i < num2; i++){
  286.                         totalVal *= num1;
  287.                     }
  288.                    
  289.                     strTotal = Double.toString(totalVal);
  290.                    
  291.                     txtScreen.setText(strTotal);
  292.                 }
  293.             }
  294.         });
  295.        
  296.         //Add Objects To Screen:
  297.         frame.add(txtScreen);
  298.         frame.add(btn0);
  299.         frame.add(btn1);
  300.         frame.add(btn2);
  301.         frame.add(btn3);
  302.         frame.add(btn4);
  303.         frame.add(btn5);
  304.         frame.add(btn6);
  305.         frame.add(btn7);
  306.         frame.add(btn8);
  307.         frame.add(btn9);
  308.        
  309.         frame.add(lblCredit);
  310.        
  311.         frame.add(btnDiv);
  312.         frame.add(btnMul);
  313.         frame.add(btnSub);
  314.         frame.add(btnAdd);
  315.         frame.add(btnDec);
  316.         frame.add(btnPower);
  317.        
  318.         frame.add(btnEqual);
  319.        
  320.         SwingUtilities.updateComponentTreeUI(frame);
  321.     }
  322.  
  323.     public static void main(String[] args) {
  324.         new Calculator();
  325.     }
  326.  
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement