Java 为什么不';我的纽扣没出现吗?
在Eclipse中作为测试应用程序运行:Java 为什么不';我的纽扣没出现吗?,java,swing,Java,Swing,在Eclipse中作为测试应用程序运行: ` //import all needed functionality import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.*; public class ScrofaniWk3 extends JApplet{ /**
` //import all needed functionality
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
public class ScrofaniWk3 extends JApplet{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
// Declare variables and put code application logic here
String userInput = null;
JLabel loanAmountLabel = new JLabel("Loan Amount: ");
JTextField loanAmount = new JTextField();
double[] ratesList = {0.0535, 0.055, 0.0575};
JLabel rateLabel=new JLabel("Interest Rate: ");
JTextField rate=new JTextField();
String[] yearsList = {"7","15","30"};
JLabel yearsLabel=new JLabel("Years of Payment: ");
JTextField years=new JTextField();
JComboBox chooseYears = new JComboBox(yearsList);
JLabel payLabel=new JLabel("Monthly Payment: ");
JLabel payment=new JLabel();
JButton calculate=new JButton("Calculate");
JButton clear=new JButton("Clear");
JButton quit=new JButton("Quit");
JTextArea payments=new JTextArea();
JScrollPane schedulePane=new JScrollPane(payments);
Container mortCalc = getContentPane();
public void init() {
//This makes the chooseYears function
chooseYears.setSelectedIndex(0);
chooseYears.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent choose) {
JComboBox cb = (JComboBox)choose.getSource();
String termYear = (String)cb.getSelectedItem();
years.setText(termYear);
int index=0;
switch (Integer.parseInt(termYear)) {
case 7: index=0; break;
case 15: index=1; break;
case 30: index=2; break;
}
rate.setText(ratesList[index]+"");
}
});
calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Perform the calculation
double rateCalc=Double.parseDouble(rate.getText())/12;
double principalCalc=Double.parseDouble(loanAmount.getText());
double yearsCalc=Integer.parseInt(years.getText())*12;
double monthlyPayment=principalCalc*Math.pow(1+rateCalc,yearsCalc)*rateCalc/(Math.pow(1+rateCalc,yearsCalc)-1);
DecimalFormat df = new DecimalFormat("$###,###.##");
payment.setText(df.format(monthlyPayment));
// Perform extra calculations to show the loan amount after each subsequent payoff
double principal=principalCalc;
int month;
StringBuffer buffer=new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int f=0; f<yearsCalc; f++) {
month=f+1;
double interest=principal*rateCalc;
double balance=principal+interest-monthlyPayment;
buffer.append(month+"\t");
buffer.append(new String(df.format(principal))+"\t");
buffer.append(new String(df.format(interest))+"\t");
buffer.append(new String(df.format(balance))+"\n");
principal=balance;
}
payments.setText(buffer.toString());
} catch(Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loanAmount.setText("");
payment.setText("");
payments.setText("");
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
//Config GUI
JPanel panelMort=new JPanel();
panelMort.setLayout(new GridLayout(5,2));
panelMort.add(loanAmountLabel); panelMort.add(loanAmount);
panelMort.add(yearsLabel); panelMort.add(years);
panelMort.add(new Label()); panelMort.add(chooseYears);
panelMort.add(rateLabel); panelMort.add(rate);
panelMort.add(payLabel); panelMort.add(payment);
JPanel buttons=new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate); buttons.add(clear); buttons.add(quit);
JPanel panelMort2=new JPanel();
panelMort2.setLayout(new BoxLayout(panelMort2, BoxLayout.Y_AXIS));
panelMort2.add(panelMort); panelMort2.add(buttons);
mortCalc.add(BorderLayout.NORTH, panelMort);
mortCalc.add(BorderLayout.CENTER, schedulePane);
}
public static void main(String[] args) {
JApplet applet = new ScrofaniWk3();
JFrame frameMort = new JFrame("ScrofaniWk3");
frameMort.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMort.getContentPane().add(applet);
frameMort.setSize(500,1000);
frameMort.setResizable(true);
frameMort.setVisible(true);
applet.init();
applet.start();
}
}`
`//导入所有需要的功能
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.text.*;
公共类ScrofaniWk3扩展了JApplet{
/**
*
*/
私有静态最终长serialVersionUID=1L;
/**
*@param args
*/
//声明变量并将代码应用程序逻辑放在此处
字符串userInput=null;
JLabel loanAmountLabel=新的JLabel(“贷款金额”);
JTextField loanAmount=新的JTextField();
双[]比率列表={0.0535,0.055,0.0575};
JLabel rateLabel=新JLabel(“利率:”);
JTextField rate=新JTextField();
字符串[]yearsList={“7”、“15”、“30”};
JLabel yearsLabel=新JLabel(“付款年份:”);
JTextField年=新JTextField();
JComboBox chooseYears=新的JComboBox(年度列表);
JLabel payLabel=新的JLabel(“每月付款:”);
JLabel payment=新JLabel();
JButton calculate=新JButton(“计算”);
JButton clear=新JButton(“clear”);
JButton quit=新JButton(“quit”);
JTextArea付款=新的JTextArea();
JScrollPane schedulePane=新的JScrollPane(付款);
容器mortCalc=getContentPane();
公共void init(){
//这使得chooseYears功能
选择耳朵。设置所选索引(0);
chooseYears.addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件选择){
JComboBox cb=(JComboBox)选择.getSource();
字符串termYear=(字符串)cb.getSelectedItem();
年份。setText(术语年);
int指数=0;
开关(整数.parseInt(术语年)){
案例7:指数=0;断裂;
案例15:指数=1;断裂;
案例30:指数=2;断裂;
}
rate.setText(ratesList[index]+“”);
}
});
calculate.addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
试试{
//执行计算
double rateCalc=double.parseDouble(rate.getText())/12;
double principalc=double.parseDouble(loanAmount.getText());
double yearsCalc=Integer.parseInt(years.getText())*12;
双月付款=主要成本*数学成本(1+费率计算,年标度)*费率计算/(数学成本(1+费率计算,年标度)-1);
DecimalFormat df=新的DecimalFormat($######,###.#“);
payment.setText(df.format(monthlyPayment));
//执行额外计算,以显示每次后续付款后的贷款金额
双重委托人=委托人;
整月;
StringBuffer=新的StringBuffer();
append(“Month\tAmount\tInterest\tBalance\n”);
对于(intf=0;fAdd
之后
您只需检查最终添加按钮的位置或保存按钮的容器
检查将JButtons
添加到--buttons的JPanel
然后检查您向--panelMort2添加的按钮
然后检查您添加了哪些内容--无
这只不过是基本逻辑。p.S.它是Container.add(Component,int),所以您应该执行mortCalc.add(panelMort,BorderLayout.NORTH)等操作。
mortCalc.add(BorderLayout.SOUTH, buttons);
mortCalc.add(BorderLayout.NORTH, panelMort);
mortCalc.add(BorderLayout.CENTER, schedulePane);