我不是把它放在浏览器或任何东西中,我只是想在eclipse中以applet的形式运行它,但是,我根本没有成功。我将首先发布代码,然后我会发布我试过通过注释掉某些东西(即构造函数等)的解决方案
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(){ ... }
我知道我可以使用全局运算符导入。 任何人,有什么我想念的吗?任何帮助表示赞赏。提前谢谢。
答案 0 :(得分:2)
如果您在网络浏览器中显示小程序控制台(或只是使用appletviewer
),您将看到错误
load: LoanCalculator.class is not public or has no public constructor.
即。将类声明为public
public class LoanCalculator extends JApplet {