Advertisement
casparcedro

calc latest

Feb 1st, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.93 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package GUI;
  6.  
  7. import java.util.Scanner;
  8.  
  9. /**
  10.  *
  11.  * @author Caspar Cedro
  12.  */
  13. public class CalculatorGUI extends javax.swing.JFrame {
  14.  
  15.     Integer firstDouble = null;
  16.     Integer secondDouble = null;
  17.     Integer current = null;
  18.     double answer = 0;
  19.     int rundivision = 0;
  20.  
  21.     public void division(boolean run) {
  22.         if (run = true) {
  23.             rundivision++;
  24.             if (firstDouble != null) {
  25.                 result.setText("/");
  26.                 rundivision++;
  27.                 if (secondDouble != null) {
  28.  
  29.                     answer = firstDouble / secondDouble;
  30.                     rundivision++;
  31.                     rundivision = 0;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     public void multiplication() {
  38.     }
  39.     /*public void operation (String operator){
  40.      double resultant = firstDouble + operator + secondDouble ;
  41.      }*/
  42. //Integer operator = null;
  43.     /*
  44.      //Variables
  45.      double plusminus;
  46.      double firstDouble;
  47.      double secondDouble;
  48.      double totalDouble;
  49.      //To check for button click
  50.      int plusClick;
  51.      int minusClick;
  52.      int multiplyClick;
  53.      int divideClick;
  54.      */
  55.  
  56.     /**
  57.      * Creates new form CalculatorGUI
  58.      */
  59.     public CalculatorGUI() {
  60.         initComponents();
  61.         //Insert code to operate calculator display here!
  62.         //result.setText("test");
  63.         //result.setText("");
  64.         //Scanner input = new Scanner(System.in);
  65.         result.setText("");
  66.         //int answer = input.nextInt();
  67.         /*if (input.hasNextInt()){
  68.          result.setText("("+input+")");
  69.          /*if (answer >= 0){
  70.          result.setText("("+answer+")");
  71.          } else {
  72.          result.setText("");
  73.          }*/
  74.         //result.setText("("+answer+")");
  75.         /*if (input.hasNextInt()) {
  76.          result.setText("(" + input.nextInt() + ")");
  77.          } else {
  78.          result.setText("");
  79.          }
  80.          } else {
  81.          result.setText("");
  82.          }*/
  83.     }
  84.  
  85.     /**
  86.      * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
  87.      */
  88.     @SuppressWarnings("unchecked")
  89.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  90.     private void initComponents() {
  91.  
  92.         division = new javax.swing.JButton();
  93.         one = new javax.swing.JButton();
  94.         four = new javax.swing.JButton();
  95.         seven = new javax.swing.JButton();
  96.         multiplication = new javax.swing.JButton();
  97.         two = new javax.swing.JButton();
  98.         five = new javax.swing.JButton();
  99.         eight = new javax.swing.JButton();
  100.         addition = new javax.swing.JButton();
  101.         three = new javax.swing.JButton();
  102.         six = new javax.swing.JButton();
  103.         nine = new javax.swing.JButton();
  104.         subtraction = new javax.swing.JButton();
  105.         zero = new javax.swing.JButton();
  106.         equals = new javax.swing.JButton();
  107.         decimal = new javax.swing.JButton();
  108.         clear = new javax.swing.JButton();
  109.         clearAll = new javax.swing.JButton();
  110.         result = new javax.swing.JFormattedTextField();
  111.  
  112.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  113.  
  114.         division.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  115.         division.setText("รท");
  116.         division.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  117.         division.addActionListener(new java.awt.event.ActionListener() {
  118.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  119.                 divisionActionPerformed(evt);
  120.             }
  121.         });
  122.  
  123.         one.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  124.         one.setText("1");
  125.         one.addActionListener(new java.awt.event.ActionListener() {
  126.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  127.                 oneActionPerformed(evt);
  128.             }
  129.         });
  130.  
  131.         four.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  132.         four.setText("4");
  133.         four.addActionListener(new java.awt.event.ActionListener() {
  134.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  135.                 fourActionPerformed(evt);
  136.             }
  137.         });
  138.  
  139.         seven.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  140.         seven.setText("7");
  141.         seven.setToolTipText("");
  142.         seven.addActionListener(new java.awt.event.ActionListener() {
  143.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  144.                 sevenActionPerformed(evt);
  145.             }
  146.         });
  147.  
  148.         multiplication.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  149.         multiplication.setText("x");
  150.         multiplication.addActionListener(new java.awt.event.ActionListener() {
  151.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  152.                 multiplicationActionPerformed(evt);
  153.             }
  154.         });
  155.  
  156.         two.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  157.         two.setText("2");
  158.         two.addActionListener(new java.awt.event.ActionListener() {
  159.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  160.                 twoActionPerformed(evt);
  161.             }
  162.         });
  163.  
  164.         five.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  165.         five.setText("5");
  166.         five.addActionListener(new java.awt.event.ActionListener() {
  167.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  168.                 fiveActionPerformed(evt);
  169.             }
  170.         });
  171.  
  172.         eight.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  173.         eight.setText("8");
  174.         eight.addActionListener(new java.awt.event.ActionListener() {
  175.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  176.                 eightActionPerformed(evt);
  177.             }
  178.         });
  179.  
  180.         addition.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  181.         addition.setText("+");
  182.         addition.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  183.         addition.addActionListener(new java.awt.event.ActionListener() {
  184.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  185.                 additionActionPerformed(evt);
  186.             }
  187.         });
  188.  
  189.         three.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  190.         three.setText("3");
  191.         three.addActionListener(new java.awt.event.ActionListener() {
  192.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  193.                 threeActionPerformed(evt);
  194.             }
  195.         });
  196.  
  197.         six.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  198.         six.setText("6");
  199.         six.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  200.         six.addActionListener(new java.awt.event.ActionListener() {
  201.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  202.                 sixActionPerformed(evt);
  203.             }
  204.         });
  205.  
  206.         nine.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  207.         nine.setText("9");
  208.         nine.addActionListener(new java.awt.event.ActionListener() {
  209.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  210.                 nineActionPerformed(evt);
  211.             }
  212.         });
  213.  
  214.         subtraction.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
  215.         subtraction.setText("-");
  216.         subtraction.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  217.         subtraction.addActionListener(new java.awt.event.ActionListener() {
  218.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  219.                 subtractionActionPerformed(evt);
  220.             }
  221.         });
  222.  
  223.         zero.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  224.         zero.setText("0");
  225.         zero.addActionListener(new java.awt.event.ActionListener() {
  226.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  227.                 zeroActionPerformed(evt);
  228.             }
  229.         });
  230.  
  231.         equals.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  232.         equals.setText("=");
  233.         equals.addActionListener(new java.awt.event.ActionListener() {
  234.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  235.                 equalsActionPerformed(evt);
  236.             }
  237.         });
  238.  
  239.         decimal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  240.         decimal.setText(".");
  241.  
  242.         clear.setText("C");
  243.  
  244.         clearAll.setText("CA");
  245.         clearAll.addActionListener(new java.awt.event.ActionListener() {
  246.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  247.                 clearAllActionPerformed(evt);
  248.             }
  249.         });
  250.  
  251.         result.setEditable(false);
  252.         result.addActionListener(new java.awt.event.ActionListener() {
  253.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  254.                 resultActionPerformed(evt);
  255.             }
  256.         });
  257.  
  258.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  259.         getContentPane().setLayout(layout);
  260.         layout.setHorizontalGroup(
  261.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  262.             .addGroup(layout.createSequentialGroup()
  263.                 .addContainerGap()
  264.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  265.                     .addComponent(result)
  266.                     .addGroup(layout.createSequentialGroup()
  267.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  268.                             .addGroup(layout.createSequentialGroup()
  269.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  270.                                     .addComponent(one, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  271.                                     .addComponent(four, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  272.                                     .addComponent(seven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  273.                                     .addComponent(division))
  274.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  275.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  276.                                     .addComponent(two, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  277.                                     .addComponent(five, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  278.                                     .addComponent(eight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  279.                                     .addComponent(multiplication, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  280.                                     .addComponent(zero)))
  281.                             .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
  282.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  283.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  284.                             .addGroup(layout.createSequentialGroup()
  285.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  286.                                     .addComponent(three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  287.                                     .addComponent(decimal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  288.                                     .addComponent(six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  289.                                     .addComponent(addition, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  290.                                     .addComponent(nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  291.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  292.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  293.                                     .addComponent(subtraction, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
  294.                                     .addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  295.                             .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
  296.                         .addGap(0, 0, Short.MAX_VALUE)))
  297.                 .addContainerGap())
  298.         );
  299.         layout.setVerticalGroup(
  300.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  301.             .addGroup(layout.createSequentialGroup()
  302.                 .addContainerGap()
  303.                 .addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
  304.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  305.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  306.                     .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  307.                     .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  308.                 .addGap(8, 8, 8)
  309.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  310.                     .addGroup(layout.createSequentialGroup()
  311.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  312.                             .addGroup(layout.createSequentialGroup()
  313.                                 .addComponent(division, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  314.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  315.                                 .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  316.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  317.                                 .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  318.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  319.                                 .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
  320.                             .addGroup(layout.createSequentialGroup()
  321.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  322.                                     .addComponent(addition, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  323.                                     .addComponent(subtraction, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
  324.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  325.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  326.                                     .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
  327.                                     .addGroup(layout.createSequentialGroup()
  328.                                         .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  329.                                         .addGap(57, 57, 57)
  330.                                         .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
  331.                             .addGroup(layout.createSequentialGroup()
  332.                                 .addGap(102, 102, 102)
  333.                                 .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
  334.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  335.                         .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
  336.                     .addGroup(layout.createSequentialGroup()
  337.                         .addComponent(multiplication, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  338.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  339.                         .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  340.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  341.                         .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  342.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  343.                         .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  344.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  345.                         .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
  346.                 .addContainerGap(12, Short.MAX_VALUE))
  347.         );
  348.  
  349.         pack();
  350.     }// </editor-fold>                        
  351.  
  352.     private void oneActionPerformed(java.awt.event.ActionEvent evt) {                                    
  353.         // TODO add your handling code here:
  354.         result.setText(result.getText() + "1");
  355.         if (firstDouble == null) {
  356.             firstDouble = 1;
  357.         }
  358.  
  359.         if (secondDouble == null) {
  360.             secondDouble = 1;
  361.         }
  362.         /* double yolo = Double.parseDouble(result.getText());
  363.      
  364.          //int number = Integer.parseInt(result.getText());
  365.          yolo++;
  366.          System.out.println(Double.parseDouble("1"));
  367.          result.setText(""+yolo+"");
  368.          //result.setText(""+1+"");*/
  369.     }                                  
  370.  
  371.     private void twoActionPerformed(java.awt.event.ActionEvent evt) {                                    
  372.         // TODO add your handling code here:
  373.         result.setText(result.getText() + "2");
  374.         if (firstDouble == null) {
  375.             firstDouble = 2;
  376.         }
  377.  
  378.         if (secondDouble == null) {
  379.             secondDouble = 2;
  380.         }
  381.  
  382.     }                                  
  383.  
  384.     private void threeActionPerformed(java.awt.event.ActionEvent evt) {                                      
  385.         // TODO add your handling code here:
  386.         result.setText(result.getText() + "3");
  387.         if (firstDouble == null) {
  388.             firstDouble = 3;
  389.         }
  390.  
  391.         if (secondDouble == null) {
  392.             secondDouble = 3;
  393.         }
  394.  
  395.     }                                    
  396.  
  397.     private void fourActionPerformed(java.awt.event.ActionEvent evt) {                                    
  398.         // TODO add your handling code here:
  399.         result.setText(result.getText() + "4");
  400.         if (firstDouble == null) {
  401.             firstDouble = 4;
  402.         }
  403.  
  404.         if (secondDouble == null) {
  405.             secondDouble = 4;
  406.         }
  407.  
  408.     }                                    
  409.  
  410.     private void fiveActionPerformed(java.awt.event.ActionEvent evt) {                                    
  411.         // TODO add your handling code here:
  412.         result.setText(result.getText() + "5");
  413.         if (firstDouble == null) {
  414.             firstDouble = 5;
  415.         }
  416.  
  417.         if (secondDouble == null) {
  418.             secondDouble = 5;
  419.         }
  420.  
  421.     }                                    
  422.  
  423.     private void sixActionPerformed(java.awt.event.ActionEvent evt) {                                    
  424.         // TODO add your handling code here:
  425.         result.setText(result.getText() + "6");
  426.         if (firstDouble == null) {
  427.             firstDouble = 6;
  428.         }
  429.  
  430.         if (secondDouble == null) {
  431.             secondDouble = 6;
  432.         }
  433.  
  434.     }                                  
  435.  
  436.     private void sevenActionPerformed(java.awt.event.ActionEvent evt) {                                      
  437.         // TODO add your handling code here:
  438.         result.setText(result.getText() + "7");
  439.         if (firstDouble == null);
  440.         {
  441.             if (rundivision >= 0) {
  442.                 int answer7 = Integer.parseInt(result.getText());
  443.                 firstDouble = answer7;
  444.             }
  445.         }
  446.         if (rundivision >= 0) {
  447.             if (secondDouble == null) {
  448.  
  449.                 int answer7part2 = Integer.parseInt(result.getText());
  450.                 secondDouble = answer7part2;
  451.             }
  452.         }
  453.  
  454.     }                                    
  455.  
  456.     private void eightActionPerformed(java.awt.event.ActionEvent evt) {                                      
  457.         // TODO add your handling code here:
  458.         result.setText(result.getText() + "8");
  459.         if (firstDouble == null) {
  460.             firstDouble = Integer.parseInt(result.getText());
  461.         }
  462.  
  463.         if (secondDouble == null) {
  464.             secondDouble = Integer.parseInt(result.getText());
  465.         }
  466.  
  467.     }                                    
  468.  
  469.     private void nineActionPerformed(java.awt.event.ActionEvent evt) {                                    
  470.         // TODO add your handling code here:
  471.         result.setText(result.getText() + "9");
  472.         if (firstDouble == null) {
  473.             firstDouble = Integer.parseInt(result.getText());
  474.         }
  475.  
  476.         if (secondDouble == null) {
  477.             secondDouble = Integer.parseInt(result.getText());
  478.         }
  479.  
  480.     }                                    
  481.  
  482.     private void zeroActionPerformed(java.awt.event.ActionEvent evt) {                                    
  483.         // TODO add your handling code here:
  484.  
  485.         result.setText(result.getText() + "0");
  486.  
  487.         if (firstDouble == null) {
  488.             firstDouble = Integer.parseInt(result.getText());
  489.         }
  490.  
  491.         if (secondDouble == null) {
  492.             secondDouble = Integer.parseInt(result.getText());
  493.         }
  494.     }                                    
  495.  
  496.     private void clearAllActionPerformed(java.awt.event.ActionEvent evt) {                                        
  497.         // TODO add your handling code here:
  498.         result.setText("");
  499.         firstDouble = null;
  500.         secondDouble = null;
  501.     }                                        
  502.  
  503.     private void resultActionPerformed(java.awt.event.ActionEvent evt) {                                      
  504.         // TODO add your handling code here:
  505.     }                                      
  506.  
  507.     private void divisionActionPerformed(java.awt.event.ActionEvent evt) {                                        
  508.         // TODO add your handling code here:
  509.         division(true);
  510.  
  511.  
  512.     }                                        
  513.  
  514.     private void multiplicationActionPerformed(java.awt.event.ActionEvent evt) {                                              
  515.         // TODO add your handling code here:
  516.  
  517.         if (firstDouble != null) {
  518.             if (secondDouble != null) {
  519.                 result.setText("X");
  520.                 answer = firstDouble * secondDouble;
  521.             }
  522.         }
  523.     }                                              
  524.  
  525.     private void additionActionPerformed(java.awt.event.ActionEvent evt) {                                        
  526.         // TODO add your handling code here:
  527.  
  528.         if (firstDouble != null) {
  529.             if (secondDouble != null) {
  530.                 result.setText("+");
  531.                 answer = firstDouble + secondDouble;
  532.             }
  533.         }
  534.     }                                        
  535.  
  536.     private void subtractionActionPerformed(java.awt.event.ActionEvent evt) {                                            
  537.         // TODO add your handling code here:
  538.  
  539.         if (firstDouble != null) {
  540.             if (secondDouble != null) {
  541.                 result.setText("-");
  542.                 answer = firstDouble - secondDouble;
  543.             }
  544.         }
  545.     }                                          
  546.  
  547.     private void equalsActionPerformed(java.awt.event.ActionEvent evt) {                                      
  548.         // TODO add your handling code here:
  549.         result.setText("" + answer + "");
  550.         /* double answer = firstDouble + secondDouble;
  551.          if(firstDouble != null){
  552.          if(operator != null){
  553.          if(secondDouble != null){
  554.          result.setText("(" + answer +")");
  555.          }
  556.          }
  557.          }*/
  558.     }                                      
  559.  
  560.     /**
  561.      * @param args the command line arguments
  562.      */
  563.     public static void main(String args[]) {
  564.         /* Set the Nimbus look and feel */
  565.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  566.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  567.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  568.          */
  569.         try {
  570.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  571.                 if ("Nimbus".equals(info.getName())) {
  572.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  573.                     break;
  574.                 }
  575.             }
  576.         } catch (ClassNotFoundException ex) {
  577.             java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  578.         } catch (InstantiationException ex) {
  579.             java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  580.         } catch (IllegalAccessException ex) {
  581.             java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  582.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  583.             java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  584.         }
  585.         //</editor-fold>
  586.  
  587.         /* Create and display the form */
  588.         java.awt.EventQueue.invokeLater(new Runnable() {
  589.             public void run() {
  590.                 new CalculatorGUI().setVisible(true);
  591.             }
  592.         });
  593.     }
  594.     // Variables declaration - do not modify                    
  595.     private javax.swing.JButton addition;
  596.     private javax.swing.JButton clear;
  597.     private javax.swing.JButton clearAll;
  598.     private javax.swing.JButton decimal;
  599.     private javax.swing.JButton division;
  600.     private javax.swing.JButton eight;
  601.     private javax.swing.JButton equals;
  602.     private javax.swing.JButton five;
  603.     private javax.swing.JButton four;
  604.     private javax.swing.JButton multiplication;
  605.     private javax.swing.JButton nine;
  606.     private javax.swing.JButton one;
  607.     private javax.swing.JFormattedTextField result;
  608.     private javax.swing.JButton seven;
  609.     private javax.swing.JButton six;
  610.     private javax.swing.JButton subtraction;
  611.     private javax.swing.JButton three;
  612.     private javax.swing.JButton two;
  613.     private javax.swing.JButton zero;
  614.     // End of variables declaration                  
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement