Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Java GUI - Mortgage Calculator

Avatar of neonate928
neonate928 asked on
Java
3 Comments1 Solution852 ViewsLast Modified:
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;
}
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter KwanFlag of Hong Kong imageAnalyst Programmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers