Welcome to Dream.In.Code
Become a Java Expert!

Join 150,362 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,801 people online right now. Registration is fast and FREE... Join Now!




GUI Program - class, interface or enum expected

 
Reply to this topicStart new topic

GUI Program - class, interface or enum expected, I think my brackets are off - again

Reason4
26 Jan, 2008 - 08:54 PM
Post #1

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

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. wink2.gif

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

User is offlineProfile CardPM
+Quote Post

capty99
RE: GUI Program - Class, Interface Or Enum Expected
26 Jan, 2008 - 10:51 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
you need to close with a bracket right before the end constructor method and then delete the last one in the program.

what are you programming this in? most give you line numbers where the error occured and its super ez to spot.
User is online!Profile CardPM
+Quote Post

Reason4
RE: GUI Program - Class, Interface Or Enum Expected
27 Jan, 2008 - 09:11 AM
Post #3

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

I am not sure where the correct "end constructor" bracket is -- I have one candidate right after the "calculate Button" section and another as the second-to-last line in the program. I tried adding and subtracting brackets in both spots and got more errors.

I am using Textpad and it says the error is on Line 201 -- the last line in the program.

I wonder if it has something to do with the fact that I haven't added an actual calculation for the Calculate button yet? Does the syntax for the buttons I have look okay?

QUOTE(capty99 @ 26 Jan, 2008 - 11:51 PM) *

you need to close with a bracket right before the end constructor method and then delete the last one in the program.

what are you programming this in? most give you line numbers where the error occured and its super ez to spot.


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: GUI Program - Class, Interface Or Enum Expected
27 Jan, 2008 - 11:18 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Capty is right about where you need to close the constructor (right before where you have "//End constructor method". Also be careful when writing java in TextPad. I have tried using the editor with Java and it tends to give this funky error and point to the last line. I think it is because textpad is inserting some strange character at the end of the file. Try using notepad and delete all space from the end of the file to the closing brace (to delete any hidden chars) and recompile. You may find it will work then.

For this reason I just write most Java in notepad.

Hope this helps you out. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month