Hi ,
For the following code , I intended to have a dialog box with labels balance , payment no : and a button named calculation. When the button will be clicked it should show the next balance according to the equation used in the code .
But its showing balance = 0(initial value for variable class) , payment no =1(the first decleration) and after the desired click its doing nothing. I am also trying to stop it at k=36! I think the problem lies with the event handler!
Can someone please help me out.
Thanks a lot.
RoN
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math.*;
public class BalanceRemaining
{
private static int k =1;
private static int n =36;
private static double i=.0075;
private static double payment = 165.25;
private static double balance ;
private static JLabel balanceLabel;
private static JLabel paymentNo;
private static JButton calculateButton;
private static DecimalFormat dollar = new DecimalFormat( "$###,##0.00" );
private static class ButtonHandler implements ActionListener
{
/*Can not understand the implementation ! Should show the initial balance when k = 1 and then after each click the next balance*What is the error here!:S!?/
public void actionPerformed(ActionEvent event)
{
while ( k<36)
{
balance = payment*((1-Math.pow((1+i),(double)(k-n)))/i);
k++;
}
}
}
public static void main (String[] args)
{
JFrame outputFrame;
Container outputPane;
ButtonHandler buttonListener;
outputFrame= new JFrame();
outputPane=outputFrame.getContentPane();
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputFrame.setSize(225,200);
outputPane.setLayout(new GridLayout(3,1));
[balanceLabel= new JLabel (dollar.format(balance), JLabel.LEFT);
paymentNo= new JLabel("Payment Number: "+ k);
calculateButton= new JButton("Calculate");
buttonListener = new ButtonHandler();
calculateButton.addActionListener(buttonListener);
outputPane.add(balanceLabel);
outputPane.add(paymentNo);
outputPane.add(calculateButton);
outputFrame.setVisible(true);
}
}