Hello experts! I'm working on this good 'ole mortgage calculator program, and trust me...I've looked for solutions and I've stared at my code for days and compared, but I cannot get the radio button row to appear below the principle input box instead of across the top. Simple, I'm sure. But I'm stumpled. Now I know this program has other issues/problems going on before it does what it's meant to do, but for now I want to get the GUI set up. Could you please take a look and see what I'm doing wrong? Thank you in advance!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent
;
import java.awt.event.ActionListe
ner;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBo
rder;
public class MortgageCalculator extends JFrame implements ActionListener
{
double rate = 0; //APR
double monthlyPayment = 0; //Monthly payment amount
double principle = 0; //Amount of loan
int term = 0; //Mortgage term in years
double interest = 0; //Interest, in months
int notePeriod = 0; //Mortgage term in months
String mTerm[] = {"7", "15", "30"};
String mRate[] = {"5.35%", "5.50%", "5.75%"};
//Labels and Input Fields
JPanel row1 = new JPanel();
JLabel mortgageLabel = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);
JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel principleLabel = new JLabel("Mortgage Principle $",JLabel.LEFT);
JTextField principleTxt = new JTextField(8);
JPanel radioPanel = new JPanel();
JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);
JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
JTextField monthlyPaymentTxt = new JTextField(8);
//Create buttons
JPanel button = new JPanel(new FlowLayout(FlowLayout.CENT
ER, 8, 8));
JButton amortizeButton = new JButton("Amortize Payments");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
JButton calculateButton = new JButton("Calculate");
JTextArea displayArea = new JTextArea(10, 30);
JScrollPane scroll = new JScrollPane(displayArea);
public MortgageCalculator()
{
super ("Mortgage Payment Calculator"); //Header
setSize(450, 400); //GUI frame size
setDefaultCloseOperation(J
Frame.EXIT
_ON_CLOSE)
;
Container pane = getContentPane();
Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Border around elements inside frame
//Pane and row properties, GUI layout
JPanel panel = new JPanel(new GridLayout(4,1));
panel.add(row1);
row1.add(mortgageLabel);
row1.setMaximumSize( new Dimension( 1000, row1.getMinimumSize().heig
ht));
row1.setBorder( rowborder);
panel.add(row2);
row2.add(principleLabel);
row2.add(principleTxt);
row2.setMaximumSize( new Dimension( 1000, row2.getMinimumSize().heig
ht));
row2.setBorder( rowborder);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(buttonA);
bgroup.add(buttonB);
bgroup.add(buttonC);
radioPanel.setLayout(new FlowLayout(FlowLayout.RIGH
T, 4, 4 ));
radioPanel.add(buttonA);
radioPanel.add(buttonB);
radioPanel.add(buttonC);
pane.add(radioPanel);
radioPanel.setMaximumSize(
new Dimension( 10000, radioPanel.getMinimumSize(
).height))
;
radioPanel.setBorder( rowborder);
panel.add(row5);
row5.add(monthlyPaymentLab
el);
row5.add(monthlyPaymentTxt
);
monthlyPaymentTxt.setEnabl
ed(false);
//Set payment amount uneditable
row5.setMaximumSize( new Dimension( 1000, row5.getMinimumSize().heig
ht));
row5.setBorder( rowborder);
panel.add(button); //Add buttons
button.add(calculateButton
);
button.add(clearButton);
button.add(exitButton);
button.add(amortizeButton)
;
pane.add(panel);
button.setMaximumSize( new Dimension( 1000, button.getMinimumSize().he
ight));
scroll.setBorder(BorderFac
tory.creat
eEmptyBord
er(10,10,1
0,10));
pane.add(scroll);
pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);
amortizeButton.addActionLi
stener(thi
s); //Add listeners
clearButton.addActionListe
ner(this);
exitButton.addActionListen
er(this);
calculateButton.addActionL
istener(th
is);
buttonA.addActionListener(
this);
buttonB.addActionListener(
this);
buttonC.addActionListener(
this);
}
public void actionPerformed(ActionEven
t event)
{
Object command = event.getSource();
if(command == calculateButton)
{
try
{
principle = Double.parseDouble(princip
leTxt.getT
ext());
}
catch(NumberFormatExceptio
n e)
{
//Select rate and term
if(buttonA.isSelected() == true)
{
rate = 5.35;
term = 7;
}
else if(buttonB.isSelected() == true)
{
rate = 5.5;
term = 15;
}
else if (buttonC.isSelected() == true)
{
rate = 5.75;
term = 30;
}
else //none of the buttons are selected, this is an actual error. Throw an exception
{
JOptionPane.showMessageDia
log(null, "Invaild Entry! Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE)
;
}
}
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
stance(Loc
ale.US);
monthlyPaymentTxt.setText(
myCurrency
Formatter.
format(mon
thlyPaymen
t));
}
if(command == clearButton)
{
principleTxt.setText(null)
;
monthlyPaymentTxt.setText(
null);
displayArea.setText(null);
}
if(command == exitButton)
{
System.exit(0);
}
if (command == amortizeButton)
{
double year = 0;
double interest = 0;
double balance = 0;
double monthlyInterest = 0;
int x = 1;
int months = term * 12;
String titles = "Month\t Principle\tInterest\tBalan
ce\n";
interest = (rate / 100) /12;
balance = principle;
displayArea.setText(titles
);
for (x = 1; x <= months; ++x)
{
//Calculate Detailed Payment Info
monthlyInterest = balance * interest;
if(interest != 0)
year = monthlyPayment - monthlyInterest;
balance = principle - year;
//Format Variables
DecimalFormat df = new DecimalFormat("\u00A4#,##0
.00"); //Crrency
DecimalFormat pf = new DecimalFormat("#,##0.00%")
; //Percentages
DecimalFormat mi = new DecimalFormat("#,##0.000%"
); //Percentages
//Positions the Cursor to the Top of the TextArea
displayArea.setCaretPositi
on(0);
displayArea.append((months
+1) + ")\t"+df.format(principle)
+"\t"+df.f
ormat(inte
rest)+"\t"
+df.format
(balance)+
"\n");
}
}
}
public void Payment()
{
//Get user input
principle = Double.parseDouble(princip
leTxt.getT
ext());
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12;
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
stance(Loc
ale.US);
monthlyPaymentTxt.setText(
myCurrency
Formatter.
format(mon
thlyPaymen
t));
}
public static void main (String[] arguments) //Main Method
{
MortgageCalculator smc = new MortgageCalculator();
smc.setVisible(true);
smc.setDefaultCloseOperati
on(JFrame.
EXIT_ON_CL
OSE);
}
} //End of program