编辑:我的问题似乎在于AutoInfoLoan类的构造函数。一旦程序启动,它就在CombinedPanels类中实例化,因此类中的所有变量都被赋予默认值(其中一些是0)。我正在努力解决这个问题,但仍然感谢任何帮助。
我正在用Java制作自动贷款计算器GUI。
我已完成所有GUI;但是,我的问题是让计算部分工作。
是首次启动时的GUI,
在"融资信息"中输入一些值后,是我的GUI。部分并更改其他一些选项并单击"计算"和
是输入这些值后的样子。
以下是我的JPanel
子类(除了顶部横幅之外的所有子类;这与任何计算无关):
**守则墙警告
付款信息:
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class PaymentInformation extends JPanel{
//Declare variables
private JPanel payInfo;
private JLabel loanAmt, monthPay, totalPay, loanVal, monthVal, totalVal;
public PaymentInformation(){
//Give panel layout
payInfo = new JPanel(new GridLayout(3, 2));
//Give titles, set alignment
loanAmt = new JLabel("Total Loan Amount: $", JLabel.LEFT);
monthPay = new JLabel("Monthly Payment: $", JLabel.LEFT);
totalPay = new JLabel("Total Payment: $", JLabel.LEFT);
loanVal = new JLabel("0.0", JLabel.RIGHT);
monthVal = new JLabel("0.0", JLabel.RIGHT);
totalVal = new JLabel("0.0", JLabel.RIGHT);
//Add stuff to JPanel
payInfo.add(loanAmt);
payInfo.add(loanVal);
payInfo.add(monthPay);
payInfo.add(monthVal);
payInfo.add(totalPay);
payInfo.add(totalVal);
//Set border
payInfo.setBorder(BorderFactory.createTitledBorder("Payment Information"));
}
//Method to get the JPanel
public JPanel getGUI(){
return payInfo;
}
//Reset to defaults
public void setDefault(){
loanVal.setText("0.0");
monthVal.setText("0.0");
totalVal.setText("0.0");
}
//Three methods to change the values of the JLabels based on argument received
public void changeLoan(String val){
loanVal.setText(val);
}
public void changeMonth(String val){
monthVal.setText(val);
}
public void changeTotal(String val){
totalVal.setText(val);
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
贷款期限:
@SuppressWarnings("serial")
public class LoanTerm extends JPanel{
private JRadioButton twoFour, threeSix, fourEight, sixZero;
private ButtonGroup loanButtons;
private JPanel lt;
private double ir;
private int loanTerm;
public LoanTerm(){
//Declare buttons, set 24 month as default
twoFour = new JRadioButton("24 Months", true);
threeSix = new JRadioButton("36 Months");
fourEight = new JRadioButton("48 Months");
sixZero = new JRadioButton("60 Months");
//Add all to ButtonGroup
loanButtons = new ButtonGroup();
loanButtons.add(twoFour);
loanButtons.add(threeSix);
loanButtons.add(fourEight);
loanButtons.add(sixZero);
//Create GridLayout within JPanel
lt = new JPanel(new GridLayout(4,1));
lt.add(twoFour);
lt.add(threeSix);
lt.add(fourEight);
lt.add(sixZero);
//Create ActionListeners for buttons
twoFour.addActionListener(new TermListener());
//Border
lt.setBorder(BorderFactory.createTitledBorder("Loan Term"));
ir = 0;
}
//Method to return JPanel
public JPanel getGUI(){
return lt;
}
//Method to reset to default selection
public void setDefault(){
twoFour.setSelected(true);
}
//Next four methods change the interest rate AND loan term integer based on the radio button selection
public void setInterest24(){
ir = 4.5;
loanTerm = 24;
}
public void setInterest36(){
ir = 5.5;
loanTerm = 36;
}
public void setInterest48(){
ir = 6.5;
loanTerm = 48;
}
public void setInterest60(){
ir = 7.0;
loanTerm = 60;
}
//Return the interest rate
public double returnInterest(){
return ir;
}
//Return loan term
public int returnLoanTerm(){
return loanTerm;
}
private class TermListener implements ActionListener{
//TODO change interest rate based on selection
//If a certain button is pressed, interest rate is changed based on selection
@Override
public void actionPerformed(ActionEvent e) {
if(twoFour.isSelected()){
setInterest24();
}
if(threeSix.isSelected()){
setInterest36();
}
if(fourEight.isSelected()){
setInterest48();
}
if(sixZero.isSelected()){
setInterest60();
}
}
}
}
带选项的价格:
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class PriceWithOptions extends JPanel{
private JPanel pwo;
private JCheckBox trans, brake, sun, nav, audio;
private double total;
public PriceWithOptions(){
//Set GridLayout within JPanel
pwo = new JPanel(new GridLayout(5, 1));
//Declare JCheckBoxes (make AntiLock Brakes default checked)
trans = new JCheckBox("Auto Transmission: $1,800");
brake = new JCheckBox("Anti-Lock Brakes: $1,200", true);
sun = new JCheckBox("Sun Roof: $800");
nav = new JCheckBox("Navigation System: $1,350");
audio = new JCheckBox("Audio Package: $1,550");
//Add CheckBoxes to the JPanel
pwo.add(trans);
pwo.add(brake);
pwo.add(sun);
pwo.add(nav);
pwo.add(audio);
//Add border
pwo.setBorder(BorderFactory.createTitledBorder("Price with Options"));
}
//Method to return JPanel
public JPanel getGUI(){
return pwo;
}
//Set defaults
public void setDefault(){
trans.setSelected(false);
brake.setSelected(true);
sun.setSelected(false);
nav.setSelected(false);
audio.setSelected(false);
}
//Method to calculate the total costs of selected options
public double calculateCost(){
total = 0;
if(trans.isSelected()){
total += 1800;
}
if(brake.isSelected()){
total += 1200;
}
if(sun.isSelected()){
total += 800;
}
if(nav.isSelected()){
total += 1350;
}
if(audio.isSelected()){
total += 1550;
}
return total;
}
}
融资信息:
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FinancingInformation {
private JLabel base, down, tax;
private JTextField baseTxt, downTxt, taxTxt;
private JPanel fi;
public FinancingInformation(){
//Declare panel layout
fi = new JPanel(new GridLayout(3, 2));
//Declare JLabels
base = new JLabel("Base Price: $ ", JLabel.LEFT);
down = new JLabel("Down Payment: $ ", JLabel.LEFT);
tax = new JLabel("Sales Tax: % ", JLabel.LEFT);
//Declare JTextFields
baseTxt = new JTextField("0.0", 15);
downTxt = new JTextField("0.0", 15);
taxTxt = new JTextField("7.0", 15);
//Put it all in JPanel
fi.add(base);
fi.add(baseTxt);
fi.add(down);
fi.add(downTxt);
fi.add(tax);
fi.add(taxTxt);
//Set border
fi.setBorder(BorderFactory.createTitledBorder("Financing Information"));
}
//Method to return JPanel
public JPanel getGUI(){
return fi;
}
//Set Defaults
public void setDefault(){
baseTxt.setText("0.0");
downTxt.setText("0.0");
taxTxt.setText("7.0");
}
//Following methods all convert JLabels into doubles, then return the value
public double returnBaseVal(){
String baseString = baseTxt.getText();
double base = Double.parseDouble(baseString);
return base;
}
public double returnDownVal(){
String downString = downTxt.getText();
double down = Double.parseDouble(downString);
return down;
}
public double returnTaxVal(){
String taxString = taxTxt.getText();
double tax = Double.parseDouble(taxString);
tax = tax/100;
return tax;
}
}
进行所有计算的类(AutoInfoLoan):
import java.text.DecimalFormat;
public class AutoInfoLoan {
private double totalLoanAmount, monthlyPayment, totalPayment, basePrice, optionCost, downPayment, salesTax, salesTaxAmount, interestRate;
private int loanTerm;
PaymentInformation pi;
FinancingInformation fi;
PriceWithOptions pwo;
LoanTerm lt;
DecimalFormat df = new DecimalFormat("#.00");
public AutoInfoLoan(PaymentInformation pi, LoanTerm lt, FinancingInformation fi, PriceWithOptions pwo){
this.pi = pi;
this.lt = lt;
this.fi = fi;
this.pwo = pwo;
basePrice = this.fi.returnBaseVal();
downPayment = this.fi.returnDownVal();
salesTax = this.fi.returnTaxVal();
interestRate = this.lt.returnInterest();
loanTerm = this.lt.returnLoanTerm();
optionCost = this.pwo.calculateCost();
}
//Method to set the salesTaxAmount
public void setSalesTax(){
salesTaxAmount = (basePrice - downPayment + optionCost) * salesTax;
}
//Method to set total loan amount
public void setTotalLoanAmount(){
totalLoanAmount = basePrice - downPayment + optionCost + salesTaxAmount;
}
//Method to set the monthly payment
public void setMonthlyPayment(){
double rate = interestRate / 12;
monthlyPayment = totalLoanAmount * rate / (1 - (Math.pow(1/(1+rate), loanTerm)));
}
//Method to set total payment
public void setTotalPayment(){
totalPayment = monthlyPayment * loanTerm + downPayment;
}
//Three methods that call change methods within the PaymentInformation class to change the JLabel values
public void returnTotalLoanAmount(){
pi.changeLoan(df.format(totalLoanAmount));
}
public void returnMonthlyPayment(){
pi.changeMonth(df.format(monthlyPayment));
}
public void returnTotalPayment(){
pi.changeTotal(df.format(totalPayment));
}
//Method to execute everything, called upon in the ActionButton listener
public void executeClass(){
setSalesTax();
setTotalLoanAmount();
setMonthlyPayment();
setTotalPayment();
returnTotalLoanAmount();
returnMonthlyPayment();
returnTotalPayment();
}
}
最后,一个类将所有JPanel放在一起组成一个JFrame并创建一个计算类的实例:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class CombinedPanels extends JFrame{
public CombinedPanels(){
setTitle("Auto Loan Calculator");
setSize(700,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel center = new JPanel(new GridLayout(2, 2, 20, 20));
//Add other classes to this layout
TitleBar tb = new TitleBar();
PaymentInformation pi = new PaymentInformation();
LoanTerm lt = new LoanTerm();
FinancingInformation fi = new FinancingInformation();
PriceWithOptions pwo = new PriceWithOptions();
AutoInfoLoan ail = new AutoInfoLoan(pi, lt, fi, pwo);
ActionButtons ab = new ActionButtons(pi, lt, fi, pwo, ail);
//Add JPanels
add(tb.getGUI(), BorderLayout.NORTH);
add(ab.getGUI(), BorderLayout.SOUTH);
//Add center JPanel to the center of BorderLayout
add(center, BorderLayout.CENTER);
//Continue with adding rest of classes to center JPanel
center.add(pi.getGUI());
center.add(lt.getGUI());
center.add(fi.getGUI());
center.add(pwo.getGUI());
}
}
我相信我的问题的开始就在于" Price with Options"部分。该JPanel中的默认选择是Antilock制动器,其成本为1200."总贷款金额"无论输入或选择了哪些信息,支付信息类中的JLabel似乎总是计算出1200 + 7%的销售税(也是该JTextField的默认值)。我一直在努力工作几个小时没有运气,所以对这个问题的任何见解都非常感激。
答案 0 :(得分:0)
之所以这样,是因为你一直没有运气好几个小时,因为应用程序的业务逻辑与GUI混合在一起。很难跟踪计算流程,因为它们是根据GUI的工作方式建模的,而不是如何计算贷款。
查看this question的第一个答案。我认为它将为您提供有关如何组织代码的答案。一旦你有一个好的设计实现,bug的来源可能是显而易见的。
答案 1 :(得分:0)
我弄清楚我做错了什么。首先,只有一个JRadioButton上有一个ActionListener(愚蠢的我)。此外,一旦程序启动,AutoInfoLoan类(计算类)中的值都将设置为默认值。我不得不在该类中添加一些额外的setter方法来实际获得正确的数字。希望这可能有助于其他人,所以他们不会重复我的愚蠢错误。