S. Gregory Guest
|
Posted: Fri Jun 25, 2004 4:51 am Post subject: Problem with radio butttons functions. Please help. |
|
|
I have almost completed a java program for class and need to finish
the text area which I know how to do, but my program is not reading
the radio buttons needed to perform the calcs. Can anyone solve this
problem. I know I am close, but after a week of trying I need help of
an expert.
Thank you.
First is the application where there is a problem and second is the
calculation application.
-----------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class MortgageFun extends JFrame implements ActionListener,
ItemListener {
double term;
double rate;
double l_amount;
double l_term;
double i_rate;
mortcalc4 pb = new mortcalc4(l_term,i_rate,l_amount);
DecimalFormat dc = new DecimalFormat("$##,##0.00");
JLabel titleLabel = new JLabel("THE MORTGAGE CALCULATOR ",
JLabel.CENTER);
JLabel la = new JLabel("Enter Loan Amount", JLabel.RIGHT);
JTextField loanAmount = new JTextField(10);
JLabel lt = new JLabel(" Enter Loan Years", JLabel.RIGHT);
JTextField loanTerm = new JTextField(10);
JLabel ir = new JLabel("Enter Interest Rate", JLabel.RIGHT);
JTextField interestRate = new JTextField(10);
JLabel mp = new JLabel(" Monthly Payment", JLabel.RIGHT);
JTextField payment = new JTextField(10);
JLabel spacer = new JLabel(" ");
JLabel footLabel = new JLabel("Mortgage Calculator
2004",JLabel.LEFT);
JLabel tableHeader = new JLabel("Payment Number " + "
Loan Balance " +
" Interest ",JLabel.LEFT);
JTextArea results = new JTextArea(15,60);
JScrollPane scroller = new
JScrollPane(results,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JButton calcButton = new JButton("Calculate");
JButton resetButton = new JButton("Reset Fields");
JMenuItem setLoans = new JMenuItem("Selected Rates");
JMenuItem exit = new JMenuItem("Exit");
JMenu usermenu = new JMenu("User Options");
JMenuBar jmb = new JMenuBar();
JLabel loans = new JLabel("Pre Selected Loans",JLabel.LEFT);
JRadioButton l1 = new JRadioButton("7 years at 5.35%");
JRadioButton l2 = new JRadioButton("15 years at 5.50%");
JRadioButton l3 = new JRadioButton("30 years at 5.75%");
public MortgageFun() {
super("Mortgage Calculator - ver. 2.1");
setSize(860,760);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
pane.setLayout(flow);
pane.setBackground(Color.black);
usermenu.setFont(new Font("Arial",Font.BOLD, 15));
usermenu.setBackground(Color.black);
usermenu.setForeground(Color.yellow);
setLoans.setFont(new Font("Arial",Font.BOLD, 15));
setLoans.setBackground(Color.black);
setLoans.setForeground(Color.yellow);
exit.setFont(new Font("Arial",Font.BOLD, 15));
exit.setBackground(Color.black);
exit.setForeground(Color.yellow);
jmb.setBackground(Color.black);
usermenu.add(setLoans);
usermenu.addSeparator();
usermenu.add(exit);
jmb.add(usermenu);
setJMenuBar(jmb);
JPanel row0 = new JPanel();
row0.setBackground(Color.black);
titleLabel.setFont(new Font("Arial",Font.BOLD+Font.ITALIC, 25));
titleLabel.setForeground(Color.yellow);
row0.add(titleLabel);
pane.add(row0);
JPanel row1 = new JPanel();
row1.setBackground(Color.black);
loans.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC, 1 );
loans.setForeground(Color.yellow);
row1.add(loans);
l1.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
l1.setForeground(Color.yellow);
l1.setBackground(Color.black);
row1.add(l1);
l2.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
l2.setForeground(Color.yellow);
l2.setBackground(Color.black);
row1.add(l2);
l3.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
l3.setForeground(Color.yellow);
l3.setBackground(Color.black);
row1.add(l3);
pane.add(row1);
JPanel row2 = new JPanel();
row2.setBackground(Color.black);
la.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
la.setForeground(Color.white);
row2.add(la);
loanAmount.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
loanAmount.setForeground(Color.black);
row2.add(loanAmount);
row2.add(spacer);
lt.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
lt.setForeground(Color.white);
row2.add(lt);
loanTerm.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
loanTerm.setForeground(Color.black);
row2.add(loanTerm);
pane.add(row2);
JPanel row3 = new JPanel();
row3.setBackground(Color.black);
ir.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
ir.setForeground(Color.white);
row3.add(ir);
interestRate.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
interestRate.setForeground(Color.black);
row3.add(interestRate);
row3.add(spacer);
mp.setFont(new Font("Comic Sans MS",Font.BOLD, 15));
mp.setForeground(Color.white);
row3.add(mp);
payment.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
payment.setForeground(Color.green);
payment.setBackground(Color.black);
payment.setEditable(false);
row3.add(payment);
pane.add(row3);
JPanel row4 = new JPanel();
results.setLineWrap(true);
results.setWrapStyleWord(true);
results.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
results.setForeground(Color.black);
row4.add(results);
row4.add(scroller);
row4.setBackground(Color.yellow);
pane.add(row4);
JPanel row5 = new JPanel();
calcButton.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
calcButton.setForeground(Color.white);
calcButton.setBackground(Color.black);
row5.add(calcButton);
resetButton.setFont(new Font("Comic Sans MS",Font.BOLD+Font.ITALIC,
15));
resetButton.setForeground(Color.white);
resetButton.setBackground(Color.black);
row5.setBackground(Color.yellow);
row5.add(resetButton);
pane.add(row5);
JPanel row6 = new JPanel();
footLabel.setFont(new Font("Verdana",Font.BOLD,12));
footLabel.setForeground(Color.yellow);
row6.setBackground(Color.black);
row6.add(footLabel);
pane.add(row6);
setContentPane(pane);
setVisible(true);
loanAmount.addActionListener(this);
loanTerm.addActionListener(this);
interestRate.addActionListener(this);
calcButton.addActionListener(this);
resetButton.addActionListener(this);
setLoans.addActionListener(this);
exit.addActionListener(this);
l1.addItemListener(this);
l2.addItemListener(this);
l3.addItemListener(this);
}
public static void main(String[] arguments) {
MortgageFun MK84 = new MortgageFun();
}
public Insets getInsets() {
Insets space = new Insets(50, 25, 15, 25);
return space;
}
public void itemStateChanged(ItemEvent evt) {
Object item = evt.getSource();
if (item == l1) {
l2.setSelected(false);
if (l3.isSelected())
l3.setSelected(false);
}
else if (item == l2) {
if (l1.isSelected())
l1.setSelected(false);
if (l3.isSelected())
l3.setSelected(false);
}
else if (item == l3) {
if (l1.isSelected())
l1.setSelected(false);
if (l2.isSelected())
l2.setSelected(false);
}
}
public void actionPerformed(ActionEvent evt) {
Object item = evt.getSource();
if (item == calcButton) {
pb.amount = Double.valueOf(loanAmount.getText()).doubleValue();
if (item != setLoans) {
pb.term = Integer.valueOf(loanTerm.getText()).intValue();
pb.interest = Double.valueOf(interestRate.getText()).doubleValue();
}
if (l1.isSelected())
{ pb.term = 7;
pb.interest = 5.35;}
if (l2.isSelected())
{ pb.term = 15;
pb.interest = 5.50;}
if (l3.isSelected())
{ pb.term = 30;
pb.interest = 5.75;}
double rs = pb.calcpayment();
payment.setText(String.valueOf(dc.format(rs)));
}else if (item == resetButton) {
loanAmount.setText(null);
loanTerm.setText(null);
lt.setVisible(true);
loanTerm.setVisible(true);
interestRate.setText(null);
ir.setVisible(true);
interestRate.setVisible(true);
payment.setText(null);
l1.setSelected(false);
l2.setSelected(false);
l3.setSelected(false);
results.setText(null);
}else if (item == exit) {
System.exit(0);
}else if (item == setLoans) {
l1.setVisible(true);
l2.setVisible(true);
l3.setVisible(true);
ir.setVisible(false);
lt.setVisible(false);
interestRate.setVisible(false);
loanTerm.setVisible(false);
}
// Not finished with text area yet.
}
}
//End of App.
NOW FOR THE CALC APP>
-------------------------------------------------------------------------------
import java.text.*;
public class mortcalc4
{
public double[] bal = new double[500];
public double[] intr = new double[500];
public double[] pri = new double[500];
public double term;
public double balance;
public double monthlypay;
public double amount;
public double interest;
public mortcalc4(double init_term, double init_interest, double
init_amount)
{
term = init_term * 12;
interest = init_interest / 100;
amount = init_amount;
}
public double calcpayment()
{
int col = 0;
monthlypay = amount * ((interest / (12*100)) / (1 -
Math.pow(((interest / (12*100)) + 1),(-1 * (term*12)))));
balance = amount;
for (col = 0; col < term; col++)
{
balance -= monthlypay - (balance * (interest/(12*100)));
bal[col] = balance;
intr[col] = balance * (interest/(12*100));
pri[col] = monthlypay - (balance * (interest/(12*100)));
}
return monthlypay;
}
}
|
|