I hope you can't get kicked off a forum for asking too many questions, but here I am again -- and this is homework.
I am trying to create a GUI for a mortgage payment program.I was able to get the basic shape and format, but am having problems getting the button code in the right spots. I suspect I may not have all my braces correct either (the comments are what I think each brace represents but since I seem to have two "End constructor" braces, there is an error somewhere.
If someone could help me out one more time I would be most grateful!
PS: I have not added the calculations yet -- that will likely spawn another question.
Here is what I have so far:
CODE
/**
* The purpose of this program is to write a Java program using a GUI. The program should
* calculate and display the mortgage payment amount from user input of the amount of the
* mortgage, the term of the mortgage, and the interest rate of the mortgage. The user should
* be able to loop back and enter new data or quit the program.
*
* References: I have used examples and "go by's" from:
* Chapter 8 of Comprehensive Concepts and Techniques, Shelly, Cashman, Starks, & Mick, (2004);
* Jim Powers' Week 2 Mortgage Calculation program posted in the Team A newsgroup; and
* Various Web sources including Dream in Code.
*/
// Import Utilities */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
public class Week2PRG421V2 extends JFrame implements ActionListener
{//begin class
DataOutputStream output;//Per text, p. 8.13, declare output stream
//Declare variables
double principal = 0;
int term = 0;
double rate = 0;
double payment = 0;
//Build graphics - per text, p. 8.13, construct a panel for each row
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
JPanel thirdRow = new JPanel();
JPanel fourthRow = new JPanel();
JPanel fifthRow = new JPanel();
JPanel sixthRow = new JPanel();
JPanel seventhRow = new JPanel();
JPanel eighthRow = new JPanel();
//Per text, p. 8.13, construct a panel for fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
//Per text, p. 8.13, construct labels and text boxes
JLabel principalLabel = new JLabel("Loan Principal (No commas or dollar signs please): ");
JTextField principalNum = new JTextField (15);
JLabel firstNameLabel = new JLabel("Term of Loan (Yrs): ");
JTextField firstName = new JTextField(10);
JTextField lastName = new JTextField(20);
JLabel addressLabel = new JLabel("Interest Rate (%):");
JTextField address = new JTextField (35);
JLabel cityLabel = new JLabel("Your Monthly Payment is: ");
JTextField city = new JTextField(10);
//Build buttons
JButton calculateButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
public static void main(String[] args)
{//begin main ******
//set the look and feel of the interface
try
{//begin try
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}//end try
catch(Exception e)
{//begin catch
JOptionPane.showMessageDialog(null,
"The UIManager could not set the Look and Feel for this application.","Error",
JOptionPane.INFORMATION_MESSAGE);
}//end catch
//configure JFrame object */
Week2PRG421V2 f = new Week2PRG421V2();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set action of exit button on title bar
f.setSize(450,300); //set size of frame
f.setTitle("Mortgage Payment Program"); //set desired text on title bar
f.setResizable(false); //allow or disallow functionality of the resize buttons on the title bar
f.setLocation(300,200); //set horizontal and vertical position of frame on screen
f.setVisible(true); //allow the frame to be seen on screen
}//end main ******
public Week2PRG421V2()
{//begin constructor method
Container c = getContentPane();
c.setLayout((new BorderLayout()));
fieldPanel.setLayout(new GridLayout(8,1));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
fifthRow.setLayout(rowSetup);
sixthRow.setLayout(rowSetup);
seventhRow.setLayout(rowSetup);
eighthRow.setLayout(rowSetup);
//Add fields to rows
firstRow.add(principalLabel);
secondRow.add(principalNum);
thirdRow.add(firstNameLabel);
fourthRow.add(firstName);
fifthRow.add(addressLabel);
sixthRow.add(address);
seventhRow.add(cityLabel);
eighthRow.add(city);
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
fieldPanel.add(fifthRow);
fieldPanel.add(sixthRow);
fieldPanel.add(seventhRow);
fieldPanel.add(eighthRow);
//Add button to panel
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
button.add(calculateButton);
button.add(clearButton);
button.add(exitButton);
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//Add functionality to buttons ******
clearButton.addActionListener(this);
exitButton.addActionListener(this);
calculateButton.addActionListener(this);
//End constructor method
public void actionPerformed(final ActionEvent event)
{ //Begin actionPerformed method ******
final Object command = event.getSource();
if(command == calculateButton)
{ //Begin IF statement for calculateButton
try
{ //Begin TRY statement
Payment();
} //End TRY statement
catch(final NumberFormatException e)
{ //Begin CATCH method
JOptionPane.showMessageDialog(null, "Invaild Entry! Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
} //End CATCH method
} //End IF statement for calculateButton
if(command == clearButton)
{ //Begin IF statement for clearButton
principal_txt.setText(null);
term_txt.setText(null);
rate_txt.setText(null);
payment_txt.setText(null);
displayArea.setText(null);
} //End IF statement for clearButton
if(command == exitButton)
{ //Begin IF statement for exitButton
System.exit(0);
} //End IF statement for exitButton
} //End actionPerformed method ******
public void actionPerformed(ActionEvent e)
{//begin actionPerformed method
}//end actionPerformed method
}//end constructor method
}//end class