Java Program with GUI


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Java Program with GUI

  1. #1
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14

    Unhappy Java Program with GUI

    Hello everyone. I am having a heck of time with this program that I am trying to complete for my Java 2 programming class. If anyone can help me with this, it will be soooo greatly appreciated. I have been working on this for days now, and am completely frustrated and upset. Below is the expectations of the assignment:

    Allow the user to enter the amount of the mortgage and then allow the user to select from a list of mortgage loans: 7 years at 5.35%, 15 years at 5.5%, and 30 years at 5.75%. Use an array of objects for the different loans. Display the mortgage payment amount (which I am showing more information on the output than merely the payment). Allow the user to loop back and enter new data or quit.

    I am receiving 4 errors when I compile, which consist of:
    E:\Programs2\MortgageCalculatorGUI3.java:158: illegal start of type
    if (arg == "Reset")
    ^
    E:\Programs2\MortgageCalculatorGUI3.java:207: <identifier> expected
    }
    ^
    E:\Programs2\MortgageCalculatorGUI3.java:210: 'class' or 'interface' expected
    }
    ^
    E:\Programs2\MortgageCalculatorGUI3.java:210: 'class' or 'interface' expected
    }
    ^
    4 errors

    Here is my coding thus far:

    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;

    public class MortgageCalculatorGUI3 extends Frame implements ActionListener
    {
    /*Set background color of panel or screen*/
    Color lightBlue = new Color(163,187,255);

    /*Declaring and initializing variables*/
    int term = 0;
    double interestRate = 0;
    double principle = 0;
    double payment = 0;
    String ArrayTerm [] = {"7 years", "15 years", "30 years"};
    String ArrayInterestRate [] = {"5.35%", "5.5%", "5.75%"};

    /*Established control buttons*/
    Panel buttonPanel = new Panel();
    Button resetButton = new Button("Reset");
    Button calculateButton = new Button("Calculate");
    Button cancelButton = new Button("Cancel");

    /*Established fields*/
    Panel inputPanel = new Panel();
    Label loanPrinciple = new Label("Loan Principle: $");
    TextField textPrinciple = new TextField(12);
    Label loanTerm = new Label("Term of Loan (Years): ");
    TextField textTerm = new TextField(3);
    Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    TextField textInterest = new TextField(6);

    public MortgageCalculatorGUI3()
    {
    /*Setup data entry screen*/
    this.setLayout(new BorderLayout());
    buttonPanel.setLayout(new FlowLayout());
    inputPanel.setLayout(new FlowLayout());

    /*Setup data entry screen fields*/
    inputPanel.add(loanPrinciple);
    inputPanel.add(principle);
    inputPanel.setBackground(lightBlue);

    /*Buttons to click with mouse*/
    buttonPanel.add(resetButton);
    buttonPanel.add(calculateButton);
    buttonPanel.add(cancelButton);
    buttonPanel.setBackground(lightBlue);

    /*Setup list*/
    comboBox.add(loanTerm);
    comboBox.add(term);
    comboBox.add(loanInterest);
    comboBox.add(interest);

    /*Add location of data entry fields to panel*/
    add(inputPanel, BorderLayout.CENTER);
    /*Add buttons to panel, reset, calculate, and cancel*/
    add(buttonPanel, BorderLayout.SOUTH);
    /*Add list to panel*/
    add(comboBox, BorderLayout.EAST);

    /*Activate button listening*/
    resetButton.addActionListener(this);
    calculateButton.addActionListener(this);
    cancelButton.addActionListener(this);

    /*Established JComboBox*/
    comboBox termList = new JComboBox(termString);
    termList.setSelectedIndex(0);
    termList.addActionListener(this);


    JComboBox interestRateList = new JComboBox(interestRateString);
    interestRateList.setSelectedIndex(0);
    interestRateList.addActionListener(this);

    /*Established exit if user closes panel*/
    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );

    }

    /*Established display*/
    public static void main(String[] args)
    {
    MortgageCalculatorGUI3 frame = new MortgageCalculatorGUI3();
    frame.setBounds(200,200,600,300);
    frame.setTitle("Mortgage Calculator GUI3");
    frame.setVisible(true);
    }

    /*ComboBox update*/
    public void actionPerformed(ActionEvent e)
    {
    JComboBox cb = (JComboBox)e.getSource();
    String arrayTerm = (String)cb.getSelectedItem();
    String arrayInterestRate = (String)
    updateLabel(loanTerm, loanInterestRate);
    }

    /*Resets entries*/
    void clearFields()
    {
    principle.setText("");
    term.setText("");
    interest.setText("");
    }

    /*Button procedures when clicked*/

    String arg = e.getActionCommand();

    /*Resets fields on panel*/
    if (arg == "Reset")
    {
    principle.setText("");
    term.setText("");
    interest.setText("");
    arg = "";
    }

    /*Canels application*/
    if (arg == "Cancel")
    {
    System.exit(0);
    arg="";
    }

    /*Performs calculations on entered data*/
    if (arg == "Calculate")
    {
    DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    double numberOfPayments=0;
    double principleValue=0;
    double interestRate=0;
    try
    {
    principleValue = Double.parseDouble(principle.getText());
    try
    {
    numberOfPayments = Double.parseDouble(term.getText())*12;
    }
    finally
    {
    interestRate = Double.parseDouble(interest.getText())/100/12;
    }
    }
    catch (Exception ex)
    {
    clearFields();
    }

    /*Payment calculation*/
    double payment = (principleValue * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));

    /*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    JOptionPane.showMessageDialog(null," Principal: $"+ TwoDigits.format(principleValue)+
    " Interest Rate: " + TwoDigits.format(interestRate*100*12)+"% Term of Loan: "+ numberOfPayments/12 + " year(s) " +
    "or "+ numberOfPayments + " monthly payments of $" + TwoDigits.format(payment)+
    " totalling $"+TwoDigits.format(payment*numberOfPayments)+". ","Mortgage Calculator GUI3",JOptionPane.INFORMATION_MESSAGE);
    arg="";
    }
    }


    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You have formatting issues (open brace and close brace mismatch). You have declared your method main, close it, then your action performed method, and your clear fields method, then you have more text which is not inside of a class or method definition. This is where your compile errors are occurring.

  3. #3
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Quote Originally Posted by nspils
    You have formatting issues (open brace and close brace mismatch). You have declared your method main, close it, then your action performed method, and your clear fields method, then you have more text which is not inside of a class or method definition. This is where your compile errors are occurring.
    Thank you for helping with those issues. I am still receiving 1 error, illegal start of expression on line 157. I cant seem to see where something is either opened and not closed, or maybe opened and closed incorrectly. If you could point out anything in the coding that you can see, it will be greatly appreciated. This assignment is due by midnight tonight eastern standard time. I have not been able to run the program at all. If you could help me just to get the program to run, I will forever be thankful . Here is the coding thus far:


    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;

    public class MortgageCalculatorGUI3 extends Frame implements ActionListener
    {
    /*Set background color of panel or screen*/
    Color lightBlue = new Color(163,187,255);

    /*Declaring and initializing variables*/
    int term = 0;
    double interestRate = 0;
    double principle = 0;
    double payment = 0;
    String ArrayTerm [] = {"7 years", "15 years", "30 years"};
    String ArrayInterestRate [] = {"5.35%", "5.5%", "5.75%"};

    /*Established control buttons*/
    Panel buttonPanel = new Panel();
    Button resetButton = new Button("Reset");
    Button calculateButton = new Button("Calculate");
    Button cancelButton = new Button("Cancel");

    /*Established fields*/
    Panel inputPanel = new Panel();
    Label loanPrinciple = new Label("Loan Principle: $");
    TextField textPrinciple = new TextField(12);
    Label loanTerm = new Label("Term of Loan (Years): ");
    TextField textTerm = new TextField(3);
    Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    TextField textInterest = new TextField(6);

    Panel comboBox = new Panel();


    public MortgageCalculatorGUI3()
    {
    /*Setup data entry screen*/
    this.setLayout(new BorderLayout());
    buttonPanel.setLayout(new FlowLayout());
    inputPanel.setLayout(new FlowLayout());

    /*Setup data entry screen fields*/
    inputPanel.add(loanPrinciple);
    inputPanel.add(principle);
    inputPanel.setBackground(lightBlue);

    /*Buttons to click with mouse*/
    buttonPanel.add(resetButton);
    buttonPanel.add(calculateButton);
    buttonPanel.add(cancelButton);
    buttonPanel.setBackground(lightBlue);

    /*Setup list*/
    comboBox.add(loanTerm);
    comboBox.add(term);
    comboBox.add(loanInterest);
    comboBox.add(interest);

    /*Add location of data entry fields to panel*/
    add(inputPanel, BorderLayout.CENTER);
    /*Add buttons to panel, reset, calculate, and cancel*/
    add(buttonPanel, BorderLayout.SOUTH);
    /*Add list to panel*/
    add(comboBox, BorderLayout.EAST);

    /*Activate button listening*/
    resetButton.addActionListener(this);
    calculateButton.addActionListener(this);
    cancelButton.addActionListener(this);

    /*Established JComboBox*/
    comboBox termList = new JComboBox(termString);
    termList.setSelectedIndex(0);
    termList.addActionListener(this);


    JComboBox interestRateList = new JComboBox(interestRateString);
    interestRateList.setSelectedIndex(0);
    interestRateList.addActionListener(this);

    /*Established exit if user closes panel*/
    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );

    }

    /*Established display*/
    public static void main(String[] args)
    {
    MortgageCalculatorGUI3 frame = new MortgageCalculatorGUI3();
    frame.setBounds(200,200,600,300);
    frame.setTitle("Mortgage Calculator GUI3");
    frame.setVisible(true);
    }

    /*ComboBox update*/
    public void actionPerformed(ActionEvent e)
    {
    JComboBox cb = (JComboBox)e.getSource();
    String arrayTerm = (String)cb.getSelectedItem();
    String arrayInterestRate = (String)
    updateLabel(loanTerm, loanInterestRate);
    }

    /*Resets entries*/
    public void actionPerformed(ActionEvent e)
    {

    void clearFields()
    {
    principle.setText("");
    term.setText("");
    interest.setText("");

    }

    String arg = e.getActionCommand();

    /*Resets fields on panel*/
    if (arg == "Reset")
    {
    principle.setText("");
    term.setText("");
    interest.setText("");
    arg = "";
    }

    /*Canels application*/
    if (arg == "Cancel")
    {
    System.exit(0);
    arg="";
    }

    /*Performs calculations on entered data*/
    if (arg == "Calculate")
    {
    DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    double numberOfPayments=0;
    double principleValue=0;
    double interestRate=0;
    try
    {
    principleValue = Double.parseDouble(principle.getText());
    try
    {
    numberOfPayments = Double.parseDouble(term.getText())*12;
    }
    finally
    {
    interestRate = Double.parseDouble(interest.getText())/100/12;
    }
    }
    catch (Exception ex)
    {
    clearFields();
    }
    }
    /*Payment calculation*/
    double payment = (principleValue * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));

    /*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    JOptionPane.showMessageDialog(null," Principal: $"+ TwoDigits.format(principleValue)+
    " Interest Rate: " + TwoDigits.format(interestRate*100*12)+"% Term of Loan: "+ numberOfPayments/12 + " year(s) " +
    "or "+ numberOfPayments + " monthly payments of $" + TwoDigits.format(payment)+
    " totalling $"+TwoDigits.format(payment*numberOfPayments)+". ","Mortgage Calculator GUI3",JOptionPane.INFORMATION_MESSAGE);
    arg="";
    }

    }

    Thank you sooooo much for everything .

    Thanks again,
    Nicole

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Nicole:

    It would be helpful if you would enclose your posted code within "code" markers ( the # symbol in the message construction editor) so that a reader can look at the structure of your code without (if so motivated as to do the extra work) pasting your code into another program to create that structure.

    I cannot see what statement is on line 157: is it still the "if (arg == "Reset")" statement, or another?

    I notice that you have two different "actionPerformed" methods. This is the probable source of your error. Did you intend to combine these two? The program will not compile with two listeners ... which one should the program respond to?

  5. #5
    Join Date
    Dec 2005
    Posts
    97
    ok...your code is a mess...i tried fixing some things as you had a lot of errors. i got it to display something but it has no functionality. i'll take a look at it some more is a little while. a lot of your errors had to do with misnaming of variables. the most common was you tried to do principle.setText("") when principle is a double so i changed that to textPrinciple.setText(""). obviously i dont know if this is what you needed, but here's the code that "works". i also commented some things out if i didnt know what you were trying to do or if there were too many errors.

    Code:
    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    
    public class MortgageCalculatorGUI3 extends Frame implements ActionListener
    {
    	/*Set background color of panel or screen*/
    	Color lightBlue = new Color(163,187,255);
    	
    	/*Declaring and initializing variables*/
    	int term = 0;
    	double interestRate = 0;
    	double principle = 0;
    	double payment = 0;
    	String ArrayTerm [] = {"7 years", "15 years", "30 years"};
    	String ArrayInterestRate [] = {"5.35%", "5.5%", "5.75%"};
    	
    	/*Established control buttons*/
    	Panel buttonPanel = new Panel();
    	Button resetButton = new Button("Reset");
    	Button calculateButton = new Button("Calculate");
    	Button cancelButton = new Button("Cancel");
    
    	/*Established fields*/
    	Panel inputPanel = new Panel();
    	Label loanPrinciple = new Label("Loan Principle: $");
    	TextField textPrinciple = new TextField(12);
    	Label loanTerm = new Label("Term of Loan (Years): ");
    	TextField textTerm = new TextField(3);
    	Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    	TextField textInterest = new TextField(6);
    	
    	Panel comboBox = new Panel();
    	
    	
    	public MortgageCalculatorGUI3()
    	{
    		/*Setup data entry screen*/
    		this.setLayout(new BorderLayout());
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
    		
    		/*Setup data entry screen fields*/
    		inputPanel.add(loanPrinciple);
    		//inputPanel.add(principle);
    		inputPanel.setBackground(lightBlue);
    		
    		/*Buttons to click with mouse*/
    		buttonPanel.add(resetButton);
    		buttonPanel.add(calculateButton);
    		buttonPanel.add(cancelButton);
    		buttonPanel.setBackground(lightBlue);
    
    		/*Setup list*/
    		comboBox.add(loanTerm);
    		//comboBox.add(term);
    		comboBox.add(loanInterest);
    		//comboBox.add(interest);
    		
    		/*Add location of data entry fields to panel*/
    		add(inputPanel, BorderLayout.CENTER);
    		/*Add buttons to panel, reset, calculate, and cancel*/
    		add(buttonPanel, BorderLayout.SOUTH);
    		/*Add list to panel*/
    		add(comboBox, BorderLayout.EAST);
    
    		/*Activate button listening*/
    		resetButton.addActionListener(this);
    		calculateButton.addActionListener(this);
    		cancelButton.addActionListener(this);
    		
    		/*Established JComboBox*/
    		JComboBox termList = new JComboBox(ArrayTerm);
    		termList.setSelectedIndex(0);
    		termList.addActionListener(this);
    
    
    		JComboBox interestRateList = new JComboBox(ArrayInterestRate);
    		interestRateList.setSelectedIndex(0);
    		interestRateList.addActionListener(this);
    
    		
    
    	}
    
    	/*Established display*/
    	public static void main(String[] args)
    	{
    	MortgageCalculatorGUI3 frame = new MortgageCalculatorGUI3();
    	frame.setBounds(200,200,600,300);
    	frame.setTitle("Mortgage Calculator GUI3");
    	frame.setVisible(true);
    	}
    
    	/*ComboBox update*/
    	/*public void actionPerformed(ActionEvent e)
    	{
    		JComboBox cb = (JComboBox)e.getSource();
    		String arrayTerm = (String)cb.getSelectedItem();
    		String arrayInterestRate = (String)
    		updateLabel(loanTerm, loanInterestRate);
    	}*/
    
    	/*Resets entries*/
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		/*Resets fields on panel*/
    		if (arg == "Reset")
    		{
    			textPrinciple.setText("");
    			textTerm.setText("");
    			textInterest.setText("");
    			arg = "";
    		}
    
    		/*Canels application*/
    		if (arg == "Cancel")
    		{
    			System.exit(0);
    			arg="";
    		}
    
    		/*Performs calculations on entered data*/
    		if (arg == "Calculate")
    		{
    			DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    			double numberOfPayments=0;
    			double principleValue=0;
    			double interestRate=0;
    			try
    			{
    				principleValue = Double.parseDouble(textPrinciple.getText());
    				try
    				{
    					numberOfPayments = Double.parseDouble(textTerm.getText())*12;
    				}
    				finally
    				{
    					interestRate = Double.parseDouble(textInterest.getText())/100/12;
    				}
    			}
    			catch (Exception ex)
    			{
    				clearFields();
    			}
    		}
    		/*Payment calculation*/
    		payment = (principle * interestRate) / (1 - Math.pow(1 + interestRate, - payment));
    
    		/*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    		/*JOptionPane.showMessageDialog(null," Principal: $"+ TwoDigits.format(principle)+
    		" Interest Rate: " + TwoDigits.format(interestRate*100*12)+"% Term of Loan: "+ payment/12 + " year(s) " +
    		"or "+ numberOfPayments + " monthly payments of $" + TwoDigits.format(payment)+
    		" totalling $"+TwoDigits.format(payment*numberOfPayments)+". ","Mortgage Calculator GUI3",JOptionPane.INFORMATION_MESSAGE);*/
    		arg="";
    	}
    	
    	public void clearFields()
    	{
    		textPrinciple.setText("");
    		textTerm.setText("");
    		textInterest.setText("");		
    	}
    
    }

  6. #6
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Thank you so much for all your help. I am so glad that I can at least compile it. Believe me, I know the coding is a mess . I really have no idea what I am doing. The first programming class that I took was terrible. I purchased a book on Java programming for version 1.4.2, but it does not help me a whole lot. I am just wondering what I have to do to gain functionality effectively. The first thread I created on this site contains the details of what the program is to do. Is there anyway you can help me get the coding to work correctly? I have to have this assignment completed by midnight tonight (EST.), and no one will help me . I have emailed my teacher numerous times for help, but he only tells me what is wrong and not how to fix the errors. Looking forward to hearing from you. Thank you again for everything, it is greatly appreciated more than you will ever know .

    Thanks again,
    Nicole

  7. #7
    Join Date
    Dec 2005
    Posts
    97
    ok, run this and tell me if its closer to what you need.

    Code:
    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    
    public class MortgageCalculatorGUI3 extends Frame implements ActionListener
    {
    	/*Set background color of panel or screen*/
    	Color lightBlue = new Color(163,187,255);
    	
    	/*Declaring and initializing variables*/
    	int term = 0;
    	double interestRate = 0;
    	double principle = 0;
    	double payment = 0;
    	String ArrayTerm [] = {"7 years", "15 years", "30 years"};
    	String ArrayInterestRate [] = {"5.35%", "5.5%", "5.75%"};
    	
    	/*Established control buttons*/
    	Panel buttonPanel = new Panel();
    	Button resetButton = new Button("Reset");
    	Button calculateButton = new Button("Calculate");
    	Button cancelButton = new Button("Cancel");
    
    	/*Established fields*/
    	Panel inputPanel = new Panel();
    	Label loanPrinciple = new Label("Loan Principle: $");
    	TextField textPrinciple = new TextField(12);
    	Label loanTerm = new Label("Term of Loan (Years): ");
    	TextField textTerm = new TextField(3);
    	Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    	TextField textInterest = new TextField(6);
    	
    	Panel comboBox = new Panel();
    	
    	
    	public MortgageCalculatorGUI3()
    	{
    		/*Setup data entry screen*/
    		this.setLayout(new BorderLayout());
    		this.setBackground(lightBlue);
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
    		
    		/*Setup data entry screen fields*/
    		inputPanel.add(loanPrinciple);
    		inputPanel.add(textPrinciple);
    		
    		
    		/*Buttons to click with mouse*/
    		buttonPanel.add(resetButton);
    		buttonPanel.add(calculateButton);
    		buttonPanel.add(cancelButton);
    		buttonPanel.setBackground(lightBlue);
    
    		/*Setup list*/
    		comboBox.add(loanTerm);
    		comboBox.add(textTerm);
    		comboBox.add(loanInterest);
    		comboBox.add(textInterest);
    		
    		/*Add location of data entry fields to panel*/
    		add(inputPanel, BorderLayout.CENTER);
    		/*Add buttons to panel, reset, calculate, and cancel*/
    		add(buttonPanel, BorderLayout.SOUTH);
    		/*Add list to panel*/
    		add(comboBox, BorderLayout.EAST);
    
    		/*Activate button listening*/
    		resetButton.addActionListener(this);
    		calculateButton.addActionListener(this);
    		cancelButton.addActionListener(this);
    		
    		/*Established JComboBox*/
    		JComboBox termList = new JComboBox(ArrayTerm);
    		termList.setSelectedIndex(0);
    		termList.addActionListener(this);
    
    
    		JComboBox interestRateList = new JComboBox(ArrayInterestRate);
    		interestRateList.setSelectedIndex(0);
    		interestRateList.addActionListener(this);		
    
    	}
    
    	/*Established display*/
    	public static void main(String[] args)
    	{
    		MortgageCalculatorGUI3 frame = new MortgageCalculatorGUI3();
    		frame.setBounds(200,200,750,300);
    		frame.setTitle("Mortgage Calculator GUI3");
    		frame.setVisible(true);
    		
    	}
    
    	/*ComboBox update*/
    	/*public void actionPerformed(ActionEvent e)
    	{
    		JComboBox cb = (JComboBox)e.getSource();
    		String arrayTerm = (String)cb.getSelectedItem();
    		String arrayInterestRate = (String)
    		updateLabel(loanTerm, loanInterestRate);
    	}*/
    
    	/*Resets entries*/
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		/*Resets fields on panel*/
    		if (arg == "Reset")
    		{
    			textPrinciple.setText("");
    			textTerm.setText("");
    			textInterest.setText("");
    			arg = "";
    		}
    
    		/*Canels application*/
    		if (arg == "Cancel")
    		{
    			System.exit(0);
    			arg="";
    		}
    
    		/*Performs calculations on entered data*/
    		if (arg == "Calculate")
    		{
    			DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    			double numberOfPayments=0;
    			double principleValue=0;
    			double interestRate=0;
    			try
    			{
    				principleValue = Double.parseDouble(textPrinciple.getText());
    				try
    				{
    					numberOfPayments = Double.parseDouble(textTerm.getText())*12;
    				}
    				finally
    				{
    					interestRate = Double.parseDouble(textInterest.getText())/100/12;
    				}
    			}
    			catch (Exception ex)
    			{
    				clearFields();
    			}
    		
    			/*Payment calculation*/
    			payment = (principle * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));
    	
    			/*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    			JOptionPane.showMessageDialog(null," Principal: $"+ principle +
    			" Interest Rate: " + interestRate*100*12+"% Term of Loan: "+ payment/12 + " year(s) " +
    			"or "+ numberOfPayments + " monthly payments of $" + payment+
    			" totalling $"+payment*numberOfPayments+". ","Mortgage Calculator GUI3",JOptionPane.INFORMATION_MESSAGE);
    			arg="";
    		}
    	}
    	
    	public void clearFields()
    	{
    		textPrinciple.setText("");
    		textTerm.setText("");
    		textInterest.setText("");		
    	}
    
    }
    just where does
    Allow the user to enter the amount of the mortgage and then allow the user to select from a list of mortgage loans: 7 years at 5.35%, 15 years at 5.5%, and 30 years at 5.75%.
    need to come in?

  8. #8
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Thank you, thank you, thank you. Yes, this was what we had to complete last week, which I did not have as much trouble with. The list that we are to select from is for this week. It can come in anywhere.

  9. #9
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Also, we only have to display the mortgage payment based on the principle entered, and the selection made from list. I have more in my output than is required. The term and interest rate need to be in the list, but the principle must be typed in by the user. Just thought I would provide some more information, incase I was confusing you earlier in any way.

    Thanks again,
    Nicole

  10. #10
    Join Date
    Dec 2005
    Posts
    97
    hmm, like this? if this is right, then just work on the calculations...

    Code:
    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class MortgageCalculatorGUI3 extends Frame implements ActionListener, ItemListener
    {
    	/*Set background color of panel or screen*/
    	Color lightBlue = new Color(163,187,255);
    	
    	/*Declaring and initializing variables*/
    	int term = 0;
    	double interestRate = 0;
    	double principle = 0;
    	double payment = 0;
    	String ArrayTerm [] = {"7", "15", "30"};
    	String ArrayInterestRate [] = {"5.35", "5.5", "5.75"};
    	
    	/*Established control buttons*/
    	Panel buttonPanel = new Panel();
    	Button resetButton = new Button("Reset");
    	Button calculateButton = new Button("Calculate");
    	Button cancelButton = new Button("Cancel");
    
    	/*Established fields*/
    	Panel inputPanel = new Panel();
    	Label loanPrinciple = new Label("Loan Principle: $");
    	TextField textPrinciple = new TextField(12);
    	Label loanTerm = new Label("Term of Loan (Years): ");
    	TextField textTerm = new TextField(3);
    	Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    	TextField textInterest = new TextField(6);	
    	Panel comboBox = new Panel();
    	JComboBox interestRateList, termList;
    	
    	
    	public MortgageCalculatorGUI3()
    	{
    		/*Setup data entry screen*/
    		this.setLayout(new BorderLayout());
    		this.setBackground(lightBlue);
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
    		
    		/*Setup data entry screen fields*/
    		inputPanel.add(loanPrinciple);
    		inputPanel.add(textPrinciple);
    		
    		
    		/*Buttons to click with mouse*/
    		buttonPanel.add(resetButton);
    		buttonPanel.add(calculateButton);
    		buttonPanel.add(cancelButton);
    		buttonPanel.setBackground(lightBlue);
    		
    		termList = new JComboBox(ArrayTerm);
    		termList.setSelectedIndex(0);
    		termList.addItemListener(this);
    		
    
    		interestRateList = new JComboBox(ArrayInterestRate);
    		interestRateList.setSelectedIndex(0);
    		interestRateList.addItemListener(this);		
    
    		/*Setup list*/
    		comboBox.add(loanTerm);
    		comboBox.add(termList);		
    		comboBox.add(loanInterest);
    		comboBox.add(interestRateList);
    		
    		/*Add location of data entry fields to panel*/
    		add(inputPanel, BorderLayout.CENTER);
    		/*Add buttons to panel, reset, calculate, and cancel*/
    		add(buttonPanel, BorderLayout.SOUTH);
    		/*Add list to panel*/
    		add(comboBox, BorderLayout.EAST);
    
    		/*Activate button listening*/
    		resetButton.addActionListener(this);
    		calculateButton.addActionListener(this);
    		cancelButton.addActionListener(this);	
    			
    
    	}
    
    	/*Established display*/
    	public static void main(String[] args)
    	{
    		MortgageCalculatorGUI3 frame = new MortgageCalculatorGUI3();
    		frame.setBounds(200,200,750,300);
    		frame.setTitle("Mortgage Calculator GUI3");
    		frame.setVisible(true);
    		
    	}
    
    	
    	public void itemStateChanged(ItemEvent e)
    	{
    		if(e.getStateChange() == ItemEvent.SELECTED)
    		{		
    			if(e.getSource() == termList)
    			{
    				int pos = termList.getSelectedIndex();
    				interestRateList.setSelectedIndex(pos);
    			}
    			if(e.getSource() == interestRateList)
    			{
    				int pos = interestRateList.getSelectedIndex();
    				termList.setSelectedIndex(pos);
    			}
    		}
    	}
    
    	/*Resets entries*/
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		/*Resets fields on panel*/
    		if (arg == "Reset")
    		{
    			textPrinciple.setText("");
    			textTerm.setText("");
    			textInterest.setText("");
    			arg = "";
    		}
    
    		/*Canels application*/
    		if (arg == "Cancel")
    		{
    			System.exit(0);
    			arg="";
    		}
    
    		/*Performs calculations on entered data*/
    		if (arg == "Calculate")
    		{
    			DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    			double numberOfPayments=0;
    			double principleValue=0;
    			double interestRate=0;
    			try
    			{
    				principleValue = Double.parseDouble(textPrinciple.getText());
    				try
    				{
    					numberOfPayments = Double.parseDouble((String)termList.getSelectedItem())*12;
    				}
    				finally
    				{
    					interestRate = Double.parseDouble((String)interestRateList.getSelectedItem())/100/12;
    				}
    			}
    			catch (Exception ex)
    			{
    				clearFields();
    			}
    		
    			/*Payment calculation*/
    			payment = (principle * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));
    	
    			/*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    			JOptionPane.showMessageDialog(null," Principal: $"+ principle +
    			" Interest Rate: " + interestRate*100*12+"% Term of Loan: "+ payment/12 + " year(s) " +
    			"or "+ numberOfPayments + " monthly payments of $" + payment+
    			" totalling $"+payment*numberOfPayments+". ","Mortgage Calculator GUI3",JOptionPane.INFORMATION_MESSAGE);
    			arg="";
    		}
    	}
    	
    	public void clearFields()
    	{
    		textPrinciple.setText("");
    		textTerm.setText("");
    		textInterest.setText("");		
    	}
    
    }

  11. #11
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Oh my God you are a true LIFESAVER!!! This is perfect, it is exactly what I was assigned to do. I will work on the calculations. Just to let you know what kind of person or student that I am, I have posted my little personal biography. I do not want you to think that I am just some student that waited till the last minute to work on their assignments. I am a truely dedicated student, but was having serious problems with the programming classes. I really want to learn this stuff, but these classes move so quickly and do not provide enough theory. Once I get passed these classes, I will be studying more on my own as much as possible to understand this new concept. Below is my biography:

    Honestly, I have always thought of myself as a very happy go lucky individual, who loves to laugh. Personally, I feel laughter is what keeps us going on this long journey through life. In addition to being a happy go lucky person, I have a few hobbies and interests, which consist of spending time with my loved ones, working on personal computer systems, and doing anything creative and fun.
    I decided on this particular college, because many of the colleges in my area do not offer a bachelorís degree program in the field of computers. It has always been my dream to achieve a bachelorís degree in the computer field. About 5 years ago, I began working out of my home as a computer technician, and decided to attend college to educate myself further in this field.

    There are several things I want to accomplish while I am in college. In addition, I have also accomplished one of my goals during my time here. While in school, I will try my best to maintain an honorary grade point average at all times. Also, I want to receive as much knowledge as possible, and retain it. Another goal I want to accomplish is achieving a bachelorís degree in Information Technology. The one goal that I accomplished so far was making the National Deanís List for the 2004/2005 and 2005/2006 school years. I received a letter in the mail that informed me that I had made the National Deanís List, and that my biography would be published in their book for both years. In addition, about 2 months ago I received an invitation to China from the International Scholar Laureate Program, due to my academic achievements thus far at the University. The trip to China was pertaining to a delegation on Technology. Unfortunately, I am unable to accept the invitation due to a lack of time and funds. Hopefully, I will receive this opportunity again in the future when time and money are no longer an issue. Honestly, I have gone through a few disturbing and depressing issues over the last 2 years, and receiving these awards really brought my spirits back up.

    Additionally, I have a large number of goals for the next five to ten years of my life. One goal I have is establishing a computer business of my own. I want to begin with several buildings in the city I live in, and surrounding counties. Afterward, I would like my company to become a state wide computer business. Another goal I have for myself is to be an independent programmer, who receives business opportunities from large name companies. The single largest goal I have for myself is to develop a program for Microsoft Corporation, and Sony.

    Thanks,
    Nicole

  12. #12
    Join Date
    Dec 2005
    Posts
    97
    im glad i could help, you basically had it completely done except for a few minor details. i really only added like 10lines of code. you really sound like a dedicated person and i hope that you are able to accomplish your goal of creating that program. well, if you have any more questions, feel free to ask.

  13. #13
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Thank you very much . I will try my best with the calculations. I cant thank you enough for helping me out. If I get stuck, I will be sure to ask for help.

    Thanks again,
    Nicole

  14. #14
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    I have completed the calculations effectively for my program . Just thought I would share, thank you again for all your help!!

    Thanks again,
    Nicole

Similar Threads

  1. How to get Java GUI to look like Windows
    By DrunkenPirate in forum Java
    Replies: 5
    Last Post: 05-15-2013, 06:21 AM
  2. Logging Java Program
    By sboothman in forum Java
    Replies: 2
    Last Post: 10-16-2006, 03:22 AM
  3. Turning This Program into A JAVA GUI
    By NAN47 in forum Java
    Replies: 2
    Last Post: 04-06-2006, 11:31 PM
  4. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  5. Replies: 1
    Last Post: 06-23-2000, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center