473,750 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java calculator problem ???

9 New Member
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
1 1813
jkmyoung
2,057 Recognized Expert Top Contributor
Instead of btnReciprocal.g etText() you need to be getting the text from the display, eg, txtDisplay.getT ext()
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

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

Similar topics

10
5155
by: Dr. Mercury | last post by:
Greetings to all - Someone suggested that Java might be the answer for a little conversion program I need. It's pretty simple, really. Just take a column of numbers: 139 259 433 637
3
15142
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows calculator. Ive made 4 classes 2 do this, my reasoning so it was easier to look through( when programming) rather than getting mixed up in my own code! My questions and problems: Ive been messing around with the windows look and feel,...
2
2205
by: erekose666 | last post by:
I need some help here if anyone is willing. I need to take this program and modify it to do something a little different. First things first, though. If you run this code like it is right now, it crashes if you enter anything but whole numbers. How can I fix that? Here's the code: import java.util.*; public class MPGCalculator { // main(): application entry point public static void main(String args) { // displaying...
2
7888
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the path, import java.math.*;//*loan calculator import java.text.*;//*formats numbers public class 3 Mortgage loans { // declare class variable array int mortgage calculator
5
2147
by: erekose666 | last post by:
I have this program right now, but I need to modify it to output to the JTextPane. What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add, subtract, multiply, divide symbols need to....just the numbers and the decimal point. I have no idea how to do this. Can anyone help me? Here's the program. /* * Calculator.java * * Created on December 12, 2006, 10:00 PM * Created by Ken...
15
45139
by: colinNeedsJavaHelp | last post by:
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5 The program needs to compute this and display the result as "The result is: 10" I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it. Thanks This is what I have so far:
11
3597
by: LolaT | last post by:
I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message. import java.util.Scanner; public class Calculator { public static void main(String args) {
3
11851
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However, my teacher wants the operators to follow the algebraic order of operations by chaining multiple operations. Such as, 7 + 4 * 2= 15. The operatorListener is the ActionListener for the operator buttons. Thanks for any help you can give me. ...
2
2495
by: jtanz0 | last post by:
Im fairly new to java programming although i have some experience in python. I have to create a calculator program (command line) which takes as input a mathematical expression consisting of decimal numbers and the four basic mathematical operators (+,-,/,*) and correctly handles nested mathematical expressions. Some examples: • 5 + 6 * 2 results in 17 • (5 + 4) * (4 - 2) results in 18 i can do the basics ie taking an input and...
1
13234
by: Techno3000 | last post by:
import java.io.*; import java.awt.*; import hsa.Console; public class Calculator { static Console c; public static void main (String args) throws IOException
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.