Hello all, I am taking my second java class and I have tried everything to get my program to compile correctly and keep getting error messages. The program is supposed to display a GUI mortgage where the user can input a principal amount, an interest rate, and hit the calculate button to display the monthly payment. Please help me.. I keep getting the same error over and over(in 6 line of code).. Any help or advice and feedback would be appreciated. Thanks.
Here is what I have so far.
/*
* Written By:
* Date:3/31/2004
* Description: Mortgage Calculator program
* Filename: Mortgage.java
PDL
//* Class: Mortgage
//* Parent: Mortgage Class
//* Attributes: A, I, R, T, P
//* Methods:
//* Design
//* This program will calculates and return a monthly mortgage payment based on user input of
//* principal amount, interest rate and term of loan.
//* The class also has public methods for calculating the amount,
//*
*/
/* establish links to java libraries*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.border.*;
public class Mortgage extends JFrame
{
// declare all the graphic objects
Font headfont = new Font("TimesRoman", Font.PLAIN, 28);
Font basefont = new Font("TimesRomain", Font.PLAIN, 16);
Font messagefont = new Font("TimesRoman", Font.BOLD, 20);
TextField textPrincipal = new TextField(10);
TextField textInterest = new TextField(10);
TextField textYears = new TextField(10);
TextField textPayment = new TextField(10);
CheckboxGroup groupInt = new CheckboxGroup();
Checkbox intmodeAnnual = new Checkbox("Annual Interest",groupInt,true);
Checkbox intmodeMonth = new Checkbox("Monthly Interest",groupInt,false);
// declare user buttons
JButton calcButton = new JButton("Calculate");
JButton exitButton = new JButton("Exit");
JButton resetbutton = new JButton("RESET");
JLabel info = new JLabel("Enter Principal, Interest, and Years.");
short modalDivisor = 12;
public Mortgage()
{
// declare JFrame size
setTitle("Mortgage Calculator Program");
setSize(680, 610);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
JPanel panel = new JPanel(new FlowLayout()); // determines the size and position of each component added to this panel//
panel.setFont(headfont);
pane.add(calcButton);
pane.add(exitButton);
pane.add(resetbutton);
JPanel p = new JPanel(new BorderLayout());
JPanel panelBody = new JPanel();
panelBody.setFont(basefont);
panelBody.setLayout( new GridLayout(5,2,10,10) );
panelBody.add( new Label("Principal") );
panelBody.add(textPrincipal);
panelBody.add( new Label("Modal Interest") );
panelBody.add(textInterest);
panelBody.add( new Label("Years") );
panelBody.add(textYears);
panelBody.add( new Label(" ") );
panelBody.add( new Label(" ") );
panelBody.add( new Label("Mortgage Payment") );
textPayment.setEditable( false );
panelBody.add(textPayment);
add(panelBody,BorderLayout.CENTER);
Panel panelRadio = new Panel();
panelRadio.setFont(basefont);
panelRadio.setLayout( new GridLayout(4,1,5,5) );
panelRadio.add(intmodeAnnual);
panelRadio.add(intmodeMonth);
panelRadio.add( new Label(" ") );
panelRadio.add( new Label(" ") );
add(panelRadio,BorderLayout.WEST);
Panel panelButtons = new Panel();
panelButtons.setFont(basefont);
panelButtons.setLayout( new GridLayout(4,1,10,20) );
panelButtons.add( new Label(" ") );
panelButtons.add(Calculate);
panelButtons.add(JbuttonExit);
panelButtons.add( new Label(" ") );
panelButtons.add(buttonReset);
add(panelButtons,BorderLayout.EAST);
Panel panelFooter = new Panel();
panelFooter.setFont(messagefont);
panelFooter.setForeground(Color.red);
panelFooter.setLayout( new FlowLayout(FlowLayout.CENTER,10,10) );
panelFooter.add(labelMessage);
add(panelFooter,BorderLayout.SOUTH);
// action listeners for buttons
mortgage.addActionListener(this);
reset.addActionListener(this);
}
}// EOF
Java
Knute Johnson - 02 Apr 2004 06:42 GMT
You've got a whole bunch of problems with this code. First you have no
main() so it will never run. You also need to pack() and setVisible()
your overridden frame. You've got several variable names mixed up and
you are attempting to attach an ActionListener to a JFrame. That won't
work. The ActionListener needs to be attached to all of your buttons
and any other components that you want to listen to for ActionEvents.
Then you need to write the ActionListener code. There is no reset
object in your code except the line where you try to add an
ActionListener to it. Here is the basic code required to make a Java
application work like you want one to.
public class XYZ extends JFrame implements ActionListener {
public XYZ() {
// create and add your buttons and labels here
// add the ActionListener this to your buttons
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
// determine which component called with either ActionCommand or
// the source component
// getActionCommand() or getSource()
// put your action code here
}
public static void main(String[] args) {
new XYZ();
}
}

Signature
Knute Johnson
email s/nospam/knute/
Molon labe...
Andrew Thompson - 02 Apr 2004 16:47 GMT
> Hello all, I am taking my second java class
> and I have tried everything to get my program
> to compile correctly and keep getting error messages.
I will be blunt. You need to understand those
6 errors before you can get much further, and for
a polite, detailed response you might try
<http://www.physci.org/codes/javafaq.jsp#cljh>
The mistakes you are making are of that level.
> Please help me.. I keep getting the same error
>over and over(in 6 line of code)..
No, that is 6 separate errors. It became apparent
you called various buttons and a label different
names between when you declared them, and when
you used them.
After correcting those, there was still the action
listener to implement for the other two errors.
I had already corrected the first four when it
occured to me that this code was probably generated
by your IDE - that's when I stopped.
It is not fair to the other members of the
group to post back 347 lines of message/code
you should have trimmed to 50 before you
posted.
>..Any help
>or advice and feedback would be appreciated. Thanks.
Get yourself to the events tutorial at Sun,
<http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html>
and have a look over the best way to prepare
examples for others..
<http://www.physci.org/codes/sscce.jsp>

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology