Question

I'm not putting this in a browser or anything, I simply want to run this in eclipse as an applet, however, I'm not succeeding at all. I'll first post the code, and then I'll post solutions I've tried by commenting out certain things (i.e. constructor, etc)

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

class LoanCalculator extends JApplet {
    //Loan Calc text fields
private JTextField annInterestRate = new JTextField();
private JTextField numOfYears = new JTextField();
private JTextField lnAmount = new JTextField();
private JTextField monthlyPayment = new JTextField();
private JTextField totalPayment = new JTextField();
    //Loan Calc buttons
private JButton computeLoan = new JButton("Compute Payment");

    public LoanCalculator(){
    //Panel for textfield and labels
    JPanel txtAndLabels = new JPanel(new GridLayout(5, 2));
    txtAndLabels.add(new JLabel("Annual Interest Rate"));
    txtAndLabels.add(annInterestRate);
    txtAndLabels.add(new JLabel("Number of Years"));
    txtAndLabels.add(numOfYears);
    txtAndLabels.add(new JLabel("Loan Amount"));
    txtAndLabels.add(lnAmount);
    txtAndLabels.add(new JLabel("Monthly Payment"));
    txtAndLabels.add(monthlyPayment);
    txtAndLabels.add(new JLabel("Total Payment"));
    txtAndLabels.add(totalPayment);
    txtAndLabels.setBorder(new TitledBorder("Enter loan amount, interest rate,     and years"));
    //Button panel
    JPanel button = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    button.add(computeLoan);
    //Add panels
    add(txtAndLabels, BorderLayout.CENTER);
    add(button, BorderLayout.SOUTH);
    //Listener for button
    computeLoan.addActionListener(new ButtonListener());
    }

    /** Compute Payment Button **/
    private class ButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //Get text field values
            double interest =   Double.parseDouble(annInterestRate.getText());
            int year = Integer.parseInt(numOfYears.getText());
            double loanAmnt = Double.parseDouble(lnAmount.getText());
            //Create loan object
            Loan loan = new Loan(interest, year, loanAmnt);
            //Display Monthly Payment and Total Payment
            monthlyPayment.setText(String.format("%.2f",   loan.getMonthlyPayment()));
            totalPayment.setText(String.format("%.2f",   loan.getTotalPayment()));
        }
    }
public static void main(String[] args) {

    /**JFrame frame = new JFrame("Loan Calculator applet");
    LoanCalculator loanApplet = new LoanCalculator();
    frame.add(loanApplet, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    **/
}
}

I've got a separate class for loan. The program works lovely as an application. So I commented out the main argument

//public static void main(String[] args){ ... }

I commented out the contructor

//public LoanCalculator(){ .... }

and made an init constructor

  public void init(){ ... }

I'm aware I can use the global operator to import. Any who, is there something I'm missing? Any assistance is appreciated. Thank you in advance.

Was it helpful?

Solution

If you display the applet console in a web browser (or simply use appletviewer) you will see the error

load: LoanCalculator.class is not public or has no public constructor.

I.e. Declare the class as public

public class LoanCalculator extends JApplet {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top