943,915 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 11260
  • Java RSS
Feb 12th, 2007
-1

Applet Calculator

Expand Post »
the errors are commented in the code:
help???

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.lang.*;
  6.  
  7.  
  8. public class Kol2_1 extends Applet implements ActionListener
  9. {
  10. Button plus = new Button("+");
  11. Button minus = new Button("-");
  12. Button times = new Button("*");
  13. Button div = new Button("/");
  14. Button eq = new Button("=");
  15.  
  16. TextField pole = new TextField("E hej zver");
  17.  
  18. double x=0.0,y=0.0, z;
  19.  
  20.  
  21. public void init()
  22. {
  23. try{
  24. x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
  25. y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
  26. }
  27. catch(StringIndexOutOfBoundsException n)
  28. {JOptionPane.showMessageDialog(this, "Vnesete broj.");}
  29.  
  30. setLayout(new GridLayout(4,2));
  31. add(plus);
  32. add(minus);
  33. add(times);
  34. add(div);
  35. add(eq);
  36. add(pole);
  37.  
  38. plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  39. minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  40. times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  41. div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  42. eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. switch(e.getSource()){ //incompatible types
  52. case plus: { z=x+y;}
  53. case minus: { z=x-y;}
  54. case times: { z=x*y;}
  55. case div: { try{z=x/y;}
  56. catch(NumberFormatException m)
  57. {
  58. if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
  59. }
  60. }
  61. case eq: {pole.setText(""+z);}
  62. }
  63. }
  64. }
Last edited by PennyBoki; Feb 12th, 2007 at 7:42 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PennyBoki is offline Offline
3 posts
since Feb 2007
Feb 12th, 2007
0

Re: Applet Calculator

*squints at code*

.....

Haha, silly me. I missed this at first glance.

1) instead of *.addActionPerformed(this); use *.addActionListener(this);

2) e.getSource() returns an Object, and switch statements work with int values. Might wanna reconsider, with casting and/or an if-else statement.
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005
Feb 12th, 2007
0

Re: Applet Calculator

you are giving plus.addactionPerformed(this);. you should not give like that. please give plus.addActionListener(this);. and then after finishing the constructor write the method

public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("+"))
{....}
}

here e.getActionCommand() method return the caption(that is the string that you written on the button). and then check whether it it equal to +,-,*,/ and then perform the relevant action.







Click to Expand / Collapse  Quote originally posted by PennyBoki ...
the errors are commented in the code:
help???

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.lang.*;
  6.  
  7.  
  8. public class Kol2_1 extends Applet implements ActionListener
  9. {
  10. Button plus = new Button("+");
  11. Button minus = new Button("-");
  12. Button times = new Button("*");
  13. Button div = new Button("/");
  14. Button eq = new Button("=");
  15.  
  16. TextField pole = new TextField("E hej zver");
  17.  
  18. double x=0.0,y=0.0, z;
  19.  
  20.  
  21. public void init()
  22. {
  23. try{
  24. x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
  25. y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
  26. }
  27. catch(StringIndexOutOfBoundsException n)
  28. {JOptionPane.showMessageDialog(this, "Vnesete broj.");}
  29.  
  30. setLayout(new GridLayout(4,2));
  31. add(plus);
  32. add(minus);
  33. add(times);
  34. add(div);
  35. add(eq);
  36. add(pole);
  37.  
  38. plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  39. minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  40. times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  41. div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  42. eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. switch(e.getSource()){ //incompatible types
  52. case plus: { z=x+y;}
  53. case minus: { z=x-y;}
  54. case times: { z=x*y;}
  55. case div: { try{z=x/y;}
  56. catch(NumberFormatException m)
  57. {
  58. if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
  59. }
  60. }
  61. case eq: {pole.setText(""+z);}
  62. }
  63. }
  64. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
muthulakshmil is offline Offline
3 posts
since Feb 2007
Feb 13th, 2007
0

Re: Applet Calculator

thanks alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PennyBoki is offline Offline
3 posts
since Feb 2007
Dec 15th, 2010
0

please help me

how to create a java program using applets in calculate for grocerry items please help me....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cempaka123 is offline Offline
2 posts
since Dec 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help !
Next Thread in Java Forum Timeline: [Help] WAV using Clip





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC