Results 1 to 3 of 3
  1. #1
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default JApplet problems

    so i have the following code
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class lab5 extends JApplet implements ActionListener
    {
    		private lab5 invest = new lab5();
    	private JFrame loan = new JFrame("Loan Calculator");
    	private JButton btninvest = new JButton("Calculate your Investment");
    	private JButton btnloan = new JButton("Loan Calculator");
    
    	/*Initalize the applet*/
    	public void init()
    	{
    		getContentPane().setLayout(new FlowLayout());
    		getContentPane().add(btninvest);
    		getContentPane().add(btnloan);
    		btninvest.addActionListener(this);
    		btnloan.addActionListener(this);
    	}
    
    
     	public void actionPerformed(ActionEvent e)
     	{
    		if (e.getSource() == btninvest)
    		{
    		  invest.setSize(300, 200);
    		  invest.setTitle("Investment Calculator");
    		  invest.setVisible(true);
    		}
    		else if (e.getSource() == btnloan)
    		{
    		  loan.setSize(300, 200);
    		  LoanApplet applet = new LoanApplet();
    		  loan.getContentPane().add(applet);
    		  applet.init();
    		  loan.setVisible(true);
    		}
    	  }
    
    }
    which returns these errors:
    H:\Advanced Java\Lab Work\Lab5\lab5.java:28: cannot find symbol
    symbol : method setTitle(java.lang.String)
    location: class lab5
    invest.setTitle("Investment Calculator");
    ^
    H:\Advanced Java\Lab Work\Lab5\lab5.java:34: cannot find symbol
    symbol : class LoanApplet
    location: class lab5
    LoanApplet applet = new LoanApplet();
    ^
    H:\Advanced Java\Lab Work\Lab5\lab5.java:34: cannot find symbol
    symbol : class LoanApplet
    location: class lab5
    LoanApplet applet = new LoanApplet();
    ^
    3 errors

    Tool completed with exit code 1
    and here is the LoanApplet:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class LoanApplet extends JApplet implements ActionListener {
      // Declare and create text fields for interest rate
      // year, loan amount, monthly payment, and total payment
      private JTextField jtfAnnualInterestRate = new JTextField();
      private JTextField jtfNumberOfYears = new JTextField();
      private JTextField jtfLoanAmount = new JTextField();
      private JTextField jtfMonthlyPayment = new JTextField();
      private JTextField jtfTotalPayment = new JTextField();
    
      // Declare and create a Compute Payment button
      private JButton jbtComputeLoan = new JButton("Compute Payment");
    
      /** Initialize user interface */
      public void init() {
    	// Set properties on the text fields
    	jtfMonthlyPayment.setEditable(false);
    	jtfTotalPayment.setEditable(false);
    
    	// Right align text fields
    	jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
    	jtfNumberOfYears.setHorizontalAlignment(JTextField.RIGHT);
    	jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
    	jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
    	jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);
    
    	// Panel p1 to hold labels and text fields
    	JPanel p1 = new JPanel(new GridLayout(5, 2));
    	p1.add(new JLabel("Annual Interest Rate"));
    	p1.add(jtfAnnualInterestRate);
    	p1.add(new JLabel("Number of Years"));
    	p1.add(jtfNumberOfYears);
    	p1.add(new JLabel("Loan Amount"));
    	p1.add(jtfLoanAmount);
    	p1.add(new JLabel("Monthly Payment"));
    	p1.add(jtfMonthlyPayment);
    	p1.add(new JLabel("Total Payment"));
    	p1.add(jtfTotalPayment);
    	p1.setBorder(new
    	  TitledBorder("Enter interest rate, year and loan amount"));
    
    	// Panel p2 to hold the button
    	JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    	p2.add(jbtComputeLoan);
    
    	// Add the components to the applet
    	getContentPane().add(p1, BorderLayout.CENTER);
    	getContentPane().add(p2, BorderLayout.SOUTH);
    
    	// Register listener
    	jbtComputeLoan.addActionListener(this);
      }
    
      /** Handle the Compute Payment button */
      public void actionPerformed(ActionEvent e) {
    	if (e.getSource() == jbtComputeLoan) {
    	  // Get values from text fields
    	  double interest =
    		Double.parseDouble(jtfAnnualInterestRate.getText());
    	  int year =
    		Integer.parseInt(jtfNumberOfYears.getText());
    	  double loanAmount =
    		Double.parseDouble(jtfLoanAmount.getText());
    
    	  // Create a loan object
    	  Loan loan = new Loan(interest, year, loanAmount);
    
    	  // Display monthly payment and total payment
    	  jtfMonthlyPayment.setText("" +
    		(int)(loan.monthlyPayment() * 100) / 100.0);
    	  jtfTotalPayment.setText("" +
    		(int)(loan.totalPayment() * 100) / 100.0);
    	}
      }
    
      public static void main(String[] args) {
    	// Create a frame
    	JFrame frame = new JFrame("Applet is in the frame");
    
    	// Create an instance of the applet
    	LoanApplet applet = new LoanApplet();
    
    	// Add the applet to the frame
    	frame.getContentPane().add(applet, BorderLayout.CENTER);
    
    	// Invoke applet's init method
    	applet.init();
    
    	// Display the frame
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(300, 300);
    	frame.setVisible(true);
      }
    }
    i'm assuming it has something to do with my instantiation of the loanapplet but i'm not 100% sure

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Why are you creating a new lab05 inside the lab05 class that doesn't make any sense.

  3. #3
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    Ok so your right lol, i have no idea about applets or anything of the sort. I changed it to read

    private JFrame invest = new JFrame();

    it loads now:)
    Last edited by Rgfirefly24; 04-24-2008 at 07:41 PM.

Similar Threads

  1. Database to JApplet
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 05:18 AM
  2. Another problem in JApplet :S
    By juju in forum Java Applets
    Replies: 2
    Last Post: 12-30-2007, 07:46 PM
  3. Problems while loading a JPanel to JApplet...
    By Ananth Chellathurai in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 10:47 AM
  4. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 08:45 AM
  5. JApplet and html
    By paty in forum Java Applets
    Replies: 1
    Last Post: 08-02-2007, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •