Results 1 to 6 of 6
  1. #1
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Simple Calculator Applet Weird Error

    Hello all,

    I am currently witing a simple calculator applet. But I have come across a bug or error In my logic that I can't seem to solve. When attempting to use the addition operator I get very strange results. Here is the code:

    Java Code:
    public class CalcApplet extends javax.swing.JApplet {
        int x;
        int y;
        int ans;
        String xy = "x";
        StringBuffer xx = null;
        StringBuffer yy = null;
        String op = null;
        
        public void init() {
            initComponents();
            xx = new StringBuffer();
            yy = new StringBuffer();
        }
    
       
        @SuppressWarnings("unchecked")
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();
            jButton7 = new javax.swing.JButton();
            jButton8 = new javax.swing.JButton();
            jButton9 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            jButton10 = new javax.swing.JButton();
            jButton11 = new javax.swing.JButton();
            jButton12 = new javax.swing.JButton();
            jButton13 = new javax.swing.JButton();
            jButton14 = new javax.swing.JButton();
            jButton15 = new javax.swing.JButton();
            jButton16 = new javax.swing.JButton();
    
            jPanel1.setBackground(new java.awt.Color(140, 140, 139));
    
            jButton1.setText("1");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
    
            jButton2.setText("2");
            jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton2MouseClicked(evt);
                }
            });
    
            jButton3.setText("3");
            jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
    
            jButton4.setText("4");
            jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton4MouseClicked(evt);
                }
            });
    
            jButton5.setText("5");
            jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton5MouseClicked(evt);
                }
            });
    
            jButton6.setText("6");
            jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton6MouseClicked(evt);
                }
            });
    
            jButton7.setText("7");
            jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton7MouseClicked(evt);
                }
            });
    
            jButton8.setText("8");
            jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton8MouseClicked(evt);
                }
            });
    
            jButton9.setText("9");
            jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton9MouseClicked(evt);
                }
            });
    
            jButton10.setText("0");
            jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton10MouseClicked(evt);
                }
            });
    
            jButton11.setText("=");
            jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton11MouseClicked(evt);
                }
            });
    
            jButton12.setText("+");
            jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton12MouseClicked(evt);
                }
            });
            jButton12.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton12ActionPerformed(evt);
                }
            });
    
            jButton13.setText("-");
            jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton13MouseClicked(evt);
                }
            });
    
            jButton14.setText("/");
            jButton14.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton14MouseClicked(evt);
                }
            });
    
            jButton15.setText("*");
            jButton15.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton15MouseClicked(evt);
                }
            });
    
            jButton16.setText("Clear");
            jButton16.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton16MouseClicked(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()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton16, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                    .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
    
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                                    .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addComponent(jButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap(41, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton1)
                        .addComponent(jButton10)
                        .addComponent(jButton13))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton5)
                        .addComponent(jButton6)
                        .addComponent(jButton4)
                        .addComponent(jButton11)
                        .addComponent(jButton14))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton7)
                        .addComponent(jButton8)
                        .addComponent(jButton9)
                        .addComponent(jButton15)
                        .addComponent(jButton12))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton16)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            );
        }
    
        private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
            if (xy == "x") {
               xx.append("4");
            } else {
                yy.append("4");
            }
        }
    
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("1");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("1");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("2");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("2");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("3");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("3");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("4");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("4");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("5");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("5");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("6");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("6");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("7");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("7");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton8MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("8");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("8");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton9MouseClicked(java.awt.event.MouseEvent evt) {
            if (xy == "x") {
               xx.append("9");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("9");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton10MouseClicked(java.awt.event.MouseEvent evt) {
            //This is Zero
            if (xy == "0") {
               xx.append("6");
               jTextField1.setText(xx.toString());
            } else {
                yy.append("0");
                jTextField1.setText(yy.toString());
            }
        }
    
        private void jButton12MouseClicked(java.awt.event.MouseEvent evt) {
            op = "+";
            xy = "y";
        }
    
        private void jButton13MouseClicked(java.awt.event.MouseEvent evt) {
            op ="-";
            xy = "y";
        }
    
        private void jButton14MouseClicked(java.awt.event.MouseEvent evt) {
            op = "/";
            xy = "y";
        }
    
        private void jButton15MouseClicked(java.awt.event.MouseEvent evt) {
           op = "*";
           xy = "y";
        }
    
        private void jButton11MouseClicked(java.awt.event.MouseEvent evt) {
            // Equals "="
            x = Integer.parseInt(xx.toString());
            y = Integer.parseInt(yy.toString());
            if (op == "+") {
                ans = x + y;
            } else if (op == "-") {
                ans = x - y;
            } else if (op == "*") {
                ans = x * y;
            } else if (op == "/") {
                ans = x / y;
            }
            jTextField1.setText(String.valueOf(ans));
            xy = "x";
        }
    
        private void jButton16MouseClicked(java.awt.event.MouseEvent evt) {
            xy = "x";
            xx = new StringBuffer();
            yy = new StringBuffer();
            jTextField1.setText("");
        }
    
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton10;
        private javax.swing.JButton jButton11;
        private javax.swing.JButton jButton12;
        private javax.swing.JButton jButton13;
        private javax.swing.JButton jButton14;
        private javax.swing.JButton jButton15;
        private javax.swing.JButton jButton16;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JButton jButton8;
        private javax.swing.JButton jButton9;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
    
    }
    The applet in action is located at: snscourier.co.cc/apps/?app=calc

    Sari || Java Freeware Foundation || President
    Last edited by sari; 01-28-2009 at 04:23 AM.
    If you don't try you can't succeed.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default description & code

    You will get better results from the forum if you post a description of the problem and the associated code.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Thank you I just put all my applets and source on that web page and figured I t would be better to see the Applet in action
    That is fixed now :)
    Last edited by sari; 01-28-2009 at 04:24 AM.
    If you don't try you can't succeed.

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

    Default

    Question 1: Why add MouseListener to a JButton and not an ActionListener? The button is optimized to work with actionlisteners not mouselisteners. For instance, will a mouse listener act properly if the button has been disabled? (answer: no).

    Question 2: Which is easier to comprehend and debug, an app that lists a whole bunch of buttons as yours or one that creates the buttons based on an array of String such as this app fragment below.
    Java Code:
    public class CalculatorGUI
    {
      private JPanel mainPanel = new JPanel();
      private String[][] buttonStrings =
      {
          {"7", "8", "9", "/"},
          {"4", "5", "6", "*"},
          {"1", "2", "3", "-"},
          {"0", ".", "=", "+"}
      };
      private JTextField displayField = new JTextField();
      private StringBuilder displaySB = new StringBuilder();
      private boolean dotEntered = false;
    
      public CalculatorGUI()
      {
        int rows = buttonStrings.length;
        int cols = buttonStrings[0].length;
        JPanel btnPanel = new JPanel(new GridLayout(rows, cols));
        NumberBtnListener numberBtnListener = new NumberBtnListener();
        for (int i = 0; i < buttonStrings.length; i++)
        {
          for (int j = 0; j < buttonStrings[i].length; j++)
          {
            String btnString = buttonStrings[i][j];
            JButton btn = new JButton(btnString);
            btnPanel.add(btn);
            if ("0123456789.".contains(btnString))
            {
              btn.addActionListener(numberBtnListener);
            }
            
          }
        }
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(btnPanel, BorderLayout.CENTER);
        mainPanel.add(displayField, BorderLayout.NORTH);
      }

  5. #5
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Answers:

    Question 1: I have no reason to use an action listener Maybe this is simply due to inexperience but why would I want my button to respond to "Any Action" and not just a mouse click

    Question 2: I realised this after I began writing the program
    If you don't try you can't succeed.

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

    Default

    Quote Originally Posted by sari View Post
    Question 1: I have no reason to use an action listener Maybe this is simply due to inexperience but why would I want my button to respond to "Any Action" and not just a mouse click
    Ah, because you don't yet understand actionlisteners it seems. Please read up on how to code JButtons in the Sun Swing tutorial and it will explain all. As a for instance, a MouseListener has no way of knowing the state of the JButton and will not work correctly if the button's state has changed (for instance in my post above if you disable the button by calling setEnabled(false)). For another an ActionListener was specifically built to handle button and menu input. You should use the correct tool for the job. Again, read the tutorial to learn about this.

    Question 2: I realised this after I began writing the program
    Good.

Similar Threads

  1. Weird Error?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:32 AM
  2. Applet Error - Can't see console
    By eZs3 in forum Java Applets
    Replies: 3
    Last Post: 05-21-2008, 10:54 PM
  3. I need simple java applet
    By silvia in forum Java Applets
    Replies: 4
    Last Post: 01-27-2008, 08:50 AM
  4. applet error
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 08:04 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
  •