CODE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calc_simpleInterest extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
private JLabel principalamountL, investmenttimeL, interestrateL,simpleinterestL;
private JTextField principalamountTF, investmenttimeTF, interestrateTF,simpleinterestTF;
private JButton calculateB, resetB, exitB;
private CalculateButtonHandler cbHandler;
private ResetButtonHandler rbHandler;
private ExitButtonHandler ebHandler;
public Calc_simpleInterest()
{
principalamountL = new JLabel("Principal Amount: ", SwingConstants.LEFT);
investmenttimeL = new JLabel("Investment Time(in years): ", SwingConstants.LEFT);
interestrateL = new JLabel("Interest Rate(%): ", SwingConstants.LEFT);
simpleinterestL = new JLabel("Simple interest",SwingConstants.LEFT);
principalamountTF = new JTextField(10);
investmenttimeTF = new JTextField(10);
interestrateTF = new JTextField(10);
simpleinterestTF = new JTextField(10);
//SPecify handlers for each button and add (register) ActionListeners to each button.
calculateB = new JButton("Calculate Interest");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
resetB = new JButton("Reset(Clear)");
rbHandler = new ResetButtonHandler();
resetB.addActionListener(rbHandler);
exitB = new JButton("Close Program");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
setTitle("Calculation of Simple Interest");
Container pane = getContentPane();
pane.setLayout(new GridLayout(6,2));
//Add things to the pane in the order you want them to appear (left to right, top to bottom)
pane.add(principalamountL);
pane.add(principalamountTF);
pane.add(investmenttimeL);
pane.add(investmenttimeTF);
pane.add(interestrateL);
pane.add(interestrateTF);
pane.add(simpleinterestL);
pane.add(simpleinterestTF);
pane.add(calculateB);
pane.add(resetB);
pane.add(exitB);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double principalamount;
double investmenttime;
double interestrate;
double simpleInterest;
principalamount = Double.parseDouble(principalamountTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
investmenttime = Double.parseDouble(investmenttimeTF.getText());
interestrate = Double.parseDouble(interestrateTF.getText());
simpleInterest = (principalamount * interestrate * investmenttime) / 100.0;
simpleinterestTF.setText("" + simpleInterest);
}
}
public class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
interestrateTF.setText("");
investmenttimeTF.setText("");
principalamountTF.setText("");
simpleinterestTF.setText("");
}
}
public class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Calc_simpleInterest simpObj = new Calc_simpleInterest();
}
}
*Edited to add the [ code] tags. Please
This post has been edited by pbl: 14 Oct, 2008 - 08:37 PM