I have successfully completed a project that I have been working on. However, I want to clean the image up slightly. This code consists of 2 different calculators and I would like to divide them with some sort of divider. Can anyone assist? Any help is greatly appreciated.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class Mortgage1 extends JFrame implements ActionListener
{
//This creates the buttons, radio buttons, and input fields
private JTextField LoanInput;
private JTextField LoanAmount;
private JTextArea show;
private JRadioButton selection1;
private JRadioButton selection2;
private JRadioButton selection3;
private JButton Calculate;
private JButton Clear;
private JPanel panelAdder;
private JLabel labela;
private JLabel labelt;
private JLabel labelr;
private JTextField textFieldMortgagePayment;
private JTextField textFieldMortgageTerm;
private JTextField textFieldInterestRate;
private JTextField textFieldTotal;
private JButton buttonCalc;
public Mortgage1()
{
super("Alex Melo, Week 3 individual assignment");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
pack();
// This is where the textfields, buttons, and radio buttons go
LoanInput= new JTextField(10);
LoanAmount = new JTextField(15);
LoanAmount.setEditable (false);
selection1= new JRadioButton("7 year at 5.35%");
selection2= new JRadioButton("15 year at 5.5%");
selection3= new JRadioButton("30 year at 5.75%");
ButtonGroup group = new ButtonGroup();
group.add(selection1);
group.add(selection2);
group.add(selection3);
selection1.setSelected(true);
panelAdder = new JPanel();
labela = new JLabel("Mortgage Amount ");
textFieldMortgagePayment = new JTextField();
labelt = new JLabel("Mortgage Term ");
textFieldMortgageTerm = new JTextField();
labelr = new JLabel("Interest Rate ");
textFieldInterestRate = new JTextField();
textFieldTotal = new JTextField();
buttonCalc = new JButton("Calculate");
buttonCalc.addActionListener(this);
Calculate = new JButton("Calculate");
Calculate.addActionListener(this);
Clear = new JButton ("Clear");
Clear.addActionListener(this);
show = new JTextArea(20,40);
Container content = getContentPane();
JPanel loanPanel = new JPanel(new GridLayout(6,2));
textFieldTotal.setEditable(false);
textFieldTotal.setColumns(10);
textFieldMortgagePayment.setColumns(8);
textFieldMortgageTerm.setColumns(4);
textFieldInterestRate.setColumns(4);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//Put labels, buttons, and text field in panel
loanPanel.add(new JLabel("Enter loan amount: "));
loanPanel.add(LoanInput);
loanPanel.add(new JLabel("PLEASE SELECT YOUR LOAN: "));
loanPanel.add(selection1);
loanPanel.add(new JLabel(" "));
loanPanel.add(selection2);
loanPanel.add(new JLabel(" "));
loanPanel.add(selection3);
loanPanel.add( new JLabel("Your monthly payment total is: "));
loanPanel.add(LoanAmount);
JPanel button = new JPanel();
panelAdder.add(labela);
panelAdder.add(textFieldMortgagePayment);
panelAdder.add(labelt);
panelAdder.add(textFieldMortgageTerm);
panelAdder.add(labelr);
panelAdder.add(textFieldInterestRate);
panelAdder.add(buttonCalc);
panelAdder.add(textFieldTotal);
contentPane.add(panelAdder);
content.add(loanPanel);
// add buttons and text to panel
button.add(Calculate);
button.add(Clear);
content.add(button);
content.add(new JScrollPane(show));
}
//This is where the formula's begin
private int[] mortgageterms = {7,15,30};
private double[] mortgagerates = {.0535,.055,.0575};
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == Calculate)
{
double mortgageamount=0;
int mortgageterm=0;
double mortgagerate=0;
double mortgagepayment;
try{
mortgageamount = Double.parseDouble(LoanInput.getText().trim());
}
catch(NumberFormatException ex1)
{
JOptionPane.showMessageDialog(this,"ERROR!");
return;
}
if(selection1.isSelected())
{
mortgageterm = mortgageterms[0];
mortgagerate = mortgagerates[0];
}
else if(selection2.isSelected())
{
mortgageterm = mortgageterms[1];
mortgagerate = mortgagerates[1];
}
else if(selection3.isSelected())
{
mortgageterm = mortgageterms[2];
mortgagerate = mortgagerates[2];
}
mortgagepayment= mortgageamount * Math.pow(1 + mortgagerate / 12, mortgageterm * 12)* (mortgagerate / 12) / (Math.pow(1 + mortgagerate / 12, mortgageterm * 12) - 1);
LoanAmount.setText("$"+String.format("%.2f", mortgagepayment));
display(mortgageamount, mortgageterm, mortgagerate, mortgagepayment);
}
//This is the action event listener for the clear button
else if ("Clear".equals(e.getActionCommand()))
{
Clear();
}
double MortgagePayment = Double.parseDouble (textFieldMortgagePayment.getText());
double MortgageTerm = Double.parseDouble(textFieldMortgageTerm.getText())*12;
double InterestRate = Double.parseDouble(textFieldInterestRate.getText()) / 100.;
double Total = MortgagePayment * (Math.pow(1+(InterestRate/12), MortgageTerm) * InterestRate/12) / (Math.pow(1 + InterestRate/12, MortgageTerm) - 1);
//decimal format
DecimalFormat df = new DecimalFormat("#,###.##");
textFieldTotal.setText(df.format(Total));
}
//This is the action for the clear button.
public void Clear()
{
LoanInput.setText ("");
LoanAmount.setText("");
LoanInput.requestFocus();
show.setText("");
}
private void display(double mortgageamount, int mortgageterm, double mortgagerate, double mortgagepayment)
{
double mortgagebalance = mortgageamount;
// This shows the full amount of information left (i.e. balance, interest paid, etc)
show.append("-----------------------------------------------------------------------------\n");
show.setText("Month Balance Left Payment Amount Interest Paid\n");
show.append("-----------------------------------------------------------------------------\n");
int count = 0;
for(int month = 0; month <= mortgageterm * 12;month++)
{
double interestPaid = mortgagebalance * mortgagerate/12;
show.append(String.format("%4d $%,10.2f $%,10.2f $%,8.2f\n",
month, mortgagebalance, mortgagepayment, interestPaid));
mortgagebalance = mortgagebalance - mortgagepayment + interestPaid;
if(count++ == 12)
{
count =1;
if(month < mortgageterm*12)
show.append("-----------------------------------------------------------------------------\n");
show.append("Month Balance Left Payment Amount Interest Paid\n");
show.append("-----------------------------------------------------------------------------\n");
}
}
}
public static void main(String[] args){
JFrame display = new Mortgage1();
display.setSize(750,760);
display.setVisible(true);
}
}