By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,027 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,027 IT Pros & Developers. It's quick & easy.

Java calculator problem ???

P: 9
Hello, I'm trying to fix my Java applet to have a fully functional calculator. I managed to create the buttons and have the +, -, /, *, =, and clear buttons WORKING. But, I cant seem to figure out how to get the decimal(.), squareroot, percent(%), reciprocal(1/x), and -/+ buttons working.. Can anyone help??

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JavaCalculator.java
  8.  *
  9.  * Created on Mar 12, 2010, 2:59:41 PM
  10.  */
  11.  
  12. package jCalculator;
  13.  
  14. /**
  15.  *
  16.  * @author Owner
  17.  */
  18. public class JavaCalculator extends javax.swing.JFrame {
  19.  
  20.     private double total1 = 0.0;
  21.     private double total2 = 0.0;
  22.     private char math_operator;
  23.     /** Creates new form JavaCalculator */
  24.     public JavaCalculator() {
  25.         initComponents();
  26.     }
  27.  
  28.     /** This method is called from within the constructor to
  29.      * initialize the form.
  30.      * WARNING: Do NOT modify this code. The content of this method is
  31.      * always regenerated by the Form Editor.
  32.      */
  33.     @SuppressWarnings("unchecked")
  34.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  35.     private void initComponents() {
  36.  
  37.         jPanel1 = new javax.swing.JPanel();
  38.         btnSeven = new javax.swing.JButton();
  39.         btnEight = new javax.swing.JButton();
  40.         btnNine = new javax.swing.JButton();
  41.         btnFour = new javax.swing.JButton();
  42.         btnOne = new javax.swing.JButton();
  43.         btnFive = new javax.swing.JButton();
  44.         btnSix = new javax.swing.JButton();
  45.         btnTwo = new javax.swing.JButton();
  46.         btnThree = new javax.swing.JButton();
  47.         btnZero = new javax.swing.JButton();
  48.         btnDivide = new javax.swing.JButton();
  49.         btnSquareroot = new javax.swing.JButton();
  50.         btnMultiply = new javax.swing.JButton();
  51.         btnPercentage = new javax.swing.JButton();
  52.         btnMinus = new javax.swing.JButton();
  53.         btnReciprocal = new javax.swing.JButton();
  54.         btnPlus = new javax.swing.JButton();
  55.         btnEquals = new javax.swing.JButton();
  56.         btnZero1 = new javax.swing.JButton();
  57.         btnZero2 = new javax.swing.JButton();
  58.         txtDisplay = new javax.swing.JTextField();
  59.         btnClear = new javax.swing.JButton();
  60.  
  61.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  62.         setTitle("Calculator");
  63.  
  64.         btnSeven.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  65.         btnSeven.setForeground(new java.awt.Color(0, 153, 153));
  66.         btnSeven.setText("7");
  67.         btnSeven.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 btnSevenActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         btnEight.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  74.         btnEight.setForeground(new java.awt.Color(0, 153, 153));
  75.         btnEight.setText("8");
  76.         btnEight.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 btnEightActionPerformed(evt);
  79.             }
  80.         });
  81.  
  82.         btnNine.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  83.         btnNine.setForeground(new java.awt.Color(0, 153, 153));
  84.         btnNine.setText("9");
  85.         btnNine.addActionListener(new java.awt.event.ActionListener() {
  86.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  87.                 btnNineActionPerformed(evt);
  88.             }
  89.         });
  90.  
  91.         btnFour.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  92.         btnFour.setForeground(new java.awt.Color(0, 153, 153));
  93.         btnFour.setText("4");
  94.         btnFour.addActionListener(new java.awt.event.ActionListener() {
  95.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  96.                 btnFourActionPerformed(evt);
  97.             }
  98.         });
  99.  
  100.         btnOne.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  101.         btnOne.setForeground(new java.awt.Color(0, 153, 153));
  102.         btnOne.setText("1");
  103.         btnOne.addActionListener(new java.awt.event.ActionListener() {
  104.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  105.                 btnOneActionPerformed(evt);
  106.             }
  107.         });
  108.  
  109.         btnFive.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  110.         btnFive.setForeground(new java.awt.Color(0, 153, 153));
  111.         btnFive.setText("5");
  112.         btnFive.addActionListener(new java.awt.event.ActionListener() {
  113.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  114.                 btnFiveActionPerformed(evt);
  115.             }
  116.         });
  117.  
  118.         btnSix.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  119.         btnSix.setForeground(new java.awt.Color(0, 153, 153));
  120.         btnSix.setText("6");
  121.         btnSix.addActionListener(new java.awt.event.ActionListener() {
  122.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.                 btnSixActionPerformed(evt);
  124.             }
  125.         });
  126.  
  127.         btnTwo.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  128.         btnTwo.setForeground(new java.awt.Color(0, 153, 153));
  129.         btnTwo.setText("2");
  130.         btnTwo.addActionListener(new java.awt.event.ActionListener() {
  131.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  132.                 btnTwoActionPerformed(evt);
  133.             }
  134.         });
  135.  
  136.         btnThree.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  137.         btnThree.setForeground(new java.awt.Color(0, 153, 153));
  138.         btnThree.setText("3");
  139.         btnThree.addActionListener(new java.awt.event.ActionListener() {
  140.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  141.                 btnThreeActionPerformed(evt);
  142.             }
  143.         });
  144.  
  145.         btnZero.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  146.         btnZero.setForeground(new java.awt.Color(0, 153, 153));
  147.         btnZero.setText("0");
  148.         btnZero.addActionListener(new java.awt.event.ActionListener() {
  149.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  150.                 btnZeroActionPerformed(evt);
  151.             }
  152.         });
  153.  
  154.         btnDivide.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  155.         btnDivide.setForeground(new java.awt.Color(204, 0, 0));
  156.         btnDivide.setText("/");
  157.         btnDivide.addActionListener(new java.awt.event.ActionListener() {
  158.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  159.                 btnDivideActionPerformed(evt);
  160.             }
  161.         });
  162.  
  163.         btnSquareroot.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
  164.         btnSquareroot.setForeground(new java.awt.Color(0, 153, 153));
  165.         btnSquareroot.setText("sqrt");
  166.         btnSquareroot.addActionListener(new java.awt.event.ActionListener() {
  167.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  168.                 btnSquarerootActionPerformed(evt);
  169.             }
  170.         });
  171.  
  172.         btnMultiply.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  173.         btnMultiply.setForeground(new java.awt.Color(204, 0, 0));
  174.         btnMultiply.setText("*");
  175.         btnMultiply.addActionListener(new java.awt.event.ActionListener() {
  176.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  177.                 btnMultiplyActionPerformed(evt);
  178.             }
  179.         });
  180.  
  181.         btnPercentage.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
  182.         btnPercentage.setForeground(new java.awt.Color(0, 153, 153));
  183.         btnPercentage.setText("%");
  184.  
  185.         btnMinus.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
  186.         btnMinus.setForeground(new java.awt.Color(204, 0, 0));
  187.         btnMinus.setText("-");
  188.         btnMinus.addActionListener(new java.awt.event.ActionListener() {
  189.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  190.                 btnMinusActionPerformed(evt);
  191.             }
  192.         });
  193.  
  194.         btnReciprocal.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
  195.         btnReciprocal.setForeground(new java.awt.Color(0, 153, 153));
  196.         btnReciprocal.setText("1/x");
  197.         btnReciprocal.addActionListener(new java.awt.event.ActionListener() {
  198.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  199.                 btnReciprocalActionPerformed(evt);
  200.             }
  201.         });
  202.  
  203.         btnPlus.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
  204.         btnPlus.setForeground(new java.awt.Color(204, 0, 0));
  205.         btnPlus.setText("+");
  206.         btnPlus.addActionListener(new java.awt.event.ActionListener() {
  207.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  208.                 btnPlusActionPerformed(evt);
  209.             }
  210.         });
  211.  
  212.         btnEquals.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
  213.         btnEquals.setForeground(new java.awt.Color(204, 0, 0));
  214.         btnEquals.setText("=");
  215.         btnEquals.addActionListener(new java.awt.event.ActionListener() {
  216.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  217.                 btnEqualsActionPerformed(evt);
  218.             }
  219.         });
  220.  
  221.         btnZero1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  222.         btnZero1.setForeground(new java.awt.Color(0, 153, 153));
  223.         btnZero1.setText("-/+");
  224.         btnZero1.addActionListener(new java.awt.event.ActionListener() {
  225.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  226.                 btnZero1ActionPerformed(evt);
  227.             }
  228.         });
  229.  
  230.         btnZero2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  231.         btnZero2.setForeground(new java.awt.Color(0, 153, 153));
  232.         btnZero2.setText(".");
  233.         btnZero2.addActionListener(new java.awt.event.ActionListener() {
  234.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  235.                 btnZero2ActionPerformed(evt);
  236.             }
  237.         });
  238.  
  239.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  240.         jPanel1.setLayout(jPanel1Layout);
  241.         jPanel1Layout.setHorizontalGroup(
  242.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  243.             .addGroup(jPanel1Layout.createSequentialGroup()
  244.                 .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  245.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  246.                 .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  247.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  248.                 .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  249.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  250.                 .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  251.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  252.                 .addComponent(btnSquareroot, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  253.             .addGroup(jPanel1Layout.createSequentialGroup()
  254.                 .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  255.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  256.                 .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  257.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  258.                 .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  259.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  260.                 .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  261.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  262.                 .addComponent(btnPercentage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  263.             .addGroup(jPanel1Layout.createSequentialGroup()
  264.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  265.                     .addGroup(jPanel1Layout.createSequentialGroup()
  266.                         .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  267.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  268.                         .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  269.                     .addGroup(jPanel1Layout.createSequentialGroup()
  270.                         .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  271.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  272.                         .addComponent(btnZero1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
  273.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  275.                     .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  276.                     .addComponent(btnZero2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  277.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  278.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  279.                     .addGroup(jPanel1Layout.createSequentialGroup()
  280.                         .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  281.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  282.                         .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  283.                     .addGroup(jPanel1Layout.createSequentialGroup()
  284.                         .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  285.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  286.                         .addComponent(btnReciprocal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
  287.         );
  288.         jPanel1Layout.setVerticalGroup(
  289.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  290.             .addGroup(jPanel1Layout.createSequentialGroup()
  291.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  292.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  293.                         .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  294.                         .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  295.                         .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  296.                         .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  297.                     .addComponent(btnSquareroot, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  298.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  299.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  300.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  301.                         .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  302.                         .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  303.                         .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  304.                         .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  305.                     .addComponent(btnPercentage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  306.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  307.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  308.                     .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  309.                     .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  310.                     .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  311.                     .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  312.                     .addComponent(btnReciprocal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  313.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  314.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  315.                     .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  316.                     .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  317.                     .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  318.                     .addComponent(btnZero1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  319.                     .addComponent(btnZero2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  320.                 .addContainerGap())
  321.         );
  322.  
  323.         txtDisplay.setEditable(false);
  324.         txtDisplay.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
  325.  
  326.         btnClear.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
  327.         btnClear.setForeground(new java.awt.Color(204, 0, 0));
  328.         btnClear.setText("C");
  329.         btnClear.addActionListener(new java.awt.event.ActionListener() {
  330.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  331.                 btnClearActionPerformed(evt);
  332.             }
  333.         });
  334.  
  335.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  336.         getContentPane().setLayout(layout);
  337.         layout.setHorizontalGroup(
  338.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  339.             .addGroup(layout.createSequentialGroup()
  340.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  341.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  342.                         .addContainerGap(176, Short.MAX_VALUE)
  343.                         .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
  344.                     .addGroup(layout.createSequentialGroup()
  345.                         .addContainerGap()
  346.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  347.                             .addComponent(txtDisplay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
  348.                             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  349.                 .addContainerGap())
  350.         );
  351.         layout.setVerticalGroup(
  352.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  353.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  354.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  355.                 .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  356.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  357.                 .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
  358.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  359.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  360.         );
  361.  
  362.         pack();
  363.     }// </editor-fold>
  364.  
  365.     private void getOperator(String btnText) {
  366.         math_operator = btnText.charAt(0);
  367.         total1 = total1 + Double.parseDouble(txtDisplay.getText());
  368.         txtDisplay.setText("");
  369.     }
  370.  
  371.  
  372.     private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {
  373.     String btnOneText = txtDisplay.getText() + btnOne.getText();
  374.     txtDisplay.setText(btnOneText);
  375.     }
  376.  
  377.     private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {
  378.     String btnTwoText = txtDisplay.getText() + btnTwo.getText();
  379.     txtDisplay.setText(btnTwoText);
  380.     }
  381.  
  382.     private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {
  383.     String btnThreeText = txtDisplay.getText() + btnThree.getText();
  384.     txtDisplay.setText(btnThreeText);
  385.     }
  386.  
  387.     private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {
  388.     String btnFourText = txtDisplay.getText() + btnFour.getText();
  389.     txtDisplay.setText(btnFourText);
  390.     }
  391.  
  392.     private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {
  393.     String btnFiveText = txtDisplay.getText() + btnFive.getText();
  394.     txtDisplay.setText(btnFiveText);
  395.     }
  396.  
  397.     private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {
  398.     String btnSixText = txtDisplay.getText() + btnSix.getText();
  399.     txtDisplay.setText(btnSixText);
  400.     }
  401.  
  402.     private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) {
  403.     String btnSevenText = txtDisplay.getText() + btnSeven.getText();
  404.     txtDisplay.setText(btnSevenText);
  405.     }
  406.  
  407.     private void btnEightActionPerformed(java.awt.event.ActionEvent evt) {
  408.     String btnEightText = txtDisplay.getText() + btnEight.getText();
  409.     txtDisplay.setText(btnEightText);
  410.     }
  411.  
  412.     private void btnNineActionPerformed(java.awt.event.ActionEvent evt) {
  413.     String btnNineText = txtDisplay.getText() + btnNine.getText();
  414.     txtDisplay.setText(btnNineText);
  415.     }
  416.  
  417.     private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) {
  418.     String btnZeroText = txtDisplay.getText() + btnZero.getText();
  419.     txtDisplay.setText(btnZeroText);
  420.     }
  421.  
  422.     private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
  423.     total2 = 0;
  424. txtDisplay.setText("");
  425.     }
  426.  
  427.     private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {
  428.  
  429.         switch ( math_operator ) {
  430.             case '+':
  431.                 total2 = total1 + Double.parseDouble( txtDisplay.getText( ) ) ;
  432.                 break;
  433.             case '-':
  434.                 total2 = total1 - Double.parseDouble(txtDisplay.getText( ) );
  435.                 break;
  436.             case '/':
  437.                 total2 = total1 / Double.parseDouble(txtDisplay.getText( ) );
  438.                 break;
  439.             case '*':
  440.                 total2 = total1 * Double.parseDouble(txtDisplay.getText( ) );
  441.                 break;
  442.  
  443.         }
  444.  
  445.         txtDisplay.setText( Double.toString(total2) );
  446.         total1 = 0;    // TODO add your handling code here:
  447. }
  448.  
  449.     private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {
  450.         String button_text = btnPlus.getText();
  451.         getOperator(button_text);
  452. }
  453.  
  454.     private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) {
  455.         String button_text = btnMinus.getText();
  456.         getOperator(button_text);
  457. }
  458.  
  459.     private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
  460.         String button_text = btnMultiply.getText();
  461.         getOperator(button_text);
  462. }
  463.  
  464.     private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {
  465.         String button_text = btnDivide.getText();
  466.         getOperator(button_text);
  467. }
  468.  
  469.     private void btnReciprocalActionPerformed(java.awt.event.ActionEvent evt) {
  470.  
  471.     float num,result;
  472.     num=Float.parseFloat(btnReciprocal.getText());
  473.     result = 1/num;
  474.     btnReciprocal.setText(String.valueOf(result));
  475.  
  476.     }
  477.  
  478.     private void btnSquarerootActionPerformed(java.awt.event.ActionEvent evt) {
  479.         float num,result;
  480.  
  481.     num=Float.parseFloat(btnSquareroot.getText());
  482.     result=num*num;
  483.     btnSquareroot.setText(String.valueOf(result));
  484.     }
  485.  
  486.     private void btnZero1ActionPerformed(java.awt.event.ActionEvent evt) {
  487.         // TODO add your handling code here:
  488.     }
  489.  
  490.     private void btnZero2ActionPerformed(java.awt.event.ActionEvent evt) {
  491.         // TODO add your handling code here:
  492.     }
  493.  
  494.     /**
  495.     * @param args the command line arguments
  496.     */
  497.     public static void main(String args[]) {
  498.         java.awt.EventQueue.invokeLater(new Runnable() {
  499.             public void run() {
  500.                 new JavaCalculator().setVisible(true);
  501.             }
  502.         });
  503.     }
  504.  
  505.     // Variables declaration - do not modify
  506.     private javax.swing.JButton btnClear;
  507.     private javax.swing.JButton btnDivide;
  508.     private javax.swing.JButton btnEight;
  509.     private javax.swing.JButton btnEquals;
  510.     private javax.swing.JButton btnFive;
  511.     private javax.swing.JButton btnFour;
  512.     private javax.swing.JButton btnMinus;
  513.     private javax.swing.JButton btnMultiply;
  514.     private javax.swing.JButton btnNine;
  515.     private javax.swing.JButton btnOne;
  516.     private javax.swing.JButton btnPercentage;
  517.     private javax.swing.JButton btnPlus;
  518.     private javax.swing.JButton btnReciprocal;
  519.     private javax.swing.JButton btnSeven;
  520.     private javax.swing.JButton btnSix;
  521.     private javax.swing.JButton btnSquareroot;
  522.     private javax.swing.JButton btnThree;
  523.     private javax.swing.JButton btnTwo;
  524.     private javax.swing.JButton btnZero;
  525.     private javax.swing.JButton btnZero1;
  526.     private javax.swing.JButton btnZero2;
  527.     private javax.swing.JPanel jPanel1;
  528.     private javax.swing.JTextField txtDisplay;
  529.     // End of variables declaration
  530.  
  531. }
  532.  
  533.  
Mar 13 '10 #1
Share this Question
Share on Google+
1 Reply


jkmyoung
Expert 100+
P: 2,057
Instead of btnReciprocal.getText() you need to be getting the text from the display, eg, txtDisplay.getText()
It's pretty much a global replace to only get the text from the display, and only set the text to the display, not the buttons themselves.
Mar 15 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.