Link to home
Start Free TrialLog in
Avatar of omoa
omoa

asked on

Problem with ActionEvent and JButton

I can not figure out what I am missing to make my exit button work in the below program. I am sure it is something simple but I am at a lost as to what it could be. The program complies without error and all my other buttons work correctly.  I am believe the problem revolves around the ActionEvent  for my  exit button but can't find it. What am I doing wrong?


import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

class mortCalcWk3 extends JFrame implements ActionListener {

//Establish Container for User Input
JTextField principalText = new JTextField(15);
JTextField yearIntrateText = new JTextField(15);
JTextField numYearsText = new JTextField(15);
JTextField monthlyPaymentsText = new JTextField(15);
JLabel validateLabel = new JLabel();


mortCalcWk3() {
super("McBride Financial Services Mortgage Calculator");
setSize(390, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
pane.setLayout(flow);

JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Loan Amount:$");
row1.add(principalLabel);
row1.add(principalText);
pane.add(row1);

JPanel row2 = new JPanel();
JLabel yearIntrateLabel = new JLabel("Yearly Interest Rate:");
row2.add(yearIntrateLabel);
row2.add(yearIntrateText);
pane.add(row2);

JPanel row3 = new JPanel();
JLabel numYearsLabel = new JLabel("Term of Loan(Years):");
row3.add(numYearsLabel);
row3.add(numYearsText);
pane.add(row3);

JPanel row4 = new JPanel();
JButton calculate = new JButton("Calculate");
calculate.addActionListener(this);
row4.add(calculate);
pane.add(row4);
JButton reset = new JButton("Reset Calculator");
reset.addActionListener(this);
row4.add(reset);
pane.add(row4);
JButton exit = new JButton("Exit?");
exit.addActionListener(this);
row4.add(exit);
pane.add(row4);

JPanel row5 = new JPanel();
JLabel monthlyPaymentsLabel = new JLabel("Total Monthly Payment:");
row5.add(monthlyPaymentsLabel);
row5.add(monthlyPaymentsText);
pane.add(row5);
setContentPane(pane);
setVisible(true);

}

public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Calculate")
compute();
if (command == "Reset Calculator")
clearAllFields();
if (command == "Exit")
exit();}
void compute()
{
try{
validateLabel.setText("");
validateLabel.validate();


Double yearIntrate = new Double (yearIntrateText.getText());
Double principal = new Double (principalText.getText());
Integer numYears = new Integer (numYearsText.getText());

double payFreq = 12;
double interestRateMonths;
double monthlyPayment;
double numPayments;


interestRateMonths = (yearIntrate.doubleValue() / 100) / payFreq;
numPayments = payFreq * numYears.intValue();

double temp = Math.pow(1 + interestRateMonths, (-1*numPayments));
monthlyPayment = principal.doubleValue() * interestRateMonths / (1 - (temp));

//Format used to set two decimal places.
DecimalFormat currency = new DecimalFormat("$0.00");

monthlyPaymentsText.setText(currency.format(monthlyPayment));

} catch(Exception error)
{
validateLabel.setText("Invalid Entry! Try again.");
}
}

void clearAllFields(){
principalText.setText("");
yearIntrateText.setText("");
numYearsText.setText("");
monthlyPaymentsText.setText("");
}
void exit(){
System.exit(0);

}

public static void main(String[] arguments)
{
mortCalcWk3 mortCalcWk3  = new mortCalcWk3 ();
      }
}// end program
ASKER CERTIFIED SOLUTION
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of omoa
omoa

ASKER

UrosVidojevic:

You are a GOD SENT.Thank you so much for being my second set of eyes. It is funny how a simple typo can make you think you are loosing your mind.  Weird how I can figure out the hard stuff but the easy things send me looking for the Tylenol.

Thank you again -500 points award.

OMOA