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:
Code:// import all need packages import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.applet.*; //extend JFrame for the user interface and actionlistener for events public class LCalcApplet extends JApplet implements ActionListener { /* create instance variables so they can be used in every method * instance variables allow all method to use them, instead of only * the method you are working in */ JButton btnCalc; JTextField txtAmount; JTextField txtAPR; JTextField txtMonths; JTextField txtPayment; JLabel lblAmount; JLabel lblAPR; JLabel lblMonths; JLabel lblPayment; /* create a JMenuBar, used to hold the Menu's such * as file, edit, and that kind of stuff...On top of that, * you need the MenuItems...These are the actual items that show * up on the drop down */ JMenuBar menuBar; JMenu fileMenu; JMenuItem exitItem; JMenuItem resetItem; //the classes constructor public void init() { //create the menuBar menuBar = new JMenuBar(); //create the items fileMenu = new JMenu("File"); exitItem = new JMenuItem("Exit"); resetItem = new JMenuItem("Reset"); /* add all the items to the Menu, this is * done by the add() method coorisponding to * the JMenu class */ fileMenu.add(exitItem); fileMenu.add(resetItem); /* add action listeners to the menu items, this * will enable us to peform action based on what was * clicked. */ exitItem.addActionListener(this); resetItem.addActionListener(this); /* once all that is done, you add the * menu to the menuBar, and then you set * the menuBar to be used in the program...Note * you do not have to add the menubar to the Container * after setting it. */ menuBar.add(fileMenu); setJMenuBar(menuBar); /* JPanels are a type of container... * It's good to use them as flow control, * and to group controls */ JPanel row1 = new JPanel(); row1.setBackground(Color.white); //add the amount textfield and label to the panel lblAmount = new JLabel(" Purchase amount: "); row1.add(lblAmount); /* The JTextField requires one parameter, * the number of characters long...So we use * ten on these. */ txtAmount = new JTextField(10); txtAmount.setBackground(Color.orange); row1.add(txtAmount); JPanel row2 = new JPanel(); row2.setBackground(Color.white); //add the apr label and textfield to the panel lblAmount = new JLabel(" Interest rate(APR): "); row2.add(lblAmount); txtAPR = new JTextField(10); txtAPR.setBackground(Color.orange); row2.add(txtAPR); JPanel row3 = new JPanel(); row3.setBackground(Color.white); //add the month textfield and label to the panel lblMonths = new JLabel("Number of months: "); row3.add(lblMonths); txtMonths = new JTextField(10); txtMonths.setBackground(Color.orange); row3.add(txtMonths); JPanel row4 = new JPanel(); row4.setBackground(Color.white); //add the payment label and textfield to the panel lblPayment = new JLabel(" Your payment is: "); lblPayment.setForeground(Color.red); row4.add(lblPayment); txtPayment = new JTextField(10); txtPayment.setBackground(Color.orange); /* set this textfield so that it cannot be edited, * because you don't want the user to enter anything * in here. */ txtPayment.setEditable(false); row4.add(txtPayment); JPanel row5 = new JPanel(); row5.setBackground(Color.white); //add the calculate button btnCalc = new JButton("Calculate"); btnCalc.setBackground(Color.cyan); /* add an actionListener to the button, this * enables us to perform events if it was clicked */ btnCalc.addActionListener(this); row5.add(btnCalc); /* Construct a container object, and * then add all of the JPanels to it. * You set the layout to the container, so * that you have some kind of flow control in the * program. */ Container content = getContentPane(); //we will use a flowlayout content.setLayout(new FlowLayout()); content.add(row1); content.add(row2); content.add(row3); content.add(row4); content.add(row5); content.setBackground(Color.white); //set the content pane of the frame setContentPane(content); //last, set it visible setVisible(true); } public void actionPerformed(ActionEvent ae) { /* check to see if the event was fired by the * calculate button, if so peform the correct operation */ if (ae.getSource() == btnCalc) { /* use some if/else statements to check and make sure * that all the textfields have some value in them */ if (txtAmount.getText().equals("")) { JOptionPane.showMessageDialog(this,"Please enter an amount"); // invoke a class method that will show a simple message to the user } else if(txtAPR.getText().equals("")) { JOptionPane.showMessageDialog(this,"Please enter the interest rate"); // invoke a class method that will show a simple message to the user } else if (txtMonths.getText().equals("")) { JOptionPane.showMessageDialog(this,"Please enter length of months"); // invoke a class method that will show a simple message to the user } /* if we make it to here, all values are filled, and ready to be proccessed * so we use a try cacth statement to make sure the values wasn't anything other * than numeric. */ else { try { //create new method used to calculate and show the monthly payment getAndShowPayment(); } catch (ArithmeticException mathE) { //catch an arithmetic exception, and if it was caught give the user //some feedback JOptionPane.showMessageDialog(this,"Error in arithmetic"); } } } //just in case the selection was the exit item else if (ae.getSource() == exitItem) { /* call the System classes exit() method, and * place 0 inside of the paranthesis. This will make * the program exit. */ System.exit(0); } //if the reset menu item was selected else if (ae.getSource() == resetItem) { /* reset all the values in the textfields to null, * we do this by callling the textfields setText() method * and putting null values in there */ txtAmount.setText(""); txtAPR.setText(""); txtMonths.setText(""); txtPayment.setText(""); } } /* This is a custom method that returns void, or nothing... * it will calculate the values, and print it out to the user */ public void getAndShowPayment() { /* Use the double's wrapper class(Double) to parse * the text into a double...Do this for all three textfields */ double dAmount = Double.valueOf(txtAmount.getText()); double dAPR = Double.valueOf(txtAPR.getText()); double dMonths = Double.valueOf(txtMonths.getText()); /* This is the forumula to calcuate the mortgage: * a -- loan amount * m -- # of months to repay * i -- monthly interest * *Monthly payment: * * a(i) * -------- (divide) * 1 - (1/1+i)^m */ /* this is the interest rate, we divide by 1200 because we * want the user to be able to enter the intrest rate in this format: * 6.5 instead of .065 */ double rate = (dAPR / 1200); //simply calculate the payment using the above formula. double dgetMonthlyPayment = (dAmount * rate / (1 - (Math.pow(1 / (1 + rate), dMonths)))); /* use the decimal format class to create a new decimal format, this way the calculation * will be to two decimal places * we precede it with java.text because this is the class that contains the * decimal format, and we don't to import the statement */ java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00"); //show the amount by calling the decimalFormat.format() method txtPayment.setText("$ " + dec.format(dgetMonthlyPayment)); } }
HTML CODE
Code:<HTML> <HEAD> </HEAD> <BODY BGCOLOR="000000"> <CENTER> <APPLET code = "LCalcApplet.class" width = "370" height = "600" > </APPLET> </CENTER> </BODY> </HTML>


Reply With Quote