Guest User

Untitled

a guest
Sep 3rd, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 38.08 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.         if (!temp.equals("0")) {
  340.             jTextField1.setText(jTextField1.getText() + "7");
  341.         } else {
  342.             jTextField1.setText("7");
  343.         }
  344.     }                                      
  345.  
  346.     private void Button8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  347.         String temp = jTextField1.getText();
  348.         if (!temp.equals("0")) {
  349.             jTextField1.setText(jTextField1.getText() + "8");
  350.         } else {
  351.             jTextField1.setText("8");
  352.         }
  353.     }                                      
  354.  
  355.     private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  356.         String temp = jTextField1.getText();
  357.         if (!temp.equals("0")) {
  358.             jTextField1.setText(jTextField1.getText() + "9");
  359.         } else {
  360.             jTextField1.setText("9");
  361.         }
  362.     }                                      
  363.  
  364.     private void Button0ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  365.         jTextField1.setText(jTextField1.getText() + "0");
  366.         String temp = jTextField1.getText();
  367.         int a = temp.lastIndexOf(" ");        
  368.         if (temp.contains(" ")) {
  369.             if (temp.startsWith("00", a + 1)) {
  370.                 temp = temp.substring(0, temp.length() - 1);
  371.             } else if (temp.startsWith("00", a + 1)) {
  372.                 temp = temp.substring(0, temp.length() - 1);
  373.             } else if (temp.startsWith("00", a + 1)) {
  374.                 temp = temp.substring(0, temp.length() - 1);
  375.             } else if (temp.startsWith("00", a + 1)) {
  376.                 temp = temp.substring(0, temp.length() - 1);
  377.             }
  378.         }
  379.         if (temp.startsWith("00")) {
  380.             temp = temp.substring(0, temp.length() - 1);
  381.         }        
  382.         jTextField1.setText(temp);
  383.     }                                      
  384.  
  385.     private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  386.         String temp = jTextField1.getText();
  387.         if (!temp.equals("0")) {
  388.             jTextField1.setText(jTextField1.getText() + "1");
  389.         } else {
  390.             jTextField1.setText("1");
  391.         }
  392.     }                                      
  393.  
  394.     private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  395.         /*String temp = jTextField1.getText();        
  396.         if (!temp.equals("0")) {
  397.             jTextField1.setText(jTextField1.getText() + "2");
  398.         } else {
  399.             jTextField1.setText("2");
  400.         }*/
  401.                
  402.     }                                      
  403.  
  404.     private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  405.         String temp = jTextField1.getText();
  406.         if (!temp.equals("0")) {
  407.             jTextField1.setText(jTextField1.getText() + "3");
  408.         } else {
  409.             jTextField1.setText("3");
  410.         }
  411.     }                                      
  412.  
  413.     private void Button4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  414.         String temp = jTextField1.getText();
  415.         if (!temp.equals("0")) {
  416.             jTextField1.setText(jTextField1.getText() + "4");
  417.         } else {
  418.             jTextField1.setText("4");
  419.         }
  420.     }                                      
  421.  
  422.     private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  423.         String temp = jTextField1.getText();
  424.         if (!temp.equals("0")) {
  425.             jTextField1.setText(jTextField1.getText() + "5");
  426.         } else {
  427.             jTextField1.setText("5");
  428.         }
  429.     }                                      
  430.  
  431.     private void Button6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  432.         String temp = jTextField1.getText();
  433.         if (!temp.equals("0")) {
  434.             jTextField1.setText(jTextField1.getText() + "6");
  435.         } else {
  436.             jTextField1.setText("6");
  437.         }
  438.     }                                      
  439.  
  440.     private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {                                        
  441.         String temp = jTextField1.getText();
  442.         if (temp.endsWith(" * ") | temp.endsWith(" / ")
  443.                 | temp.endsWith(" + ") | temp.endsWith(" - ")) {
  444.             jTextField1.setText(temp.substring(0, temp.length() - 3));
  445.         } else if (temp.endsWith("Infinity") | temp.endsWith("NaN")) {
  446.             jTextField1.setText("0");
  447.         } else if (0 == temp.length() | 1 == temp.length()) {
  448.             jTextField1.setText("0");
  449.         } else if (0 != temp.length()) {
  450.             jTextField1.setText(temp.substring(0, temp.length() - 1));
  451.         }
  452.     }                                        
  453.  
  454.     private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  455.         String temp = jTextField1.getText();
  456.         if (temp.contains(" ")) {            
  457.             temp = jTextField1.getText();
  458.             if (operation.contains(" * ")) {
  459.                 operation = " / ";
  460.                 temp = temp.replace(" * ", operation);
  461.                 jTextField1.setText(temp);                
  462.             } else if (operation.contains(" + ")) {
  463.                 operation = " / ";
  464.                 temp = temp.replace(" + ", operation);
  465.                 jTextField1.setText(temp);    
  466.             } else if (operation.contains(" - ")) {
  467.                 operation = " / ";
  468.                 temp = temp.replace(" - ", operation);
  469.                 jTextField1.setText(temp);    
  470.             }
  471.         } else {
  472.             firstNum = Double.valueOf(jTextField1.getText());
  473.             operation = " / ";
  474.             temp = String.valueOf(firstNum);
  475.             if (temp.endsWith(".0")) {
  476.                 temp = temp.substring(0, temp.length() - 2);
  477.                 jTextField1.setText(temp + operation);
  478.             } else {
  479.                 jTextField1.setText(temp + operation);
  480.             }
  481.         }
  482.     }                                            
  483.  
  484.     private void PowButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  485.         String temp = jTextField1.getText();
  486.         if (temp.contains(" ")) {            
  487.             temp = jTextField1.getText();
  488.             if (operation.contains(" / ")) {
  489.                 operation = " * ";
  490.                 temp = temp.replace(" / ", operation);
  491.                 jTextField1.setText(temp);                
  492.             } else if (operation.contains(" + ")) {
  493.                 operation = " * ";
  494.                 temp = temp.replace(" + ", operation);
  495.                 jTextField1.setText(temp);    
  496.             } else if (operation.contains(" - ")) {
  497.                 operation = " * ";
  498.                 temp = temp.replace(" - ", operation);
  499.                 jTextField1.setText(temp);    
  500.             }
  501.         } else {
  502.             firstNum = Double.valueOf(jTextField1.getText());
  503.             operation = " * ";
  504.             temp = String.valueOf(firstNum);
  505.             if (temp.endsWith(".0")) {
  506.                 temp = temp.substring(0, temp.length() - 2);
  507.                 jTextField1.setText(temp + operation);
  508.             } else {
  509.                 jTextField1.setText(temp + operation);
  510.             }
  511.         }
  512.     }                                        
  513.  
  514.     private void PlusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  515.         String temp = jTextField1.getText();
  516.         if (temp.contains(" ")) {            
  517.             temp = jTextField1.getText();
  518.             if (operation.contains(" * ")) {
  519.                 operation = " + ";
  520.                 temp = temp.replace(" * ", operation);
  521.                 jTextField1.setText(temp);                
  522.             } else if (operation.contains(" / ")) {
  523.                 operation = " + ";
  524.                 temp = temp.replace(" / ", operation);
  525.                 jTextField1.setText(temp);    
  526.             } else if (operation.contains(" - ")) {
  527.                 operation = " + ";
  528.                 temp = temp.replace(" - ", operation);
  529.                 jTextField1.setText(temp);    
  530.             }
  531.         } else {
  532.             firstNum = Double.valueOf(jTextField1.getText());
  533.             operation = " + ";
  534.             temp = String.valueOf(firstNum);
  535.             if (temp.endsWith(".0")) {
  536.                 temp = temp.substring(0, temp.length() - 2);
  537.                 jTextField1.setText(temp + operation);
  538.             } else {
  539.                 jTextField1.setText(temp + operation);
  540.             }
  541.         }
  542.     }                                          
  543.  
  544.     private void MinusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  545.         String temp = jTextField1.getText();
  546.         if (temp.contains(" ")) {            
  547.             temp = jTextField1.getText();
  548.             if (operation.contains(" * ")) {
  549.                 operation = " - ";
  550.                 temp = temp.replace(" * ", operation);
  551.                 jTextField1.setText(temp);                
  552.             } else if (operation.contains(" + ")) {
  553.                 operation = " - ";
  554.                 temp = temp.replace(" + ", operation);
  555.                 jTextField1.setText(temp);    
  556.             } else if (operation.contains(" / ")) {
  557.                 operation = " - ";
  558.                 temp = temp.replace(" / ", operation);
  559.                 jTextField1.setText(temp);    
  560.             }
  561.         } else {
  562.             firstNum = Double.valueOf(jTextField1.getText());
  563.             operation = " - ";
  564.             temp = String.valueOf(firstNum);
  565.             if (temp.endsWith(".0")) {
  566.                 temp = temp.substring(0, temp.length() - 2);
  567.                 jTextField1.setText(temp + operation);
  568.             } else {
  569.                 jTextField1.setText(temp + operation);
  570.             }
  571.         }
  572.     }                                          
  573.  
  574.     private void FinalButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  575.         String temp = jTextField1.getText();
  576.         int a = temp.lastIndexOf(" ");
  577.         if (operation.trim().equals("*")) {
  578.            if (temp.contains(" ")) {
  579.                 secondNum = Double.valueOf(temp.substring(a + 1));  
  580.                 double answer = firstNum * secondNum;
  581.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  582.                 temp = String.valueOf(answer);
  583.                 if (temp.endsWith(".0")) {
  584.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  585.                 } else {
  586.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  587.                 }
  588.                 jTextField1.setText(temp);              
  589.             } else {                
  590.                 double numPerOperation = secondNum;
  591.                 double answer = Double.valueOf(temp);
  592.                 answer *= numPerOperation;
  593.                 firstNum = answer;
  594.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  595.                 temp = String.valueOf(answer);
  596.                 if (temp.endsWith(".0")) {
  597.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  598.                 } else {
  599.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  600.                 }
  601.                 jTextField1.setText(temp);    
  602.             }            
  603.         } else if (operation.trim().equals("/")) {            
  604.             if (temp.contains(" ")) {
  605.                 secondNum = Double.valueOf(temp.substring(a + 1));  
  606.                 double answer = firstNum / secondNum;
  607.                 temp = String.valueOf(answer);
  608.                 if (temp.equals("NaN") | temp.equals("Infinity")) {                  
  609.                     jTextField1.setText(temp);    
  610.                 } else {
  611.                     answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  612.                     temp = String.valueOf(answer);
  613.                     if (temp.endsWith(".0")) {
  614.                         temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  615.                     } else {
  616.                         temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  617.                     }
  618.                     jTextField1.setText(temp);
  619.                 }                
  620.             } else {                
  621.                 double numPerOperation = secondNum;
  622.                 double answer = Double.valueOf(temp);
  623.                 answer /= numPerOperation;
  624.                 firstNum = answer;
  625.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  626.                 temp = String.valueOf(answer);
  627.                 if (temp.endsWith(".0")) {
  628.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  629.                 } else {
  630.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  631.                 }
  632.                 jTextField1.setText(temp);    
  633.             }            
  634.         } else if (operation.trim().equals("-")) {
  635.            if (temp.contains(" ")) {
  636.                 secondNum = Double.valueOf(temp.substring(a + 1));  
  637.                 double answer = firstNum - secondNum;
  638.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  639.                 temp = String.valueOf(answer);
  640.                 if (temp.endsWith(".0")) {
  641.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  642.                 } else {
  643.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  644.                 }
  645.                 jTextField1.setText(temp);              
  646.             } else {                
  647.                 double numPerOperation = secondNum;
  648.                 double answer = Double.valueOf(temp);
  649.                 answer -= numPerOperation;
  650.                 firstNum = answer;
  651.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  652.                 temp = String.valueOf(answer);
  653.                 if (temp.endsWith(".0")) {
  654.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  655.                 } else {
  656.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  657.                 }
  658.                 jTextField1.setText(temp);    
  659.             }            
  660.         } else if (operation.trim().equals("+")) {
  661.             if (temp.contains(" ")) {
  662.                 secondNum = Double.valueOf(temp.substring(a + 1));  
  663.                 double answer = firstNum + secondNum;
  664.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  665.                 temp = String.valueOf(answer);
  666.                 if (temp.endsWith(".0")) {
  667.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  668.                 } else {
  669.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  670.                 }
  671.                 jTextField1.setText(temp);              
  672.             } else {                
  673.                 double numPerOperation = secondNum;
  674.                 double answer = Double.valueOf(temp);
  675.                 answer += numPerOperation;
  676.                 firstNum = answer;
  677.                 answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
  678.                 temp = String.valueOf(answer);
  679.                 if (temp.endsWith(".0")) {
  680.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
  681.                 } else {
  682.                     temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
  683.                 }
  684.                 jTextField1.setText(temp);    
  685.             }            
  686.         }
  687.     }                                          
  688.  
  689.     private void PointButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  690.         String temp = jTextField1.getText();
  691.         String secondNumtemp = "";
  692.  
  693.         int a = temp.indexOf(" ");
  694.         int point1 = temp.indexOf(".");
  695.         if (!temp.startsWith(".", point1)) {
  696.             jTextField1.setText(jTextField1.getText() + ".");
  697.         } else if (temp.contains(" ")) {
  698.             if (a != -1) {
  699.                 secondNumtemp = temp.substring(a + 3);
  700.             }
  701.             if (!secondNumtemp.contains(".")) {
  702.                 jTextField1.setText(jTextField1.getText() + ".");
  703.             }
  704.         }
  705.     }                                          
  706.  
  707.     private void CButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
  708.         jTextField1.setText("0");
  709.     }                                      
  710.  
  711.     //++Реплейсинг действия
  712.     //управление с клавиатуры
  713.     //удаление задержкой миши
  714.     //автоудаление минуса
  715.     //--замена 0 во втором числе.
  716.     //++округление до 10 знаков
  717.     //++кнопка = теперь повторяет действие, напр 2*2=4 при повторе =8 =16 =32 =64 и тд.
  718.     //++введение нулей во втором числе.    
  719.     //++введение двух точек во втором числе
  720.  
  721.     // Variables declaration - do not modify                    
  722.     private javax.swing.JButton Button0;
  723.     private javax.swing.JButton Button1;
  724.     private javax.swing.JButton Button2;
  725.     private javax.swing.JButton Button3;
  726.     private javax.swing.JButton Button4;
  727.     private javax.swing.JButton Button5;
  728.     private javax.swing.JButton Button6;
  729.     private javax.swing.JButton Button7;
  730.     private javax.swing.JButton Button8;
  731.     private javax.swing.JButton Button9;
  732.     private javax.swing.JButton CButton;
  733.     private javax.swing.JButton DivideButton;
  734.     private javax.swing.JButton FinalButton;
  735.     private javax.swing.JButton MinusButton;
  736.     private javax.swing.JButton PlusButton;
  737.     private javax.swing.JButton PointButton;
  738.     private javax.swing.JButton PowButton;
  739.     private javax.swing.JTextField jTextField1;
  740.     // End of variables declaration                  
  741. }
Advertisement
Add Comment
Please, Sign In to add comment