TheGadgetCatTumblr

2 Hour Calculator (March 14th 2012: Pi Day)

Mar 14th, 2012
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5.  
  6. public class Main {
  7.  
  8.     //Frame constructs
  9.     public static JFrame frame;
  10.     public static JPanel mainPanel;
  11.     public static JPanel numbersPanel;
  12.     public static JPanel buttonPanel;
  13.     public static JTextArea output;
  14.     public static JTextArea input;
  15.    
  16.     //numerical buttons
  17.     public static JButton one;
  18.     public static JButton two;
  19.     public static JButton three;
  20.     public static JButton four;
  21.     public static JButton five;
  22.     public static JButton six;
  23.     public static JButton seven;
  24.     public static JButton eight;
  25.     public static JButton nine;
  26.     public static JButton zero;
  27.    
  28.     //operator buttons
  29.     public static JButton add;
  30.     public static JButton subtract;
  31.     public static JButton multiply;
  32.     public static JButton divide;
  33.     public static JButton clear;
  34.     public static JButton clearAll;
  35.     public static JButton decimal;
  36.    
  37.     //Action listener
  38.     public static ActionListener buttonListener;
  39.    
  40.     //Doubles for storing math
  41.     public static double numOne = 0;
  42.     public static double numTwo = 0;
  43.    
  44.     public static void main(String[] args){
  45.        
  46.         //initialize all the components
  47.         Initialize();
  48.         //set up the GUI
  49.         GUI();
  50.         //update the button presses and do the math
  51.         Update();
  52.        
  53.     }
  54.    
  55.     public static void Initialize(){
  56.        
  57.         //simple inits for frame, panels and text fields
  58.         frame = new JFrame("Calculator");
  59.        
  60.         mainPanel = new JPanel();
  61.         numbersPanel = new JPanel();
  62.         buttonPanel = new JPanel();
  63.        
  64.         output = new JTextArea();
  65.         input = new JTextArea();
  66.        
  67.         frame.setSize(300,380);
  68.         frame.setResizable(false);
  69.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.        
  71.         mainPanel.setSize(300,380);
  72.         numbersPanel.setPreferredSize(new Dimension(280,50));
  73.         buttonPanel.setPreferredSize(new Dimension(290,380));
  74.        
  75.         output.setPreferredSize(new Dimension(280,20));
  76.         output.setEditable(false);
  77.        
  78.         input.setPreferredSize(new Dimension(280,20));
  79.         input.setEditable(false);
  80.        
  81.         //universal dimension to make it easier to change later
  82.         Dimension buttonDimension = new Dimension(65,50);
  83.        
  84.         //set up dimensions for buttons
  85.         //numerical buttons
  86.         one = new JButton("1");
  87.         one.setPreferredSize(buttonDimension);
  88.         two = new JButton("2");
  89.         two.setPreferredSize(buttonDimension);
  90.         three = new JButton("3");
  91.         three.setPreferredSize(buttonDimension);
  92.         four = new JButton("4");
  93.         four.setPreferredSize(buttonDimension);
  94.         five = new JButton("5");
  95.         five.setPreferredSize(buttonDimension);
  96.         six = new JButton("6");
  97.         six.setPreferredSize(buttonDimension);
  98.         seven = new JButton("7");
  99.         seven.setPreferredSize(buttonDimension);
  100.         eight = new JButton("8");
  101.         eight.setPreferredSize(buttonDimension);
  102.         nine = new JButton("9");
  103.         nine.setPreferredSize(buttonDimension);
  104.         zero = new JButton("0");
  105.         zero.setPreferredSize(buttonDimension);
  106.        
  107.         //operator buttons
  108.         add = new JButton("+");
  109.         add.setPreferredSize(buttonDimension);
  110.         subtract = new JButton("-");
  111.         subtract.setPreferredSize(buttonDimension);
  112.         multiply = new JButton("x");
  113.         multiply.setPreferredSize(buttonDimension);
  114.         divide = new JButton("\\");
  115.         divide.setPreferredSize(buttonDimension);
  116.         clear = new JButton("C");
  117.         clear.setPreferredSize(buttonDimension);
  118.         clearAll = new JButton("Clear All");
  119.         clearAll.setPreferredSize(new Dimension(100,50));
  120.         decimal = new JButton(".");
  121.         decimal.setPreferredSize(buttonDimension);
  122.     }
  123.    
  124.     public static void GUI(){
  125.        
  126.         //add everything to the appropriate frame/panel
  127.         frame.add(mainPanel);
  128.        
  129.         mainPanel.add(numbersPanel);
  130.         mainPanel.add(buttonPanel);
  131.        
  132.         numbersPanel.add(output);
  133.         numbersPanel.add(input);
  134.        
  135.         buttonPanel.add(one);
  136.         buttonPanel.add(two);
  137.         buttonPanel.add(three);
  138.         buttonPanel.add(add);
  139.        
  140.         buttonPanel.add(four);
  141.         buttonPanel.add(five);
  142.         buttonPanel.add(six);
  143.         buttonPanel.add(subtract);
  144.        
  145.         buttonPanel.add(seven);
  146.         buttonPanel.add(eight);
  147.         buttonPanel.add(nine);
  148.         buttonPanel.add(multiply);
  149.         buttonPanel.add(zero);
  150.        
  151.         buttonPanel.add(divide);
  152.         buttonPanel.add(decimal);
  153.         buttonPanel.add(clear);
  154.         buttonPanel.add(clearAll);
  155.        
  156.         //make sure it's all visible
  157.         frame.setVisible(true);
  158.     }
  159.    
  160.     public static void Update(){
  161.        
  162.         //the action listener to control all the buttons
  163.         buttonListener = new ActionListener(){
  164.             public void actionPerformed(ActionEvent e) {
  165.                
  166.                 //input for numerical buttons
  167.                 if(e.getSource().equals(one))
  168.                     input.append("1");
  169.                 if(e.getSource().equals(two))
  170.                     input.append("2");
  171.                 if(e.getSource().equals(three))
  172.                     input.append("3");
  173.                 if(e.getSource().equals(four))
  174.                     input.append("4");
  175.                 if(e.getSource().equals(five))
  176.                     input.append("5");
  177.                 if(e.getSource().equals(six))
  178.                     input.append("6");
  179.                 if(e.getSource().equals(seven))
  180.                     input.append("7");
  181.                 if(e.getSource().equals(eight))
  182.                     input.append("8");
  183.                 if(e.getSource().equals(nine))
  184.                     input.append("9");
  185.                 if(e.getSource().equals(zero))
  186.                     input.append("0");
  187.                
  188.                 //for adding decimals
  189.                 if(e.getSource().equals(decimal))
  190.                     input.append(".");
  191.                
  192.                 //for inputting math operators
  193.                 //ADDING
  194.                 if(e.getSource().equals(add)){
  195.                     //don't deal with empty strings
  196.                     if(input.getText().equals(""))
  197.                         numOne = 0;
  198.                     else
  199.                         numOne = Double.parseDouble(input.getText());
  200.                    
  201.                     numTwo += numOne;
  202.                    
  203.                     //clear the input
  204.                     input.setText("");
  205.                    
  206.                     //display the sum
  207.                     output.setText("" + numTwo);
  208.                 }
  209.                
  210.                 //SUBTRACTING
  211.                 if(e.getSource().equals(subtract)){
  212.                     //don't deal with empty strings
  213.                     if(input.getText().equals(""))
  214.                         numOne = 0;
  215.                     else
  216.                         numOne = Double.parseDouble(input.getText());
  217.                
  218.                     numTwo -= numOne;
  219.                    
  220.                     //clear the input
  221.                     input.setText("");
  222.                    
  223.                     //display difference
  224.                     output.setText("" + numTwo);
  225.                 }
  226.                
  227.                 //MULTIPLYING
  228.                 if(e.getSource().equals(multiply)){
  229.                     //don't deal with empty strings
  230.                     if(input.getText().equals(""))
  231.                         numOne = 0;
  232.                     else
  233.                         numOne = Double.parseDouble(input.getText());
  234.                    
  235.                     numTwo *= numOne;
  236.                    
  237.                     //clear the input
  238.                     input.setText("");
  239.                    
  240.                     //display product
  241.                     output.setText("" + numTwo);
  242.                 }
  243.                
  244.                 //DIVIDING
  245.                 if(e.getSource().equals(divide)){
  246.                     //don't deal with empty strings
  247.                     if(input.getText().equals(""))
  248.                         numOne = 0;
  249.                     else
  250.                         numOne = Double.parseDouble(input.getText());
  251.                    
  252.                     numTwo /= numOne;
  253.                    
  254.                     //clear the input
  255.                     input.setText("");
  256.                    
  257.                     //display the quotient
  258.                     output.setText("" + numTwo);
  259.                 }
  260.                
  261.                 //clear the input
  262.                 if(e.getSource().equals(clear)){
  263.                     //clear text field
  264.                     input.setText("");
  265.                    
  266.                     //clear number in memory
  267.                     numOne = 0;
  268.                 }
  269.                
  270.                 //clear the whole calculator
  271.                 if(e.getSource().equals(clearAll)){
  272.                     //clear the text fields
  273.                     input.setText("");
  274.                     output.setText("");
  275.                    
  276.                     //clear the numbers in memory
  277.                     numOne = 0;
  278.                     numTwo = 0;
  279.                 }
  280.                    
  281.             }
  282.         };
  283.        
  284.         //add all the action listeners...
  285.         //could probably house buttons in array and apply through that...
  286.         one.addActionListener(buttonListener);
  287.         two.addActionListener(buttonListener);
  288.         three.addActionListener(buttonListener);
  289.         four.addActionListener(buttonListener);
  290.         five.addActionListener(buttonListener);
  291.         six.addActionListener(buttonListener);
  292.         seven.addActionListener(buttonListener);
  293.         eight.addActionListener(buttonListener);
  294.         nine.addActionListener(buttonListener);
  295.         zero.addActionListener(buttonListener);
  296.        
  297.         add.addActionListener(buttonListener);
  298.         subtract.addActionListener(buttonListener);
  299.         multiply.addActionListener(buttonListener);
  300.         divide.addActionListener(buttonListener);
  301.         clear.addActionListener(buttonListener);
  302.         clearAll.addActionListener(buttonListener);
  303.         decimal.addActionListener(buttonListener);
  304.     }
  305.    
  306. }
Advertisement
Add Comment
Please, Sign In to add comment