2015-09-27 77 views
0

编辑:我的问题似乎在于AutoInfoLoan类的构造函数。只要程序启动,它就会在CombinedPanels类中实例化,因此类中的所有变量都被赋予默认值(其中一些为0)。我正在研究这一点,但任何帮助仍然感激。跨多个类更改值

我在Java中制作一个汽车贷款计算器GUI。

我已经完成了所有的GUI;然而,我的问题是让计算部分工作。

Before

是当第一次推出的GUI,

During

是我的GUI中的“金融信息”部分中输入一些值,并改变一些其他的选择,然后点击“计算”之后,和

What if should look like

是WH在输入这些值后应该看起来像。

这里是我的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(); 
} 
} 

最后,一类把所有JPanels连成一个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()); 
} 
} 

我相信我的问题的开始在于“价格与选项”部分。该JPanel中的默认选择是Antilock制动器,其成本为1200.支付信息类中的“总贷款金额”JLabel似乎总是计算出1200 + 7%的销售税(也是该JTextField的默认值),无论输入或选择什么信息。我一直在努力几个小时,没有运气,所以对这个问题的任何见解都非常感谢。

+0

看看[Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)。从本质上讲,您需要某种模型来模拟您的数据并为一个或多个视图提供信息。该模型还可以提供更改时间的通知,允许UI的其他部分相应地更新其状态 – MadProgrammer

+0

您应该先阅读本文:http://stackoverflow.com/help/mcve –

回答

0

原因是因为您一直在寻找它几个小时没有运气是因为该应用程序的业务逻辑与GUI混合在一起。由于它们是根据GUI的工作原理进行建模的,而不是如何计算贷款,所以很难遵循计算流程。

看看第一个回答this question。我想它会给你如何组织你的代码的答案。一旦你实现了一个好的设计,错误的来源可能会很明显。

+0

My Model是AutoInfoLoan类,它是多个面板是视图,并且CombinedPanels以及实例化它的类(LoanCalculateGUI,这里未显示,因为它只有2行)是控制器。我以为我已经有了一些MVC设计。 –

+0

好吧,请仔细考虑一下:您可以更改文本用户界面,而无需修改逻辑,即贷款是如何计算的? – Baltasarq

0

我想清楚我做错了什么。首先,只有一个JRadioButton有一个ActionListener(愚蠢的我)。此外,AutoInfoLoan类(计算类)中的值在程序启动后立即全部设置为默认值。我不得不在该类中添加一些额外的setter方法来实际获得正确的数字。希望这可能会帮助别人,所以他们不会重复我愚蠢的错误。