Results 1 to 6 of 6
- 01-28-2009, 02:11 AM #1
[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:
The applet in action is located at: snscourier.co.cc/apps/?app=calcJava 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 }
Sari || Java Freeware Foundation || PresidentLast edited by sari; 01-28-2009 at 03:23 AM.
If you don't try you can't succeed.
- 01-28-2009, 03:05 AM #2
description & code
You will get better results from the forum if you post a description of the problem and the associated code.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-28-2009, 03:18 AM #3
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 03:24 AM.
If you don't try you can't succeed.
-
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); }
- 01-28-2009, 04:53 AM #5
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 programIf you don't try you can't succeed.
-
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.
Good.Question 2: I realised this after I began writing the program
Similar Threads
-
Weird Error?
By sciguy77 in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:32 AM -
Applet Error - Can't see console
By eZs3 in forum Java AppletsReplies: 3Last Post: 05-21-2008, 09:54 PM -
I need simple java applet
By silvia in forum Java AppletsReplies: 4Last Post: 01-27-2008, 07:50 AM -
applet error
By Peter in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks