Results 1 to 7 of 7
  1. #1
    isayblank is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Help with making a windows like calculator!

    this is working but I have some problems. This should supposed to work like the windows calculator

    say, I input "2+3*4/3" (without using the equals sign)...It will not give me the correct result.
    Another problem is that, for example you type "2 +" then you decided to use "-" instead of "+", the first operation won't cancel.. Help please. Thanks.

    PHP Code:
    package javaapplication1;
    
    
    public class ScientificCalculator extends javax.swing.JDialog {
        private int perform = 1;
        private double total1 = 0;
        private double total2 = 0;
        private double extra = 1;
        private double extra2 = 1;
        private int clear = 0;
        private int test = 0;
        private char operation = ' ';
    
    
        public ScientificCalculator(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
         public void getOperation(String btn){
            operation = btn.charAt(0);
    
            if(operation=='+'){
                total1=total1+Double.parseDouble(txt1.getText());
                txt1.setText(Double.toString(total1));
            }
            if(operation=='-'){
                total1=total1+Double.parseDouble(txt1.getText());
                txt1.setText(Double.toString(total1));
            }
            if(operation=='*'){
                extra=extra*Double.parseDouble(txt1.getText());
                txt1.setText(Double.toString(extra));
            }
            if(operation=='/'){
                extra=Double.parseDouble(txt1.getText())/extra;
                txt1.setText(Double.toString(extra));
            }
        }
    
    
    
        /** 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">//GEN-BEGIN:initComponents
        private void initComponents() {
    
            jButton2 = new javax.swing.JButton();
            txt1 = new javax.swing.JTextField();
            btnSeven = new javax.swing.JButton();
            btnEight = new javax.swing.JButton();
            btnNine = new javax.swing.JButton();
            btnFour = new javax.swing.JButton();
            btnFive = new javax.swing.JButton();
            btnSix = new javax.swing.JButton();
            btnOne = new javax.swing.JButton();
            btnTwo = new javax.swing.JButton();
            btnThree = new javax.swing.JButton();
            btnZero = new javax.swing.JButton();
            jButton12 = new javax.swing.JButton();
            btnDecimal = new javax.swing.JButton();
            btnMultiply = new javax.swing.JButton();
            btnDivide = new javax.swing.JButton();
            btnAdd = new javax.swing.JButton();
            btnMinus = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();
            btnClear = new javax.swing.JButton();
            btnEquals = new javax.swing.JButton();
    
            jButton2.setText("jButton1");
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            txt1.setFont(new java.awt.Font("Tahoma", 0, 24));
            txt1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
            txt1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txt1ActionPerformed(evt);
                }
            });
    
            btnSeven.setText("7");
            btnSeven.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSevenActionPerformed(evt);
                }
            });
    
            btnEight.setText("8");
            btnEight.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEightActionPerformed(evt);
                }
            });
    
            btnNine.setText("9");
            btnNine.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnNineActionPerformed(evt);
                }
            });
    
            btnFour.setText("4");
            btnFour.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnFourActionPerformed(evt);
                }
            });
    
            btnFive.setText("5");
            btnFive.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnFiveActionPerformed(evt);
                }
            });
    
            btnSix.setText("6");
            btnSix.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSixActionPerformed(evt);
                }
            });
    
            btnOne.setText("1");
            btnOne.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnOneActionPerformed(evt);
                }
            });
    
            btnTwo.setText("2");
            btnTwo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnTwoActionPerformed(evt);
                }
            });
    
            btnThree.setText("3");
            btnThree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnThreeActionPerformed(evt);
                }
            });
    
            btnZero.setText("0");
            btnZero.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnZeroActionPerformed(evt);
                }
            });
    
            jButton12.setText("jButton12");
    
            btnDecimal.setText(".");
            btnDecimal.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDecimalActionPerformed(evt);
                }
            });
    
            btnMultiply.setText("*");
            btnMultiply.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnMultiplyActionPerformed(evt);
                }
            });
    
            btnDivide.setText("/");
            btnDivide.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDivideActionPerformed(evt);
                }
            });
    
            btnAdd.setText("+");
            btnAdd.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddActionPerformed(evt);
                }
            });
    
            btnMinus.setText("-");
            btnMinus.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnMinusActionPerformed(evt);
                }
            });
    
            jButton6.setText("=");
    
            btnClear.setText("C");
            btnClear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnClearActionPerformed(evt);
                }
            });
    
            btnEquals.setText("=");
            btnEquals.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEqualsActionPerformed(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(layout.createSequentialGroup()
                            .addGap(119, 119, 119)
                            .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnSeven)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnEight)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnNine))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnFour)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnFive)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnSix))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(btnZero, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(btnOne)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(btnTwo)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnDecimal)
                                        .addComponent(btnThree))))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(btnAdd, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addComponent(btnMultiply, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(btnEquals, 0, 0, Short.MAX_VALUE)
                                        .addComponent(btnMinus, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addComponent(btnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                                    .addGap(33, 33, 33)
                                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(44, 44, 44)
                                    .addComponent(btnClear))))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(22, 22, 22))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnNine, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnSix, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnTwo, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                                    .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                                .addComponent(btnMultiply, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnMinus, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(btnEquals, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnZero, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(btnDecimal, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
                    .addGap(18, 18, 18)
                    .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>//GEN-END:initComponents
    
    
    
    
        private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThreeActionPerformed
            // TODO add your handling code here:
           if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Three = txt1.getText() + btnThree.getText();
    txt1.setText(Three);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnThreeActionPerformed
    
        private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFourActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Four = txt1.getText() + btnFour.getText();
    txt1.setText(Four);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnFourActionPerformed
    
        private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZeroActionPerformed
            // TODO add your handling code here:
              if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Zero = txt1.getText() + btnZero.getText();
    txt1.setText(Zero);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnZeroActionPerformed
    
        private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOneActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String One = txt1.getText() + btnOne.getText();
    txt1.setText(One);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnOneActionPerformed
    
        private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTwoActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Two = txt1.getText() + btnTwo.getText();
    txt1.setText(Two);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnTwoActionPerformed
    
        private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiveActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Five = txt1.getText() + btnFive.getText();
    txt1.setText(Five);
    clear=0;
    
        }//GEN-LAST:event_btnFiveActionPerformed
    
        private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSixActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
            else if (clear==1){
        txt1.setText("");
    }
            txt1.getText();
            String Six = txt1.getText() + btnSix.getText();
        txt1.setText(Six);
        clear=0;
        test = 0;
    
        }//GEN-LAST:event_btnSixActionPerformed
    
        private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSevenActionPerformed
            // TODO add your handling code here:
           if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }
    txt1.getText();
    String Seven = txt1.getText() + btnSeven.getText();
    txt1.setText(Seven);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnSevenActionPerformed
    
        private void btnEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEightActionPerformed
            // TODO add your handling code here:
           if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }
    else txt1.getText();
    String Eight = txt1.getText() + btnEight.getText();
    txt1.setText(Eight);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnEightActionPerformed
    
        private void btnNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNineActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
    if (clear==1){
        txt1.setText("");
    }txt1.getText();
    String Nine = txt1.getText() + btnNine.getText();
    txt1.setText(Nine);
    clear=0;
    test = 0;
    
        }//GEN-LAST:event_btnNineActionPerformed
    
        private void btnDecimalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDecimalActionPerformed
            // TODO add your handling code here:
            if (test>=1){
        txt1.setText("");
    }
     if (clear==1){
        txt1.setText("");
    }
    
    
    txt1.getText();
    
    String Point = txt1.getText() + btnDecimal.getText();
    txt1.setText(Point);
    
    clear = 0;
    test = 0;
    
            
        }//GEN-LAST:event_btnDecimalActionPerformed
    
        private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
            // TODO add your handling code here:
            txt1.setText("0");
            perform = 1;
            total1 = 0;
            total2 = 0;
            extra = 1;
            extra2 = 1;
            clear = 0;
            test = 0;
            operation = ' ';
            clear=1;
    
        }//GEN-LAST:event_btnClearActionPerformed
    
        private void txt1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt1ActionPerformed
            // TODO add your handling code here:
            txt1.setText("");
            clear = 1;
    
    
    
    
    
    
        }//GEN-LAST:event_txt1ActionPerformed
    
        private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
            // TODO add your handling code here:
            String btn = btnAdd.getText();
            getOperation(btn);
            test = test + 1;
    
        }//GEN-LAST:event_btnAddActionPerformed
    
        private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEqualsActionPerformed
            // TODO add your handling code here:
            if(operation=='+'){
    
        total2=total1+Double.parseDouble(txt1.getText());
    
        txt1.setText(Double.toString(total2));
        total1=0;
    }
    if(operation=='-'){
    
        total2=total1-Double.parseDouble(txt1.getText());
    
        txt1.setText(Double.toString(total2));
        total1=0;
    }
    if(operation=='*'){
    
        extra2=extra*Double.parseDouble(txt1.getText());
    
        txt1.setText(Double.toString(extra2));
        extra=1;
    }
    if(operation=='/'){
    
        extra2=extra/Double.parseDouble(txt1.getText());
    
        txt1.setText(Double.toString(extra2));
        extra=1;
            }
        }//GEN-LAST:event_btnEqualsActionPerformed
    
        private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDivideActionPerformed
            // TODO add your handling code here:
            String btn = btnDivide.getText();
            getOperation(btn);
            test = test + 1;
    
        }//GEN-LAST:event_btnDivideActionPerformed
    
        private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMultiplyActionPerformed
            // TODO add your handling code here:
            String btn = btnMultiply.getText();
            getOperation(btn);
            test = test + 1;
    
        }//GEN-LAST:event_btnMultiplyActionPerformed
    
        private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMinusActionPerformed
            // TODO add your handling code here:
            String btn = btnMinus.getText();
            getOperation(btn);
            test = test + 1;
    
        }//GEN-LAST:event_btnMinusActionPerformed
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    ScientificCalculator dialog = new ScientificCalculator(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton btnAdd;
        private javax.swing.JButton btnClear;
        private javax.swing.JButton btnDecimal;
        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 btnSeven;
        private javax.swing.JButton btnSix;
        private javax.swing.JButton btnThree;
        private javax.swing.JButton btnTwo;
        private javax.swing.JButton btnZero;
        private javax.swing.JButton jButton12;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton6;
        private javax.swing.JTextField txt1;
        // End of variables declaration//GEN-END:variables
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Help with making a windows like calculator!

    That's a lot of code and logic to work through.
    Have you tried debugging it? Add lots of println statements that print out the values of the variables that control the logic and to show where the execution flow goes so that you can see what the computer sees when the code is executed.

    Also there is a lot of very poorly formatted code(statements all start in column 1 and are not indented) that should be properly formatted if you want anyone to try to read your code.
    Last edited by Norm; 09-22-2012 at 04:15 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with making a windows like calculator!

    You have way too much redundancy in that code and it is just asking for you to use arrays or collections such as an ArrayList with for loops to simplify the code and make it much easier to understand (especially for us!), modify and debug. Having said that, are you using stacks to hold the entered numbers and operations? If not, I would suggest that you consider doing that, where you would push the number onto the number stack and the operation onto the operation stack. Then if you want to change the operation, simply pop the bad operation and push the good one.

    Also your program is one very large class that encompasses everything in it, also known as a "god" class. This type of design can lead to lots of difficult to debug errors making it hard to debug, enhance or modify your program. You will want to refactor this code by separating out your program logic from your GUI code, as this will pay you tremendous dividends when trying to enhance it as you're doing now.
    Last edited by Fubarable; 09-22-2012 at 04:21 PM.

  4. #4
    isayblank is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with making a windows like calculator!

    Here's my modified version. It's shorter but I think it became worse. Since I changed the codes, the operators won't act like the equals sign too. And same problems written above still exists.

    Java Code:
    package calculator;
    
    
    public class myCalculator extends javax.swing.JDialog {
        double firstNum;
        double secondNum;
        double total;
        double plusminus;
        double sqrt;
        double percent;
    
        int plusClick;
        int minusClick;
        int multiplyClick;
        int divideClick;
        int decimalClick;
        
        public String memory;
        /** Creates new form myCalculator */
        public myCalculator(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            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() {
    
            buttonGroup1 = new javax.swing.ButtonGroup();
            btn0 = new javax.swing.JButton();
            btn1 = new javax.swing.JButton();
            btn4 = new javax.swing.JButton();
            btn7 = new javax.swing.JButton();
            btnPosNeg = new javax.swing.JButton();
            btn2 = new javax.swing.JButton();
            btn5 = new javax.swing.JButton();
            btn8 = new javax.swing.JButton();
            btnDecimal = new javax.swing.JButton();
            btn3 = new javax.swing.JButton();
            btn6 = new javax.swing.JButton();
            btn9 = new javax.swing.JButton();
            btnAdd = new javax.swing.JButton();
            btnSub = new javax.swing.JButton();
            btnMul = new javax.swing.JButton();
            btnDiv = new javax.swing.JButton();
            btnEquals = new javax.swing.JButton();
            btnSqrt = new javax.swing.JButton();
            txtDisplay = new javax.swing.JTextField();
            btnC = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            btn0.setText("0");
            btn0.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn0ActionPerformed(evt);
                }
            });
    
            btn1.setText("1");
            btn1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn1ActionPerformed(evt);
                }
            });
    
            btn4.setText("4");
            btn4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn4ActionPerformed(evt);
                }
            });
    
            btn7.setText("7");
            btn7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn7ActionPerformed(evt);
                }
            });
    
            btnPosNeg.setText("+/-");
            btnPosNeg.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnPosNegActionPerformed(evt);
                }
            });
    
            btn2.setText("2");
            btn2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn2ActionPerformed(evt);
                }
            });
    
            btn5.setText("5");
            btn5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn5ActionPerformed(evt);
                }
            });
    
            btn8.setText("8");
            btn8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn8ActionPerformed(evt);
                }
            });
    
            btnDecimal.setText(".");
            btnDecimal.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDecimalActionPerformed(evt);
                }
            });
    
            btn3.setText("3");
            btn3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn3ActionPerformed(evt);
                }
            });
    
            btn6.setText("6");
            btn6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn6ActionPerformed(evt);
                }
            });
    
            btn9.setText("9");
            btn9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn9ActionPerformed(evt);
                }
            });
    
            btnAdd.setText("+");
            btnAdd.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddActionPerformed(evt);
                }
            });
    
            btnSub.setText("-");
            btnSub.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSubActionPerformed(evt);
                }
            });
    
            btnMul.setText("*");
            btnMul.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnMulActionPerformed(evt);
                }
            });
    
            btnDiv.setText("/");
            btnDiv.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDivActionPerformed(evt);
                }
            });
    
            btnEquals.setText("=");
            btnEquals.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEqualsActionPerformed(evt);
                }
            });
    
            btnSqrt.setText("sqrt");
            btnSqrt.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSqrtActionPerformed(evt);
                }
            });
    
            txtDisplay.setEditable(false);
            txtDisplay.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtDisplayActionPerformed(evt);
                }
            });
    
            btnC.setText("C");
            btnC.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCActionPerformed(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.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnPosNeg, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnDecimal, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnMul, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnSub, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnSqrt, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(43, 43, 43)
                            .addComponent(txtDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))
                    .addContainerGap(55, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnSqrt, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(82, 82, 82)
                            .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnMul, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnSub, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnPosNeg, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnDecimal, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(61, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
    private void btnDivActionPerformed(java.awt.event.ActionEvent evt) {                                       
         firstNum=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
         txtDisplay.setText("");
         divideClick=1;
         decimalClick=0;
    
         multiplyClick=0;
         plusClick=0;
         minusClick=0;
         
    
         
    // TODO add your handling code here:
    }                                      
    
    private void btnMulActionPerformed(java.awt.event.ActionEvent evt) {                                       
         firstNum=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
         txtDisplay.setText("");
         multiplyClick=1;
         decimalClick=0;
    
         divideClick=0;
         plusClick=0;
         minusClick=0;
         
    
        // TODO add your handling code here:
    }                                      
    
    private void btnSubActionPerformed(java.awt.event.ActionEvent evt) {                                       
         firstNum=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
         txtDisplay.setText("");
         minusClick=1;
         decimalClick=0;
    
         multiplyClick=0;
         plusClick=0;
         divideClick=0;
         
         
     
    
         // TODO add your handling code here:
    }                                      
    
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
         firstNum=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
         txtDisplay.setText("");
         plusClick=1;
         decimalClick=0;
    
         multiplyClick=0;
         divideClick=0;
         minusClick=0;
         
    
        
    }                                      
    
    private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          
         secondNum=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
         if(plusClick>0){
         total = firstNum + secondNum;
         txtDisplay.setText(String.valueOf(total));
         firstNum = 0;
         secondNum = 0;
         plusClick = 0;
    
         }
    
    
         if(minusClick>0){
         total = firstNum - secondNum;
         txtDisplay.setText(String.valueOf(total));
         firstNum = 0;
         secondNum = 0;
         minusClick = 0;
    
         
         }    
        if(multiplyClick>0){
         total = firstNum * secondNum;
         txtDisplay.setText(String.valueOf(total));
         firstNum = 0;
         secondNum = 0;
         multiplyClick = 0;
    
         
        }
    
        if(divideClick>0){
         total = firstNum / secondNum;
         txtDisplay.setText(String.valueOf(total));
         firstNum = 0;
         secondNum = 0;
         divideClick = 0;
    
        }
    }                                         
    
    private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn0.getText());// TODO add your handling code here:
    }                                    
    
    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn1.getText());// TODO add your handling code here:
    }                                    
    
    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn2.getText());// TODO add your handling code here:
    }                                    
    
    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn3.getText());// TODO add your handling code here:
    }                                    
    
    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn4.getText());// TODO add your handling code here:
    }                                    
    
    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn5.getText());// TODO add your handling code here:
    }                                    
    
    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn6.getText());// TODO add your handling code here:
    }                                    
    
    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn7.getText());// TODO add your handling code here:
    }                                    
    
    private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn8.getText());// TODO add your handling code here:
    }                                    
    
    private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        txtDisplay.setText(txtDisplay.getText()+btn9.getText());// TODO add your handling code here:
    }                                    
    
    private void btnPosNegActionPerformed(java.awt.event.ActionEvent evt) {                                          
        plusminus=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
        plusminus=plusminus*(-1);
        txtDisplay.setText(String.valueOf(plusminus));// TODO add your handling code here:
    }                                         
    
    private void btnDecimalActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if(decimalClick==0){
            txtDisplay.setText(txtDisplay.getText()+btnDecimal.getText());
            decimalClick=1;
        }// TODO add your handling code here:
    }                                          
    
    private void btnSqrtActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
        sqrt = Math.sqrt(Double.parseDouble(txtDisplay.getText()));
        txtDisplay.setText("");
        txtDisplay.setText(txtDisplay.getText() + sqrt);
    
    }                                       
    
    private void btnCActionPerformed(java.awt.event.ActionEvent evt) {                                     
         txtDisplay.setText("");
         decimalClick=0;// TODO add your handling code here:
    }                                    
    
    private void txtDisplayActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(myCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(myCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(myCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(myCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the dialog */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    myCalculator dialog = new myCalculator(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton btn0;
        private javax.swing.JButton btn1;
        private javax.swing.JButton btn2;
        private javax.swing.JButton btn3;
        private javax.swing.JButton btn4;
        private javax.swing.JButton btn5;
        private javax.swing.JButton btn6;
        private javax.swing.JButton btn7;
        private javax.swing.JButton btn8;
        private javax.swing.JButton btn9;
        private javax.swing.JButton btnAdd;
        private javax.swing.JButton btnC;
        private javax.swing.JButton btnDecimal;
        private javax.swing.JButton btnDiv;
        private javax.swing.JButton btnEquals;
        private javax.swing.JButton btnMul;
        private javax.swing.JButton btnPosNeg;
        private javax.swing.JButton btnSqrt;
        private javax.swing.JButton btnSub;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JTextField txtDisplay;
        // End of variables declaration
    }
    Last edited by isayblank; 09-22-2012 at 05:14 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with making a windows like calculator!

    The above code is unreadable since you're using the wrong tags. Please edit your post and change your [quote] [/quote] tags to [code] [/code] tags.

  6. #6
    isayblank is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with making a windows like calculator!

    Please excuse my ignorance, I'm sorry.

  7. #7
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with making a windows like calculator!

    Hello,

    Please provide the code snippet where you are calculating the expression. To my understanding you want the calculation to show result whenever there is a key stroke ? correct ?

Similar Threads

  1. Replies: 13
    Last Post: 07-20-2012, 07:58 PM
  2. Help making a very simple calculator?
    By sfs in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 03:59 AM
  3. Windows java vs Windows AIX
    By sysp in forum New To Java
    Replies: 0
    Last Post: 01-26-2011, 05:32 AM
  4. Help making calculator with TextArea
    By matrixcool in forum AWT / Swing
    Replies: 9
    Last Post: 12-15-2010, 02:56 AM
  5. Help making a calculator
    By karl_koch_89 in forum New To Java
    Replies: 3
    Last Post: 08-27-2010, 02:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •