Guest User

Untitled

a guest
Sep 5th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 38.58 KB | None | 0 0
  1. package calc;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5.  
  6. public class Gui extends javax.swing.JFrame {
  7.  
  8.     double firstNum = 0;
  9.     double secondNum = 0;
  10.     String operation = "";
  11.  
  12.     public Gui() {
  13.         initComponents();
  14.     }
  15.  
  16.     //@SuppressWarnings("unchecked")
  17.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  18.     private void initComponents() {
  19.  
  20.         jTextField1 = new javax.swing.JTextField();
  21.         Button0 = new javax.swing.JButton();
  22.         Button1 = new javax.swing.JButton();
  23.         Button2 = new javax.swing.JButton();
  24.         Button3 = new javax.swing.JButton();
  25.         Button4 = new javax.swing.JButton();
  26.         Button5 = new javax.swing.JButton();
  27.         Button6 = new javax.swing.JButton();
  28.         Button7 = new javax.swing.JButton();
  29.         Button8 = new javax.swing.JButton();
  30.         Button9 = new javax.swing.JButton();
  31.         CButton = new javax.swing.JButton();
  32.         DivideButton = new javax.swing.JButton();
  33.         PowButton = new javax.swing.JButton();
  34.         PlusButton = new javax.swing.JButton();
  35.         MinusButton = new javax.swing.JButton();
  36.         PointButton = new javax.swing.JButton();
  37.         FinalButton = new javax.swing.JButton();
  38.  
  39.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  40.         setTitle("Calculator v1.0");
  41.         setBackground(new java.awt.Color(204, 204, 204));
  42.         setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  43.         setForeground(new java.awt.Color(255, 255, 255));
  44.         setResizable(false);
  45.  
  46.         jTextField1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
  47.         jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
  48.         jTextField1.setText("0");
  49.         jTextField1.setToolTipText("");
  50.         jTextField1.setEnabled(false);
  51.         jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
  52.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  53.                 jTextField1MouseClicked(evt);
  54.             }
  55.         });
  56.  
  57.         Button0.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  58.         Button0.setText("0");
  59.         Button0.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  60.         Button0.setFocusPainted(false);
  61.         Button0.setFocusable(false);
  62.         Button0.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 Button0ActionPerformed(evt);
  65.             }
  66.         });
  67.  
  68.         Button1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  69.         Button1.setText("1");
  70.         Button1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  71.         Button1.setFocusPainted(false);
  72.         Button1.setFocusable(false);
  73.         Button1.addActionListener(new java.awt.event.ActionListener() {
  74.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  75.                 Button1ActionPerformed(evt);
  76.             }
  77.         });
  78.  
  79.         Button2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  80.         Button2.setText("2");
  81.         Button2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  82.         Button2.setFocusPainted(false);
  83.         Button2.setFocusable(false);
  84.         Button2.addActionListener(new java.awt.event.ActionListener() {
  85.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  86.                 Button2ActionPerformed(evt);
  87.             }
  88.         });
  89.  
  90.         Button3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  91.         Button3.setText("3");
  92.         Button3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  93.         Button3.setFocusPainted(false);
  94.         Button3.setFocusable(false);
  95.         Button3.addActionListener(new java.awt.event.ActionListener() {
  96.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  97.                 Button3ActionPerformed(evt);
  98.             }
  99.         });
  100.  
  101.         Button4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  102.         Button4.setText("4");
  103.         Button4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  104.         Button4.setFocusPainted(false);
  105.         Button4.setFocusable(false);
  106.         Button4.addActionListener(new java.awt.event.ActionListener() {
  107.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  108.                 Button4ActionPerformed(evt);
  109.             }
  110.         });
  111.  
  112.         Button5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  113.         Button5.setText("5");
  114.         Button5.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  115.         Button5.setFocusPainted(false);
  116.         Button5.setFocusable(false);
  117.         Button5.addActionListener(new java.awt.event.ActionListener() {
  118.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  119.                 Button5ActionPerformed(evt);
  120.             }
  121.         });
  122.  
  123.         Button6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  124.         Button6.setText("6");
  125.         Button6.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  126.         Button6.setFocusPainted(false);
  127.         Button6.setFocusable(false);
  128.         Button6.addActionListener(new java.awt.event.ActionListener() {
  129.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  130.                 Button6ActionPerformed(evt);
  131.             }
  132.         });
  133.  
  134.         Button7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  135.         Button7.setText("7");
  136.         Button7.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  137.         Button7.setFocusPainted(false);
  138.         Button7.setFocusable(false);
  139.         Button7.addActionListener(new java.awt.event.ActionListener() {
  140.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  141.                 Button7ActionPerformed(evt);
  142.             }
  143.         });
  144.  
  145.         Button8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  146.         Button8.setText("8");
  147.         Button8.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  148.         Button8.setFocusPainted(false);
  149.         Button8.setFocusable(false);
  150.         Button8.addActionListener(new java.awt.event.ActionListener() {
  151.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  152.                 Button8ActionPerformed(evt);
  153.             }
  154.         });
  155.  
  156.         Button9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  157.         Button9.setText("9");
  158.         Button9.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  159.         Button9.setFocusPainted(false);
  160.         Button9.setFocusable(false);
  161.         Button9.addActionListener(new java.awt.event.ActionListener() {
  162.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  163.                 Button9ActionPerformed(evt);
  164.             }
  165.         });
  166.  
  167.         CButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  168.         CButton.setText("C");
  169.         CButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  170.         CButton.setFocusPainted(false);
  171.         CButton.setFocusable(false);
  172.         CButton.addActionListener(new java.awt.event.ActionListener() {
  173.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  174.                 CButtonActionPerformed(evt);
  175.             }
  176.         });
  177.  
  178.         DivideButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  179.         DivideButton.setText("/");
  180.         DivideButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  181.         DivideButton.setFocusPainted(false);
  182.         DivideButton.setFocusable(false);
  183.         DivideButton.addActionListener(new java.awt.event.ActionListener() {
  184.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  185.                 DivideButtonActionPerformed(evt);
  186.             }
  187.         });
  188.  
  189.         PowButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  190.         PowButton.setText("*");
  191.         PowButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  192.         PowButton.setFocusPainted(false);
  193.         PowButton.setFocusable(false);
  194.         PowButton.addActionListener(new java.awt.event.ActionListener() {
  195.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  196.                 PowButtonActionPerformed(evt);
  197.             }
  198.         });
  199.  
  200.         PlusButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  201.         PlusButton.setText("+");
  202.         PlusButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  203.         PlusButton.setFocusPainted(false);
  204.         PlusButton.setFocusable(false);
  205.         PlusButton.addActionListener(new java.awt.event.ActionListener() {
  206.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  207.                 PlusButtonActionPerformed(evt);
  208.             }
  209.         });
  210.  
  211.         MinusButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  212.         MinusButton.setText("-");
  213.         MinusButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  214.         MinusButton.setFocusPainted(false);
  215.         MinusButton.setFocusable(false);
  216.         MinusButton.addActionListener(new java.awt.event.ActionListener() {
  217.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  218.                 MinusButtonActionPerformed(evt);
  219.             }
  220.         });
  221.  
  222.         PointButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  223.         PointButton.setText(".");
  224.         PointButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  225.         PointButton.setFocusPainted(false);
  226.         PointButton.setFocusable(false);
  227.         PointButton.addActionListener(new java.awt.event.ActionListener() {
  228.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  229.                 PointButtonActionPerformed(evt);
  230.             }
  231.         });
  232.  
  233.         FinalButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  234.         FinalButton.setText("=");
  235.         FinalButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  236.         FinalButton.setFocusPainted(false);
  237.         FinalButton.setFocusable(false);
  238.         FinalButton.addActionListener(new java.awt.event.ActionListener() {
  239.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  240.                 FinalButtonActionPerformed(evt);
  241.             }
  242.         });
  243.  
  244.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  245.         getContentPane().setLayout(layout);
  246.         layout.setHorizontalGroup(
  247.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  248.             .addGroup(layout.createSequentialGroup()
  249.                 .addContainerGap()
  250.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  251.                     .addComponent(jTextField1)
  252.                     .addGroup(layout.createSequentialGroup()
  253.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  254.                             .addGroup(layout.createSequentialGroup()
  255.                                 .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  256.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  257.                                 .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  258.                             .addGroup(layout.createSequentialGroup()
  259.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  260.                                     .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  261.                                     .addComponent(CButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  262.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  263.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  264.                                     .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  265.                                     .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))
  266.                         .addGap(6, 6, 6)
  267.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  268.                             .addGroup(layout.createSequentialGroup()
  269.                                 .addComponent(PowButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  270.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  271.                                 .addComponent(MinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  272.                             .addGroup(layout.createSequentialGroup()
  273.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  274.                                     .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  275.                                     .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  276.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  277.                                 .addComponent(PlusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
  278.                         .addGap(0, 0, Short.MAX_VALUE))
  279.                     .addGroup(layout.createSequentialGroup()
  280.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  281.                             .addGroup(layout.createSequentialGroup()
  282.                                 .addComponent(Button0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  283.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  284.                                 .addComponent(PointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  285.                             .addGroup(layout.createSequentialGroup()
  286.                                 .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  287.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  288.                                 .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  289.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  290.                                 .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
  291.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  292.                         .addComponent(FinalButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
  293.                 .addContainerGap())
  294.         );
  295.         layout.setVerticalGroup(
  296.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  297.             .addGroup(layout.createSequentialGroup()
  298.                 .addContainerGap()
  299.                 .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  300.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  301.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  302.                     .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  303.                     .addComponent(PowButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  304.                     .addComponent(MinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  305.                     .addComponent(CButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  306.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  307.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  308.                     .addGroup(layout.createSequentialGroup()
  309.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  310.                             .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  311.                             .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  312.                             .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  313.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  314.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  315.                             .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  316.                             .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  317.                             .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
  318.                     .addComponent(PlusButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  319.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  320.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  321.                     .addGroup(layout.createSequentialGroup()
  322.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  323.                             .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  324.                             .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  325.                             .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  326.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  327.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  328.                             .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  329.                             .addComponent(PointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
  330.                     .addComponent(FinalButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  331.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  332.         );
  333.  
  334.         pack();
  335.     }// </editor-fold>                        
  336.  
  337.     private void Button7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  338.         String temp = jTextField1.getText();
  339.         int a = temp.lastIndexOf(" ");
  340.         if (!temp.equals("0")) {
  341.             jTextField1.setText(jTextField1.getText() + "7");
  342.         } else {
  343.             jTextField1.setText("7");
  344.         }
  345.         if (temp.substring(a + 1).startsWith("0")) {
  346.             jTextField1.setText(temp.substring(0, a + 1) + "7");
  347.         }
  348.     }                                      
  349.  
  350.     private void Button8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  351.         String temp = jTextField1.getText();
  352.         int a = temp.lastIndexOf(" ");
  353.         if (!temp.equals("0")) {
  354.             jTextField1.setText(jTextField1.getText() + "8");
  355.         } else {
  356.             jTextField1.setText("8");
  357.         }
  358.         if (temp.substring(a + 1).startsWith("0")) {
  359.             jTextField1.setText(temp.substring(0, a + 1) + "8");
  360.         }
  361.     }                                      
  362.  
  363.     private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  364.         String temp = jTextField1.getText();
  365.         int a = temp.lastIndexOf(" ");
  366.         if (!temp.equals("0")) {
  367.             jTextField1.setText(jTextField1.getText() + "9");
  368.         } else {
  369.             jTextField1.setText("9");
  370.         }
  371.         if (temp.substring(a + 1).startsWith("0")) {
  372.             jTextField1.setText(temp.substring(0, a + 1) + "9");
  373.         }
  374.     }                                      
  375.  
  376.     private void Button0ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  377.         jTextField1.setText(jTextField1.getText() + "0");
  378.         String temp = jTextField1.getText();
  379.         int a = temp.lastIndexOf(" ");
  380.         if (temp.contains(" ")) {
  381.             if (temp.startsWith("00", a + 1)) {
  382.                 temp = temp.substring(0, temp.length() - 1);
  383.             } else if (temp.startsWith("00", a + 1)) {
  384.                 temp = temp.substring(0, temp.length() - 1);
  385.             } else if (temp.startsWith("00", a + 1)) {
  386.                 temp = temp.substring(0, temp.length() - 1);
  387.             } else if (temp.startsWith("00", a + 1)) {
  388.                 temp = temp.substring(0, temp.length() - 1);
  389.             }
  390.         }
  391.         if (temp.startsWith("00")) {
  392.             temp = temp.substring(0, temp.length() - 1);
  393.         }
  394.         jTextField1.setText(temp);
  395.     }                                      
  396.  
  397.     private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  398.         String temp = jTextField1.getText();
  399.         int a = temp.lastIndexOf(" ");
  400.         if (!temp.equals("0")) {
  401.             jTextField1.setText(jTextField1.getText() + "1");
  402.         } else {
  403.             jTextField1.setText("1");
  404.         }
  405.         if (temp.substring(a + 1).startsWith("0")) {
  406.             jTextField1.setText(temp.substring(0, a + 1) + "1");
  407.         }
  408.     }                                      
  409.  
  410.     private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  411.         String temp = jTextField1.getText();
  412.         int a = temp.lastIndexOf(" ");
  413.         if (!temp.equals("0")) {
  414.             jTextField1.setText(jTextField1.getText() + "2");
  415.         } else {
  416.             jTextField1.setText("2");
  417.         }
  418.  
  419.         if (temp.substring(a + 1).startsWith("0")) {
  420.             jTextField1.setText(temp.substring(0, a + 1) + "2");
  421.         }
  422.  
  423.     }                                      
  424.  
  425.     private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  426.         String temp = jTextField1.getText();
  427.         int a = temp.lastIndexOf(" ");
  428.         if (!temp.equals("0")) {
  429.             jTextField1.setText(jTextField1.getText() + "3");
  430.         } else {
  431.             jTextField1.setText("3");
  432.         }
  433.         if (temp.substring(a + 1).startsWith("0")) {
  434.             jTextField1.setText(temp.substring(0, a + 1) + "3");
  435.         }
  436.     }                                      
  437.  
  438.     private void Button4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  439.         String temp = jTextField1.getText();
  440.         int a = temp.lastIndexOf(" ");
  441.         if (!temp.equals("0")) {
  442.             jTextField1.setText(jTextField1.getText() + "4");
  443.         } else {
  444.             jTextField1.setText("4");
  445.         }
  446.         if (temp.substring(a + 1).startsWith("0")) {
  447.             jTextField1.setText(temp.substring(0, a + 1) + "4");
  448.         }
  449.     }                                      
  450.  
  451.     private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  452.         String temp = jTextField1.getText();
  453.         int a = temp.lastIndexOf(" ");
  454.         if (!temp.equals("0")) {
  455.             jTextField1.setText(jTextField1.getText() + "5");
  456.         } else {
  457.             jTextField1.setText("5");
  458.         }
  459.         if (temp.substring(a + 1).startsWith("0")) {
  460.             jTextField1.setText(temp.substring(0, a + 1) + "5");
  461.         }
  462.     }                                      
  463.  
  464.     private void Button6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  465.         String temp = jTextField1.getText();
  466.         int a = temp.lastIndexOf(" ");
  467.         if (!temp.equals("0")) {
  468.             jTextField1.setText(jTextField1.getText() + "6");
  469.         } else {
  470.             jTextField1.setText("6");
  471.         }
  472.         if (temp.substring(a + 1).startsWith("0")) {
  473.             jTextField1.setText(temp.substring(0, a + 1) + "6");
  474.         }
  475.     }                                      
  476.  
  477.     private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {                                        
  478.         String temp = jTextField1.getText();
  479.         if (temp.endsWith(" * ") | temp.endsWith(" / ")
  480.                 | temp.endsWith(" + ") | temp.endsWith(" - ")) {
  481.             jTextField1.setText(temp.substring(0, temp.length() - 3));
  482.         } else if (temp.endsWith("Infinity") | temp.endsWith("NaN")) {
  483.             jTextField1.setText("0");
  484.         } else if (0 == temp.length() | 1 == temp.length()) {
  485.             jTextField1.setText("0");
  486.         } else if (0 != temp.length()) {
  487.             jTextField1.setText(temp.substring(0, temp.length() - 1));
  488.         }
  489.     }                                        
  490.  
  491.     private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  492.         String temp = jTextField1.getText();
  493.         if (temp.contains(" ")) {
  494.             temp = jTextField1.getText();
  495.             if (operation.contains(" * ")) {
  496.                 operation = " / ";
  497.                 temp = temp.replace(" * ", operation);
  498.                 jTextField1.setText(temp);
  499.             } else if (operation.contains(" + ")) {
  500.                 operation = " / ";
  501.                 temp = temp.replace(" + ", operation);
  502.                 jTextField1.setText(temp);
  503.             } else if (operation.contains(" - ")) {
  504.                 operation = " / ";
  505.                 temp = temp.replace(" - ", operation);
  506.                 jTextField1.setText(temp);
  507.             }
  508.         } else {
  509.             firstNum = Double.valueOf(jTextField1.getText());
  510.             operation = " / ";
  511.             temp = String.valueOf(firstNum);
  512.             if (temp.endsWith(".0")) {
  513.                 temp = temp.substring(0, temp.length() - 2);
  514.                 jTextField1.setText(temp + operation);
  515.             } else {
  516.                 jTextField1.setText(temp + operation);
  517.             }
  518.         }
  519.     }                                            
  520.  
  521.     private void PowButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  522.         String temp = jTextField1.getText();
  523.         if (temp.contains(" ")) {
  524.             temp = jTextField1.getText();
  525.             if (operation.contains(" / ")) {
  526.                 operation = " * ";
  527.                 temp = temp.replace(" / ", operation);
  528.                 jTextField1.setText(temp);
  529.             } else if (operation.contains(" + ")) {
  530.                 operation = " * ";
  531.                 temp = temp.replace(" + ", operation);
  532.                 jTextField1.setText(temp);
  533.             } else if (operation.contains(" - ")) {
  534.                 operation = " * ";
  535.                 temp = temp.replace(" - ", operation);
  536.                 jTextField1.setText(temp);
  537.             }
  538.         } else {
  539.             firstNum = Double.valueOf(jTextField1.getText());
  540.             operation = " * ";
  541.             temp = String.valueOf(firstNum);
  542.             if (temp.endsWith(".0")) {
  543.                 temp = temp.substring(0, temp.length() - 2);
  544.                 jTextField1.setText(temp + operation);
  545.             } else {
  546.                 jTextField1.setText(temp + operation);
  547.             }
  548.         }
  549.     }                                        
  550.  
  551.     private void PlusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  552.         String temp = jTextField1.getText();
  553.         if (temp.contains(" ")) {
  554.             temp = jTextField1.getText();
  555.             if (operation.contains(" * ")) {
  556.                 operation = " + ";
  557.                 temp = temp.replace(" * ", operation);
  558.                 jTextField1.setText(temp);
  559.             } else if (operation.contains(" / ")) {
  560.                 operation = " + ";
  561.                 temp = temp.replace(" / ", operation);
  562.                 jTextField1.setText(temp);
  563.             } else if (operation.contains(" - ")) {
  564.                 operation = " + ";
  565.                 temp = temp.replace(" - ", operation);
  566.                 jTextField1.setText(temp);
  567.             }
  568.         } else {
  569.             firstNum = Double.valueOf(jTextField1.getText());
  570.             operation = " + ";
  571.             temp = String.valueOf(firstNum);
  572.             if (temp.endsWith(".0")) {
  573.                 temp = temp.substring(0, temp.length() - 2);
  574.                 jTextField1.setText(temp + operation);
  575.             } else {
  576.                 jTextField1.setText(temp + operation);
  577.             }
  578.         }
  579.     }                                          
  580.  
  581.     private void MinusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  582.         String temp = jTextField1.getText();
  583.         if (temp.contains(" ")) {
  584.             temp = jTextField1.getText();
  585.             if (operation.contains(" * ")) {
  586.                 operation = " - ";
  587.                 temp = temp.replace(" * ", operation);
  588.                 jTextField1.setText(temp);
  589.             } else if (operation.contains(" + ")) {
  590.                 operation = " - ";
  591.                 temp = temp.replace(" + ", operation);
  592.                 jTextField1.setText(temp);
  593.             } else if (operation.contains(" / ")) {
  594.                 operation = " - ";
  595.                 temp = temp.replace(" / ", operation);
  596.                 jTextField1.setText(temp);
  597.             }
  598.         } else {
  599.             firstNum = Double.valueOf(jTextField1.getText());
  600.             operation = " - ";
  601.             temp = String.valueOf(firstNum);
  602.             if (temp.endsWith(".0")) {
  603.                 temp = temp.substring(0, temp.length() - 2);
  604.                 jTextField1.setText(temp + operation);
  605.             } else {
  606.                 jTextField1.setText(temp + operation);
  607.             }
  608.         }
  609.     }                                          
  610.  
  611.     private void FinalButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  612.         String temp = jTextField1.getText();
  613.         int a = temp.lastIndexOf(" ");
  614.         if (operation.trim().equals("*")) {
  615.             if (temp.contains(" ")) {
  616.                 secondNum = Double.valueOf(temp.substring(a + 1));
  617.                 double answer = firstNum * secondNum;
  618.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  619.                 temp = String.valueOf(answer);
  620.                 if (temp.endsWith(".0")) {
  621.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  622.                 } else {
  623.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  624.                 }
  625.                 jTextField1.setText(temp);
  626.             } else {
  627.                 double numPerOperation = secondNum;
  628.                 double answer = Double.valueOf(temp);
  629.                 answer *= numPerOperation;
  630.                 firstNum = answer;
  631.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  632.                 temp = String.valueOf(answer);
  633.                 if (temp.endsWith(".0")) {
  634.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  635.                 } else {
  636.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  637.                 }
  638.                 jTextField1.setText(temp);
  639.             }
  640.         } else if (operation.trim().equals("/")) {
  641.             if (temp.contains(" ")) {
  642.                 secondNum = Double.valueOf(temp.substring(a + 1));
  643.                 double answer = firstNum / secondNum;
  644.                 temp = String.valueOf(answer);
  645.                 if (temp.equals("NaN") | temp.equals("Infinity")) {
  646.                     jTextField1.setText(temp);
  647.                 } else {
  648.                     answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  649.                     temp = String.valueOf(answer);
  650.                     if (temp.endsWith(".0")) {
  651.                         temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  652.                     } else {
  653.                         temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  654.                     }
  655.                     jTextField1.setText(temp);
  656.                 }
  657.             } else {
  658.                 double numPerOperation = secondNum;
  659.                 double answer = Double.valueOf(temp);
  660.                 answer /= numPerOperation;
  661.                 firstNum = answer;
  662.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  663.                 temp = String.valueOf(answer);
  664.                 if (temp.endsWith(".0")) {
  665.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  666.                 } else {
  667.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  668.                 }
  669.                 jTextField1.setText(temp);
  670.             }
  671.         } else if (operation.trim().equals("-")) {
  672.             if (temp.contains(" ")) {
  673.                 secondNum = Double.valueOf(temp.substring(a + 1));
  674.                 double answer = firstNum - secondNum;
  675.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  676.                 temp = String.valueOf(answer);
  677.                 if (temp.endsWith(".0")) {
  678.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  679.                 } else {
  680.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  681.                 }
  682.                 jTextField1.setText(temp);
  683.             } else {
  684.                 double numPerOperation = secondNum;
  685.                 double answer = Double.valueOf(temp);
  686.                 answer -= numPerOperation;
  687.                 firstNum = answer;
  688.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  689.                 temp = String.valueOf(answer);
  690.                 if (temp.endsWith(".0")) {
  691.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  692.                 } else {
  693.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  694.                 }
  695.                 jTextField1.setText(temp);
  696.             }
  697.         } else if (operation.trim().equals("+")) {
  698.             if (temp.contains(" ")) {
  699.                 secondNum = Double.valueOf(temp.substring(a + 1));
  700.                 double answer = firstNum + secondNum;
  701.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  702.                 temp = String.valueOf(answer);
  703.                 if (temp.endsWith(".0")) {
  704.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  705.                 } else {
  706.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  707.                 }
  708.                 jTextField1.setText(temp);
  709.             } else {
  710.                 double numPerOperation = secondNum;
  711.                 double answer = Double.valueOf(temp);
  712.                 answer += numPerOperation;
  713.                 firstNum = answer;
  714.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  715.                 temp = String.valueOf(answer);
  716.                 if (temp.endsWith(".0")) {
  717.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  718.                 } else {
  719.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  720.                 }
  721.                 jTextField1.setText(temp);
  722.             }
  723.         }
  724.     }                                          
  725.  
  726.     private void PointButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  727.         String temp = jTextField1.getText();
  728.         String secondNumtemp = "";
  729.  
  730.         int a = temp.indexOf(" ");
  731.         int point1 = temp.indexOf(".");
  732.         if (!temp.startsWith(".", point1)) {
  733.             jTextField1.setText(jTextField1.getText() + ".");
  734.         } else if (temp.contains(" ")) {
  735.             if (a != -1) {
  736.                 secondNumtemp = temp.substring(a + 3);
  737.             }
  738.             if (!secondNumtemp.contains(".")) {
  739.                 jTextField1.setText(jTextField1.getText() + ".");
  740.             }
  741.         }
  742.     }                                          
  743.  
  744.     private void CButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
  745.         jTextField1.setText("0");
  746.     }                                      
  747.  
  748.  
  749.     // Variables declaration - do not modify                    
  750.     private javax.swing.JButton Button0;
  751.     private javax.swing.JButton Button1;
  752.     private javax.swing.JButton Button2;
  753.     private javax.swing.JButton Button3;
  754.     private javax.swing.JButton Button4;
  755.     private javax.swing.JButton Button5;
  756.     private javax.swing.JButton Button6;
  757.     private javax.swing.JButton Button7;
  758.     private javax.swing.JButton Button8;
  759.     private javax.swing.JButton Button9;
  760.     private javax.swing.JButton CButton;
  761.     private javax.swing.JButton DivideButton;
  762.     private javax.swing.JButton FinalButton;
  763.     private javax.swing.JButton MinusButton;
  764.     private javax.swing.JButton PlusButton;
  765.     private javax.swing.JButton PointButton;
  766.     private javax.swing.JButton PowButton;
  767.     private javax.swing.JTextField jTextField1;
  768.     // End of variables declaration                  
  769. }
Add Comment
Please, Sign In to add comment