actionPerformed and exceptions help?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 3
Reputation: PennyBoki is an unknown quantity at this point 
Solved Threads: 0
PennyBoki PennyBoki is offline Offline
Newbie Poster

actionPerformed and exceptions help?

 
0
  #1
Feb 11th, 2007
Hi this is my first post here:cheesy:.
I'm having a problem with how to throw exception in an applet and how to add actionPerformed to other buttons.
This applet calculates the sqare roots of an equation.
It calculates the stuf ok but the thing is that I want to throw an
exception when any of the TextFields are emty. Other thing I want the
RESET Button to clear the TextFields when clicked.
This is the code:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.lang.*;
  6. import java.lang.String;
  7.  
  8. public class Kv extends java.applet.Applet implements ActionListener {
  9.  
  10.  
  11.  
  12. public class Isklucok extends NumberFormatException
  13. {
  14. public Isklucok()
  15. {
  16. super("Enter a number.");
  17. }
  18. }
  19.  
  20.  
  21. Label aa = new Label("a:");
  22. Label bb = new Label("b:");
  23. Label cc = new Label("c:");
  24.  
  25. TextField A = new TextField();
  26. TextField B = new TextField();
  27. TextField C = new TextField();
  28.  
  29. Button otkaz = new Button("Reset");
  30. Button calc = new Button("Calculate");
  31.  
  32. double X1, X2, a, b, c, D;
  33.  
  34. public void init() {
  35.  
  36. setLayout(new GridLayout(4,2));
  37. add(aa);
  38. add(A);
  39. add(bb);
  40. add(B);
  41. add(cc);
  42. add(C);
  43. add(otkaz);
  44. add(calc);
  45.  
  46. calc.addActionListener(new Racunaj());
  47. otkaz.addActionListener(this);
  48. }
  49.  
  50. class Racunaj implements ActionListener throws new Isklucok()
  51.  
  52. {
  53. public void actionPerformed(ActionEvent e)
  54. {
  55.  
  56. a = Double.parseDouble(A.getText());
  57. b = Double.parseDouble(B.getText());
  58. c = Double.parseDouble(C.getText());
  59.  
  60. D = (b*b-4*a*c);
  61.  
  62. X1 = ((-b)-Math.sqrt(D))/(2*a);
  63. X2 = ((-b)+Math.sqrt(D))/(2*a);
  64.  
  65. if(D>0)
  66. JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2);
  67. else if(D<0)
  68. JOptionPane.showMessageDialog(this, "the numbers are komplex");
  69. else
  70. JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2);
  71.  
  72.  
  73. if ((A.getText()==null)||(B.getText()==null)||(C.getText()==null)) throw new Isklucok();
  74.  
  75.  
  76. }
  77.  
  78.  
  79. }
  80. public class Zaebi extends Kv implements ActionListener
  81. {
  82. A.setText("");
  83. B.setText="";
  84. C.setText="";
  85. }
  86. }

the html code is:

  1. <html>
  2. <head>
  3. </head>
  4. <body bgcolor="000000">
  5. <center>
  6. <applet
  7. code = "Kv.class"
  8. width = "250"
  9. height = "125"
  10. >
  11. </applet>
  12. </center>
  13. </body>
  14. </html>

Any kind of HELP is welcome.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: actionPerformed and exceptions help?

 
0
  #2
Feb 11th, 2007
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1080 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC