0

我没有把它放在浏览器或任何东西中,我只是想在 eclipse 中将它作为一个小程序运行,但是,我根本没有成功。我将首先发布代码,然后通过注释掉某些内容(即构造函数等)来发布我尝试过的解决方案

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);
    **/
}
}

我有一个单独的贷款课程。该程序作为一个应用程序工作得很好。所以我注释掉了主要论点

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

我注释掉了构造函数

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

并创建了一个 init 构造函数

  public void init(){ ... }

我知道我可以使用全局运算符进行导入。谁,有什么我想念的吗?任何帮助表示赞赏。先感谢您。

4

1 回答 1

2

如果您在 Web 浏览器中显示小程序控制台(或简单地使用appletviewer),您将看到错误

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

即声明类为public

public class LoanCalculator extends JApplet {
于 2013-09-17T09:41:55.043 回答