Link to home
Start Free TrialLog in
Avatar of neonate928
neonate928

asked on

Java GUI - Mortgage Calculator

I am taking Java II programming and I have an assignment to write a mortgage calculator program with a GUI, that takes a users input, principal, term, and interest and then calculate the monthly payment after the user enters this input and clicks calculate. My program compiles but when I go to run it I get nothing just a lot of "garbage" in command prompt, which I cannot interpret. Could someone please review my code and tell me whats happening?

F.Y.I - As of right now, I only want the user to input a principal and then click on the calculate button, which then will trigger the event to have the principal mulitplied by 10 and then display the result. I only did this to try and start small and then add the rest (interest, term) later once I got this part to work.
import javax.swing.*;
 
//package learn;
 
public class MortgageCalGUI extends javax.swing.JFrame
{
 
	/** Creating a new form MortgageCalGUI **/
	public MortgageCalGUI()
	{
		//call to method Components() to initilize the new from
		Components();
		
	}
 
	/** The Components() method is called within our MortgageCalGUI constructor to initilize our new MortgageCalGUI form **/
	private void Components()
	{
		//creating text fields
		txtfld_principal = new javax.swing.JTextField();
 
		//creating labels for our text fields
		label_principal = new javax.swing.JLabel();
		label_principal.setText("Principal");
 
		label_monthly = new javax.swing.JLabel();
		label_monthly.setText("0");
		
 
		//creating Button
		btn_calculate = new javax.swing.JButton();
		btn_calculate.setText("Calculate");
 
		//event handler for our button btn_calculate
		btn_calculate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				CalculationPerformed(evt);
			}
		});		
		
		//create our "escape" route
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		//create a new frame nameing it Mortgage Calculator
		JFrame frame = new JFrame("MortgageCalculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//set the layout of the window
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(label_principal))
					.addGroup(layout.createSequentialGroup()
						.addComponent(btn_calculate)	
						.addComponent(label_monthly)))
				.addContainerGap(27,Short.MAX_VALUE))
		);
		
		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { btn_calculate, txtfld_principal, txtfld_interest, txtfld_term });
 
		
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(label_principal))
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(btn_calculate)
					.addComponent(label_monthly))
				.addContainerGap(21, Short.MAX_VALUE))
		);
		pack();
	}
	
	private void CalculationPerformed(java.awt.event.ActionEvent evt)
	{
		double principal = (int)(Double.parseDouble(txtfld_principal.getText()));
 
		final double mthly = (principal * 10);
		label_monthly.setText(mthly + " Monthly Payment.");
	}	
 
	public static void main(String[] args)
	{
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MortgageCalGUI().setVisible(true);
			}
		});
	}
 
	//Variable Declarations
	private javax.swing.JLabel label_principal;
	private javax.swing.JLabel label_interest;
	private javax.swing.JLabel label_term;
	private javax.swing.JLabel label_monthly;
 
	private javax.swing.JTextField txtfld_principal;
	private javax.swing.JTextField txtfld_interest;
	private javax.swing.JTextField txtfld_term;
 
	private javax.swing.JButton btn_calculate;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong 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 neonate928
neonate928

ASKER

pkwan:

Hey thanks! I thought I had removed all the interest and term variables. I had them there originally but could not get it to work so I tried to make it more simple to try and fix my "bug". Again thank you!!!!
Thank you much!