My calculate button won't calculate - Please help

Hey JAVA Gods,

I was wondering if you folks can take a look see at the code below. I can't seem to get the calculate button to work....

/packages importedforclass and method use

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.text.*;

import java.awt.Button;

import java.awt.Graphics;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Label;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

publicclass MortageCalGUIextends JAppletimplements ActionListener{

TextField principalText, paymentText, termText, interestText;

Button calculate;//calculation button

double beginamount;// original principle

double intRate;// interest rate

double years;// length of loan in years

finalint numpayments = 12;//payments per year

NumberFormat format;

publicvoid init(){

// Use a grid bag layout.

GridBagLayout layout =new GridBagLayout();

GridBagConstraints constraints =new GridBagConstraints();

setLayout(layout);

Label heading =new Label("Tony's JAVA Applet Mortgage Calculator - POS407");

Label principalLabel =new Label("Principal(Loan Amount)");

Label termLabel =new Label("Number of Years");

Label interestLabel =new Label("The Interest Rate");

Label paymentLabel =new Label("Your Monthly Payment");

//Set field lengths

principalText =new TextField(10);

termText =new TextField(10);

paymentText =new TextField(10);

interestText =new TextField(10);

//Display payment field/not editable

paymentText.setEditable(false);

calculate =new Button("Calculate It");

// Define the grid bag

constraints.weighty = 1.0;//Distribute space of 1.0

constraints.gridwidth = GridBagConstraints.REMAINDER;//last component

constraints.anchor = GridBagConstraints.NORTH;//top of display/centered horizontally

layout.setConstraints(heading, constraints);

// Anchor most components to the right

constraints.anchor = GridBagConstraints.EAST;

//setting constraints

constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component

layout.setConstraints(principalLabel, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;

layout.setConstraints(principalText, constraints);

constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component

layout.setConstraints(termLabel, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;

layout.setConstraints(termText, constraints);

constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component

layout.setConstraints(interestLabel, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;

layout.setConstraints(interestText, constraints);

constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component

layout.setConstraints(paymentLabel, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;

layout.setConstraints(paymentText, constraints);

constraints.anchor = GridBagConstraints.CENTER;

layout.setConstraints(calculate, constraints);

// Add components

add(heading);

add(principalLabel);

add(principalText);

add(termLabel);

add(termText);

add(interestLabel);

add(interestText);

add(paymentLabel);

add(paymentText);

add(calculate);

// Receive action events

principalText.addActionListener(this);

termText.addActionListener(this);

interestText.addActionListener(this);

calculate.addActionListener(this);

// Number formatting

format = NumberFormat.getInstance();

format.setMinimumFractionDigits(2);

format.setMaximumFractionDigits(2);

}

//User presses calculate

publicvoid actionPerformed(ActionEvent calculate){

repaint();

}

// Displays result of calculations

publicvoid paint(Graphics g){

double result = 0.0;

String principalString = principalText.getText();

String termString = termText.getText();

String interestString = interestText.getText();

try{

if (principalString.length() != 0 && termString.length() != 0

&& interestString.length() != 0){

beginamount = Double.parseDouble(principalString);

years = Double.parseDouble(termString);

intRate = Double.parseDouble(interestString) / 100;

result = calculate();

principalText.setText(format.format(result));

}

showStatus("");// erase any error messages displayed

}catch (NumberFormatException exc){

showStatus("Invalid Input");

paymentText.setText("");

}

}

// Calculate the loan payment

double calculate(){

double first;

double second;

double x, z;

first = intRate * beginamount / numpayments;

x = -(numpayments * years);

z = (intRate / numpayments) + 1.0;

second = 1.0 - Math.pow(z,x);

return first / second;

}

}

[8816 byte] By [aguyfromgua] at [2007-11-27 11:33:36]
# 1

You need to take action when the button is clicked in the action method. The paint method should only be used to 'draw', not to calculate or process any data.

In the init use

JButton.addActionListener(ActionListener);

then in your actionPerformed(ActionEvent) method define the action that your button should cause: the 'calculating'.

//in the init

calculate.addActionListener(this);you can use this as your class implements ActionListener

//then the action method overriding the ActionListener.actionPerformed(ActionEvent)

public void actionPerformed(ActionEvent evt)

{

if (evt.getSource()==calculate)

{

//do calculating here

repaint();

}

//ect, rest of method

}

Hope this helps.

The swing tutorials may come in handy

e.g: http://java.sun.com/docs/books/tutorial/uiswing/

Bamkin

bamkin-ov-lestaa at 2007-7-29 16:52:25 > top of Java-index,Desktop,Core GUI APIs...