applet not showing up

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

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

applet not showing up

 
0
  #1
Mar 13th, 2005
My applet is not showing up in a web page, and all of the code is correct...
I'll post it and the html code:

  1. // import all need packages
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.applet.*;
  6.  
  7. //extend JFrame for the user interface and actionlistener for events
  8. public class LCalcApplet extends JApplet implements ActionListener
  9. {
  10. /* create instance variables so they can be used in every method
  11. * instance variables allow all method to use them, instead of only
  12. * the method you are working in
  13. */
  14. JButton btnCalc;
  15. JTextField txtAmount;
  16. JTextField txtAPR;
  17. JTextField txtMonths;
  18. JTextField txtPayment;
  19. JLabel lblAmount;
  20. JLabel lblAPR;
  21. JLabel lblMonths;
  22. JLabel lblPayment;
  23.  
  24. /* create a JMenuBar, used to hold the Menu's such
  25. * as file, edit, and that kind of stuff...On top of that,
  26. * you need the MenuItems...These are the actual items that show
  27. * up on the drop down
  28. */
  29. JMenuBar menuBar;
  30. JMenu fileMenu;
  31. JMenuItem exitItem;
  32. JMenuItem resetItem;
  33.  
  34. //the classes constructor
  35. public void init()
  36. {
  37. //create the menuBar
  38. menuBar = new JMenuBar();
  39. //create the items
  40. fileMenu = new JMenu("File");
  41. exitItem = new JMenuItem("Exit");
  42. resetItem = new JMenuItem("Reset");
  43. /* add all the items to the Menu, this is
  44. * done by the add() method coorisponding to
  45. * the JMenu class
  46. */
  47. fileMenu.add(exitItem);
  48. fileMenu.add(resetItem);
  49. /* add action listeners to the menu items, this
  50. * will enable us to peform action based on what was
  51. * clicked.
  52. */
  53. exitItem.addActionListener(this);
  54. resetItem.addActionListener(this);
  55. /* once all that is done, you add the
  56. * menu to the menuBar, and then you set
  57. * the menuBar to be used in the program...Note
  58. * you do not have to add the menubar to the Container
  59. * after setting it.
  60. */
  61. menuBar.add(fileMenu);
  62. setJMenuBar(menuBar);
  63.  
  64. /* JPanels are a type of container...
  65. * It's good to use them as flow control,
  66. * and to group controls
  67. */
  68. JPanel row1 = new JPanel();
  69. row1.setBackground(Color.white);
  70. //add the amount textfield and label to the panel
  71. lblAmount = new JLabel(" Purchase amount: ");
  72. row1.add(lblAmount);
  73. /* The JTextField requires one parameter,
  74. * the number of characters long...So we use
  75. * ten on these.
  76. */
  77. txtAmount = new JTextField(10);
  78. txtAmount.setBackground(Color.orange);
  79. row1.add(txtAmount);
  80.  
  81. JPanel row2 = new JPanel();
  82. row2.setBackground(Color.white);
  83. //add the apr label and textfield to the panel
  84. lblAmount = new JLabel(" Interest rate(APR): ");
  85. row2.add(lblAmount);
  86. txtAPR = new JTextField(10);
  87. txtAPR.setBackground(Color.orange);
  88. row2.add(txtAPR);
  89.  
  90. JPanel row3 = new JPanel();
  91. row3.setBackground(Color.white);
  92. //add the month textfield and label to the panel
  93. lblMonths = new JLabel("Number of months: ");
  94. row3.add(lblMonths);
  95. txtMonths = new JTextField(10);
  96. txtMonths.setBackground(Color.orange);
  97. row3.add(txtMonths);
  98.  
  99. JPanel row4 = new JPanel();
  100. row4.setBackground(Color.white);
  101. //add the payment label and textfield to the panel
  102. lblPayment = new JLabel(" Your payment is: ");
  103. lblPayment.setForeground(Color.red);
  104. row4.add(lblPayment);
  105. txtPayment = new JTextField(10);
  106. txtPayment.setBackground(Color.orange);
  107. /* set this textfield so that it cannot be edited,
  108. * because you don't want the user to enter anything
  109. * in here.
  110. */
  111. txtPayment.setEditable(false);
  112. row4.add(txtPayment);
  113.  
  114. JPanel row5 = new JPanel();
  115. row5.setBackground(Color.white);
  116. //add the calculate button
  117. btnCalc = new JButton("Calculate");
  118. btnCalc.setBackground(Color.cyan);
  119. /* add an actionListener to the button, this
  120. * enables us to perform events if it was clicked
  121. */
  122. btnCalc.addActionListener(this);
  123. row5.add(btnCalc);
  124.  
  125. /* Construct a container object, and
  126. * then add all of the JPanels to it.
  127. * You set the layout to the container, so
  128. * that you have some kind of flow control in the
  129. * program.
  130. */
  131. Container content = getContentPane();
  132. //we will use a flowlayout
  133. content.setLayout(new FlowLayout());
  134. content.add(row1);
  135. content.add(row2);
  136. content.add(row3);
  137. content.add(row4);
  138. content.add(row5);
  139. content.setBackground(Color.white);
  140. //set the content pane of the frame
  141. setContentPane(content);
  142. //last, set it visible
  143. setVisible(true);
  144.  
  145. }
  146.  
  147. public void actionPerformed(ActionEvent ae)
  148. {
  149. /* check to see if the event was fired by the
  150. * calculate button, if so peform the correct operation
  151. */
  152. if (ae.getSource() == btnCalc)
  153. {
  154. /* use some if/else statements to check and make sure
  155. * that all the textfields have some value in them
  156. */
  157. if (txtAmount.getText().equals(""))
  158. {
  159. JOptionPane.showMessageDialog(this,"Please enter an amount");
  160. // invoke a class method that will show a simple message to the user
  161. }
  162. else if(txtAPR.getText().equals(""))
  163. {
  164. JOptionPane.showMessageDialog(this,"Please enter the interest rate");
  165. // invoke a class method that will show a simple message to the user
  166. }
  167. else if (txtMonths.getText().equals(""))
  168. {
  169. JOptionPane.showMessageDialog(this,"Please enter length of months");
  170. // invoke a class method that will show a simple message to the user
  171. }
  172. /* if we make it to here, all values are filled, and ready to be proccessed
  173. * so we use a try cacth statement to make sure the values wasn't anything other
  174. * than numeric.
  175. */
  176. else
  177. {
  178. try {
  179. //create new method used to calculate and show the monthly payment
  180. getAndShowPayment();
  181. }
  182. catch (ArithmeticException mathE)
  183. {
  184. //catch an arithmetic exception, and if it was caught give the user
  185. //some feedback
  186. JOptionPane.showMessageDialog(this,"Error in arithmetic");
  187. }
  188. }
  189. }
  190. //just in case the selection was the exit item
  191. else if (ae.getSource() == exitItem)
  192. {
  193. /* call the System classes exit() method, and
  194. * place 0 inside of the paranthesis. This will make
  195. * the program exit.
  196. */
  197. System.exit(0);
  198. }
  199. //if the reset menu item was selected
  200. else if (ae.getSource() == resetItem)
  201. {
  202. /* reset all the values in the textfields to null,
  203. * we do this by callling the textfields setText() method
  204. * and putting null values in there
  205. */
  206. txtAmount.setText("");
  207. txtAPR.setText("");
  208. txtMonths.setText("");
  209. txtPayment.setText("");
  210. }
  211. }
  212.  
  213. /* This is a custom method that returns void, or nothing...
  214. * it will calculate the values, and print it out to the user
  215. */
  216. public void getAndShowPayment()
  217. {
  218. /* Use the double's wrapper class(Double) to parse
  219. * the text into a double...Do this for all three textfields
  220. */
  221. double dAmount = Double.valueOf(txtAmount.getText());
  222. double dAPR = Double.valueOf(txtAPR.getText());
  223. double dMonths = Double.valueOf(txtMonths.getText());
  224.  
  225. /* This is the forumula to calcuate the mortgage:
  226. * a -- loan amount
  227. * m -- # of months to repay
  228. * i -- monthly interest
  229. *
  230. *Monthly payment:
  231. *
  232. * a(i)
  233. * -------- (divide)
  234. * 1 - (1/1+i)^m
  235. */
  236.  
  237. /* this is the interest rate, we divide by 1200 because we
  238. * want the user to be able to enter the intrest rate in this format:
  239. * 6.5 instead of .065
  240. */
  241. double rate = (dAPR / 1200);
  242. //simply calculate the payment using the above formula.
  243.  
  244. double dgetMonthlyPayment = (dAmount * rate / (1 - (Math.pow(1 / (1 + rate), dMonths))));
  245. /* use the decimal format class to create a new decimal format, this way the calculation
  246. * will be to two decimal places
  247. * we precede it with java.text because this is the class that contains the
  248. * decimal format, and we don't to import the statement
  249. */
  250. java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");
  251. //show the amount by calling the decimalFormat.format() method
  252. txtPayment.setText("$ " + dec.format(dgetMonthlyPayment));
  253. }
  254. }


HTML CODE
  1. <HTML>
  2. <HEAD>
  3. </HEAD>
  4. <BODY BGCOLOR="000000">
  5. <CENTER>
  6. <APPLET
  7. code = "LCalcApplet.class"
  8. width = "370"
  9. height = "600"
  10. >
  11. </APPLET>
  12. </CENTER>
  13. </BODY>
  14. </HTML>
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: applet not showing up

 
0
  #2
Mar 13th, 2005
never mind.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC