Results 1 to 7 of 7
  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default small issues with a program

    I got everything done on my program with a couple minor issues...first my clear button in the menu exits my program and my exit the program button does not do anything and everything looks right on my end...the other issue is I am getting two errors in the parsing and it looks right to me but I guess I am doing something wrong:

    instructions:

    1. 4 Labels (align them to the right by using the parameter Label.RIGHT when you construct your label… Label lengthLabel = new Label(“Enter the length: “, Label.RIGHT);)
    2. 4 TextFields (2 for input and 2 for output… your output fields should be read-only). All of your TextFields can be length 10.
    3. 2 Buttons… one to calculate the area and perimeter and one to exit… use the labels for the buttons shown below
    area = length * width
    perimeter = 2 * (length + width)
    4. create a menu for File -> Exit (to terminate the application)
    5. create a menu for Edit -> Clear (to clear all TextFields)
    6. only calculate/display the area and perimeter if length and width are valid and greater than zero… if not, display an error message in a dialog box, clear out all TextFields, and put the cursor in the length field.
    7. when you click the Calculate button, be sure all 4 values in the TextFields are formatted to two decimal places with a comma in the thousands place (see below)
    8. be sure to include code to terminate the application when the X is clicked
    9. use a GridLayout for the frame with 5 rows, 2 columns, and 5 pixels for spacing horizontally and vertically
    10. utilize the setActionCommand() method to allow for only one “exit” section of code in your actionPerformed() method
    11. have the Frame display in the center of the monitor with a width of 350 and height of 200
    12. have the Frame title of “Area and Perimeter of a Rectangle”


    Java Code:
    //import packages
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    
    //create a subclass at the fram class
    public class RectangleApp extends Frame implements ActionListener
    {
    	//construct variables
    	private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
    	private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
    	private Label areaLabel = new Label("Area: ", Label.RIGHT);
    	private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
    	private Panel topPanel;
    	private Button calculateButton = new Button("Calculate");
    	private Button exitButton = new Button("Exit the Program");
    	private TextField lengthField = new TextField(10);
    	private TextField widthField = new TextField(10);
    	private TextField areaField = new TextField(10);
    	private TextField perimeterField = new TextField(10);
    	private boolean first;
    	private boolean clearText;
    	private DecimalFormat calcPattern;
    	private double length;
    	private double width;
    	private double area;
    	private double perimeter;
    
    	//constructor method
    	public RectangleApp()
    	{
    		//create an instance of the menu
    		MenuBar mnuBar = new MenuBar();
    		setMenuBar(mnuBar); //display the previously constructed MenuBar
    
    		//construct and populate File Menu
    		Menu mnuFile = new Menu("File"); //create a command on the MenuBar
    		mnuBar.add(mnuFile);
    		MenuItem mnuFileExit = new MenuItem("Exit");
    
    		//construct a command to go under a menu
    		mnuFile.add(mnuFileExit);
    
    		//construct and populate the edit menu
    		Menu mnuEdit = new Menu("Edit");
    		mnuBar.add(mnuEdit);
    		MenuItem mnuEditClear = new MenuItem("Clear");
    		mnuEdit.add(mnuEditClear);
    
    		//register the action listener with each of the menuitems
    		mnuFileExit.addActionListener(this);
    		mnuEditClear.addActionListener(this);
    
    		//assign an ActionCommand to each of the MenuItems
    		mnuFileExit.setActionCommand("Exit");
    		mnuEditClear.setActionCommand("Clear");
    
    		//construct components and initialize beginning values
    		topPanel = new Panel();
    		calcPattern = new DecimalFormat("###,###.##");
    		topPanel.add(lengthLabel);
    		topPanel.add(lengthField);
    		length = 0.0;
    		topPanel.add(widthLabel);
    		topPanel.add(widthField);
    		width = 0.0;
    		topPanel.add(areaLabel);
    		topPanel.add(areaField);
    		area = 0.0;
    		areaField.setEditable(false);
    		topPanel.add(perimeterLabel);
    		topPanel.add(perimeterField);
    		perimeter = 0.0;
    		perimeterField.setEditable(false);
    		topPanel.add(calculateButton);
    		topPanel.add(exitButton);
    
    		//set layouts for the Frame and Panels
    		setLayout(new BorderLayout());
    		topPanel.setLayout(new GridLayout(5, 2, 5, 5));
    
    		//add components to frame
    		add(topPanel, BorderLayout.NORTH);
    
    
    		//allow the x to close the application
    		addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		} //end window adapter
    		);
    	} //end calculator[] constructor method
    		public static void main(String args [])
    		{
    			//construct an instance of the Frame (Calculator)
    			RectangleApp f = new RectangleApp();
    			f.setTitle("Area and Perimeter of a Rectangle");
    			f.setSize(350, 200);
    			f.setLocationRelativeTo(null);
    			f.setVisible(true);
    		} //end main
    		public void actionPerformed(ActionEvent e)
    		{
    			//test for menu item clicks
    			String arg = e.getActionCommand();
    
    		//exit was clicked
    		if(arg == "Exit");
    		System.exit(0);
    
    		//clear was clikced
    		if(arg.equals("Clear"))
    		{
    			clearText = true;
    			first = true;
    
    		} //end if about
    
    		//Calculate button was clicked
    		{
    			if(arg.equals("Calculate"))
    
    			convert data in TextField to int
    			double length = Double.parseDouble(lengthField.getText());
    			double width = Double.parseDouble(widthField.getText());
    			double area = Double.parseDouble(areaField.getText());
    			double perimeter = Double.parseDouble(perimeterField.getText());
    			area = length * width;
    			perimeter = length + width;
    		} //end the if go
    
    		//exitbutton was clicked
    		if(arg.equals("Exit the program"))
    		{
    			System.exit(0);
    		} //end the if exit
    
    	} //end action performed
    } //end class
    by the way I know I have not done error checking just wanna make sure everything is right first before I check for errors

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Ok, what is your question?
    freedom exists in the world of ideas

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, first explain your question properly. And post your code where you stuck with, and also it is better if others can test your code easily.

  4. #4
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default

    my issue is that the exit the application will not exit...it does not do anything

    Also the errors I am getting are:

    C:\Documents and Settings\Don & Diane Kruep\My Documents\RectangleApp.java:131: ';' expected
    Calculate button was clicked
    ^
    C:\Documents and Settings\Don & Diane Kruep\My Documents\RectangleApp.java:131: cannot resolve symbol
    symbol : class Calculate
    location: class RectangleApp
    Calculate button was clicked
    ^
    2 errors

    Tool completed with exit code 1

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Try this jim, you have add action listener in wrong way.

    Java Code:
           // exit button
                exitButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I do this just for fun ;)

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import javax.swing.JOptionPane;
    
    //create a subclass at the fram class
    public class RectangleApp extends Frame implements ActionListener
    {
        //initialize variables
        private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
        private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
        private Label areaLabel = new Label("Area: ", Label.RIGHT);
        private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
        private Panel topPanel;
        private Button calculateButton = new Button("Calculate");
        private Button exitButton = new Button("Exit the Program");
        private TextField lengthField = new TextField(10);
        private TextField widthField = new TextField(10);
        private TextField areaField = new TextField(10);
        private TextField perimeterField = new TextField(10);
        private double length;
        private double width;
        private double area;
        private double perimeter;
    
        public RectangleApp() // Default constructor
        {
                //create an instance of the menu
            MenuBar mnuBar = new MenuBar();
            setMenuBar(mnuBar); //display the previously constructed MenuBar
    
            //construct and populate File Menu
            Menu mnuFile = new Menu("File"); //create a command on the MenuBar
            mnuBar.add(mnuFile);
            MenuItem mnuFileExit = new MenuItem("Exit");
    
            //construct a command to go under a menu
            mnuFile.add(mnuFileExit);
    
            //construct and populate the edit menu
            Menu mnuEdit = new Menu("Edit");
            mnuBar.add(mnuEdit);
            MenuItem mnuEditClear = new MenuItem("Clear");
            mnuEdit.add(mnuEditClear);
    
            //register the action listener with each of the menuitems
            mnuFileExit.addActionListener(this);
            mnuEditClear.addActionListener(this);
    
            //assign an ActionCommand to each of the MenuItems
            mnuFileExit.setActionCommand("Exit");
            mnuEditClear.setActionCommand("Clear");
    
            //construct components and initialize beginning values
            topPanel = new Panel();
            topPanel.add(lengthLabel);
            topPanel.add(lengthField);
            length = 0.0;
            topPanel.add(widthLabel);
            topPanel.add(widthField);
            width = 0.0;
            topPanel.add(areaLabel);
            topPanel.add(areaField);
            area = 0.0;
            areaField.setEditable(false);
            topPanel.add(perimeterLabel);
            topPanel.add(perimeterField);
            perimeter = 0.0;
            perimeterField.setEditable(false);
            topPanel.add(calculateButton);
            topPanel.add(exitButton);
    
            //set layouts for the Frame and Panels
            setLayout(new BorderLayout());
            topPanel.setLayout(new GridLayout(5, 2, 5, 5));
    
            //add components to frame
            add(topPanel, BorderLayout.NORTH);
    
        // exit button
            exitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
    
        // calculate button
            calculateButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    calculateResults();
                }
            });
        } //end calculator[] constructor method
    		
        public static void main(String args []) {
            //construct an instance of the Frame (Calculator)
            RectangleApp f = new RectangleApp();
            f.setTitle("Area and Perimeter of a Rectangle");
            f.setSize(350, 200);
            f.setLocationRelativeTo(null);
            f.setResizable(false);
            f.setVisible(true);
        } //end main
    
        private void clearText() {
            lengthField.setText(null);
            widthField.setText(null);
            areaField.setText(null);
            perimeterField.setText(null);
        }
        
        private void calculateResults() {
            try{
                length = Double.parseDouble(lengthField.getText());
                width = Double.parseDouble(widthField.getText());
                
                area = length * width;
                perimeter = 2 * (length + width);
                
                areaField.setText(String.valueOf(area));
                perimeterField.setText(String.valueOf(perimeter));
            }
            catch(NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid inputs are found!", "Error!",
                        0);
                clearText();
            }
        }
        
        public void actionPerformed(ActionEvent e) {
            //test for menu item clicks
    	String arg = e.getActionCommand();
    
            //exit was clicked
            if(arg.equals("Exit")) {
                System.exit(0);
            }
    
            //clear was clikced
    	if(arg.equals("Clear")) {
                clearText();
    	} //end if about
    
        } //end action performed
    } //end class

Similar Threads

  1. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 04:38 PM
  2. Small scale Java Editor
    By Greenfrog99 in forum AWT / Swing
    Replies: 0
    Last Post: 01-27-2008, 09:46 PM
  3. Replies: 1
    Last Post: 01-05-2008, 08:48 PM
  4. Small tennis simulation in Java
    By diego in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 02:32 AM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •