943,935 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1368
  • Java RSS
Feb 11th, 2007
0

actionPerformed and exceptions help?

Expand Post »
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:
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. 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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PennyBoki is offline Offline
3 posts
since Feb 2007
Feb 11th, 2007
0

Re: actionPerformed and exceptions help?

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

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: how do i apply this binary search into this libary program
Next Thread in Java Forum Timeline: java program to calculate simple interest





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


Follow us on Twitter


© 2011 DaniWeb® LLC