Results 1 to 3 of 3
Thread: Java calculator problem ???
- 03-13-2010, 05:16 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Java calculator problem ???
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??
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * JavaCalculator.java * * Created on Mar 12, 2010, 2:59:41 PM */ package jCalculator; /** * * @author Owner */ public class JavaCalculator extends javax.swing.JFrame { private double total1 = 0.0; private double total2 = 0.0; private char math_operator; /** Creates new form JavaCalculator */ public JavaCalculator() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); btnSeven = new javax.swing.JButton(); btnEight = new javax.swing.JButton(); btnNine = new javax.swing.JButton(); btnFour = new javax.swing.JButton(); btnOne = new javax.swing.JButton(); btnFive = new javax.swing.JButton(); btnSix = new javax.swing.JButton(); btnTwo = new javax.swing.JButton(); btnThree = new javax.swing.JButton(); btnZero = new javax.swing.JButton(); btnDivide = new javax.swing.JButton(); btnSquareroot = new javax.swing.JButton(); btnMultiply = new javax.swing.JButton(); btnPercentage = new javax.swing.JButton(); btnMinus = new javax.swing.JButton(); btnReciprocal = new javax.swing.JButton(); btnPlus = new javax.swing.JButton(); btnEquals = new javax.swing.JButton(); btnZero1 = new javax.swing.JButton(); btnZero2 = new javax.swing.JButton(); txtDisplay = new javax.swing.JTextField(); btnClear = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Calculator"); btnSeven.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnSeven.setForeground(new java.awt.Color(0, 153, 153)); btnSeven.setText("7"); btnSeven.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSevenActionPerformed(evt); } }); btnEight.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnEight.setForeground(new java.awt.Color(0, 153, 153)); btnEight.setText("8"); btnEight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEightActionPerformed(evt); } }); btnNine.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnNine.setForeground(new java.awt.Color(0, 153, 153)); btnNine.setText("9"); btnNine.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNineActionPerformed(evt); } }); btnFour.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnFour.setForeground(new java.awt.Color(0, 153, 153)); btnFour.setText("4"); btnFour.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFourActionPerformed(evt); } }); btnOne.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnOne.setForeground(new java.awt.Color(0, 153, 153)); btnOne.setText("1"); btnOne.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOneActionPerformed(evt); } }); btnFive.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnFive.setForeground(new java.awt.Color(0, 153, 153)); btnFive.setText("5"); btnFive.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFiveActionPerformed(evt); } }); btnSix.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnSix.setForeground(new java.awt.Color(0, 153, 153)); btnSix.setText("6"); btnSix.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSixActionPerformed(evt); } }); btnTwo.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnTwo.setForeground(new java.awt.Color(0, 153, 153)); btnTwo.setText("2"); btnTwo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnTwoActionPerformed(evt); } }); btnThree.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnThree.setForeground(new java.awt.Color(0, 153, 153)); btnThree.setText("3"); btnThree.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnThreeActionPerformed(evt); } }); btnZero.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnZero.setForeground(new java.awt.Color(0, 153, 153)); btnZero.setText("0"); btnZero.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnZeroActionPerformed(evt); } }); btnDivide.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N btnDivide.setForeground(new java.awt.Color(204, 0, 0)); btnDivide.setText("/"); btnDivide.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDivideActionPerformed(evt); } }); btnSquareroot.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N btnSquareroot.setForeground(new java.awt.Color(0, 153, 153)); btnSquareroot.setText("sqrt"); btnSquareroot.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSquarerootActionPerformed(evt); } }); btnMultiply.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N btnMultiply.setForeground(new java.awt.Color(204, 0, 0)); btnMultiply.setText("*"); btnMultiply.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnMultiplyActionPerformed(evt); } }); btnPercentage.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N btnPercentage.setForeground(new java.awt.Color(0, 153, 153)); btnPercentage.setText("%"); btnMinus.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N btnMinus.setForeground(new java.awt.Color(204, 0, 0)); btnMinus.setText("-"); btnMinus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnMinusActionPerformed(evt); } }); btnReciprocal.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N btnReciprocal.setForeground(new java.awt.Color(0, 153, 153)); btnReciprocal.setText("1/x"); btnReciprocal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnReciprocalActionPerformed(evt); } }); btnPlus.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N btnPlus.setForeground(new java.awt.Color(204, 0, 0)); btnPlus.setText("+"); btnPlus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPlusActionPerformed(evt); } }); btnEquals.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N btnEquals.setForeground(new java.awt.Color(204, 0, 0)); btnEquals.setText("="); btnEquals.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEqualsActionPerformed(evt); } }); btnZero1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N btnZero1.setForeground(new java.awt.Color(0, 153, 153)); btnZero1.setText("-/+"); btnZero1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnZero1ActionPerformed(evt); } }); btnZero2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N btnZero2.setForeground(new java.awt.Color(0, 153, 153)); btnZero2.setText("."); btnZero2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnZero2ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSquareroot, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnPercentage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnZero1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnZero2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnReciprocal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(btnSquareroot, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(btnPercentage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnReciprocal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnZero1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnZero2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); txtDisplay.setEditable(false); txtDisplay.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N btnClear.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N btnClear.setForeground(new java.awt.Color(204, 0, 0)); btnClear.setText("C"); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(176, Short.MAX_VALUE) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtDisplay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); }// </editor-fold> private void getOperator(String btnText) { math_operator = btnText.charAt(0); total1 = total1 + Double.parseDouble(txtDisplay.getText()); txtDisplay.setText(""); } private void btnOneActionPerformed(java.awt.event.ActionEvent evt) { String btnOneText = txtDisplay.getText() + btnOne.getText(); txtDisplay.setText(btnOneText); } private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) { String btnTwoText = txtDisplay.getText() + btnTwo.getText(); txtDisplay.setText(btnTwoText); } private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) { String btnThreeText = txtDisplay.getText() + btnThree.getText(); txtDisplay.setText(btnThreeText); } private void btnFourActionPerformed(java.awt.event.ActionEvent evt) { String btnFourText = txtDisplay.getText() + btnFour.getText(); txtDisplay.setText(btnFourText); } private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) { String btnFiveText = txtDisplay.getText() + btnFive.getText(); txtDisplay.setText(btnFiveText); } private void btnSixActionPerformed(java.awt.event.ActionEvent evt) { String btnSixText = txtDisplay.getText() + btnSix.getText(); txtDisplay.setText(btnSixText); } private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) { String btnSevenText = txtDisplay.getText() + btnSeven.getText(); txtDisplay.setText(btnSevenText); } private void btnEightActionPerformed(java.awt.event.ActionEvent evt) { String btnEightText = txtDisplay.getText() + btnEight.getText(); txtDisplay.setText(btnEightText); } private void btnNineActionPerformed(java.awt.event.ActionEvent evt) { String btnNineText = txtDisplay.getText() + btnNine.getText(); txtDisplay.setText(btnNineText); } private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) { String btnZeroText = txtDisplay.getText() + btnZero.getText(); txtDisplay.setText(btnZeroText); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { total2 = 0; txtDisplay.setText(""); } private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) { switch ( math_operator ) { case '+': total2 = total1 + Double.parseDouble( txtDisplay.getText( ) ) ; break; case '-': total2 = total1 - Double.parseDouble(txtDisplay.getText( ) ); break; case '/': total2 = total1 / Double.parseDouble(txtDisplay.getText( ) ); break; case '*': total2 = total1 * Double.parseDouble(txtDisplay.getText( ) ); break; } txtDisplay.setText( Double.toString(total2) ); total1 = 0; // TODO add your handling code here: } private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) { String button_text = btnPlus.getText(); getOperator(button_text); } private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) { String button_text = btnMinus.getText(); getOperator(button_text); } private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) { String button_text = btnMultiply.getText(); getOperator(button_text); } private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) { String button_text = btnDivide.getText(); getOperator(button_text); } private void btnReciprocalActionPerformed(java.awt.event.ActionEvent evt) { float num,result; num=Float.parseFloat(btnReciprocal.getText()); result = 1/num; btnReciprocal.setText(String.valueOf(result)); } private void btnSquarerootActionPerformed(java.awt.event.ActionEvent evt) { float num,result; num=Float.parseFloat(btnSquareroot.getText()); result=num*num; btnSquareroot.setText(String.valueOf(result)); } private void btnZero1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void btnZero2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JavaCalculator().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnClear; private javax.swing.JButton btnDivide; private javax.swing.JButton btnEight; private javax.swing.JButton btnEquals; private javax.swing.JButton btnFive; private javax.swing.JButton btnFour; private javax.swing.JButton btnMinus; private javax.swing.JButton btnMultiply; private javax.swing.JButton btnNine; private javax.swing.JButton btnOne; private javax.swing.JButton btnPercentage; private javax.swing.JButton btnPlus; private javax.swing.JButton btnReciprocal; private javax.swing.JButton btnSeven; private javax.swing.JButton btnSix; private javax.swing.JButton btnSquareroot; private javax.swing.JButton btnThree; private javax.swing.JButton btnTwo; private javax.swing.JButton btnZero; private javax.swing.JButton btnZero1; private javax.swing.JButton btnZero2; private javax.swing.JPanel jPanel1; private javax.swing.JTextField txtDisplay; // End of variables declaration }Last edited by danielmessick; 03-13-2010 at 06:24 PM.
-
Cross-post: Java calculator problem ??? - Java answers
OP, please read up on the proper etiquette for notifying us about cross-posts here: JavaRanch - Be Forthright When Cross Posting To Other Sites
Your cooperation on this would be greatly appreciated (and the converse is true!).
Best of luck.
-
Also, myself, I would tend to use a single ActionListener for all the number and decimal buttons, and inside this actionlistener have logic that prevents decimal from being added more than once. I'd create another ActionListener for the basic math function keys, perhaps another for the more advanced math function keys, and another one for memory function keys.
Also, when you say things are "not working" you would help us a lot by going into as much detail as possible as to how the buttons are not working.
Also, again, please address the cross-post issue. A simple statement from you that you acknowledge this would suffice. Thanks.
Similar Threads
-
problem Creating Calculator with java and jsp
By mindblaster in forum New To JavaReplies: 0Last Post: 12-21-2009, 11:43 PM -
Time Calculator Problem
By ktisallred in forum New To JavaReplies: 3Last Post: 09-22-2009, 07:15 PM -
Simple Calculator Display Problem :(
By jimbob in forum Java AppletsReplies: 4Last Post: 07-18-2009, 04:13 AM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM -
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07-06-2008, 04:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks