0

Hello, I need help on this project for school.
My instructions are "Write a program called OfficeAreaCalculator.java that displays the following prompts using two label components:

Enter the length of the office:
Enter the width of the office:

Have your program accept the user input in two text fields. When a button is clicked, your program should calculate the area of the office and display the area in a text field. This display should be cleared whenever the input text fields receive the focus. A second button should be provided to terminate the application (Exit button)."

and here is my code

/**
 * 
 */
package officeAreaCalculator;

import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 * @author Kevin
 *
 */
public class OfficeAreaCalculator extends JFrame {
	private JFrame mainFrame;
	private JButton calculateButton;
	private JButton exitButton;
	private JTextField lengthField;
	private JTextField widthField;
	private JTextField areaField;
	private JLabel lengthLabel;
	private JLabel widthLabel;
	private JLabel areaLabel;
	private ActionListener chandler;
	private ActionListener ehandler;
	
	
	
	
	/**
	 * 
	 */
	public OfficeAreaCalculator() {
		mainFrame = new JFrame("Office Area Calculator");
		
		calculateButton = new JButton("Calculate area");
		exitButton = new JButton("Exit");
		lengthLabel = new JLabel("Enter the length of the office:");
		widthLabel = new JLabel("Enter the width of the office:");
		areaLabel = new JLabel("Office area:");
		lengthField = new JTextField(5);
		widthField = new JTextField(5);
		areaField = new JTextField(5);
		
		Container c = mainFrame.getContentPane();
		
		c.setLayout(new FlowLayout());
		
		c.add(lengthLabel);
		c.add(lengthField);
		c.add(widthLabel);
		c.add(widthField);
		c.add(areaLabel);
		c.add(areaField);
		c.add(calculateButton);
		c.add(exitButton);
		
		calculateButton.setMnemonic('C');
		exitButton.setMnemonic('x');
		
		mainFrame.setSize(250,150);
		
		mainFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e) {System.exit(0);}
		});
		
		calculateButtonHandler.chandler = new calculateButtonHandler();
		calculateButton.addActionListener(chandler);
		
		ExitButtonHandler.ehandler = new ExitButtonHandler();
		exitButton.addActionListener(ehandler);
		
		FocusHandler fhandler = new FocusHandler();
		lengthField.addFocusListener(fhandler);
		widthField.addFocusListener(fhandler);
		areaField.addFocusListener(fhandler);
		
		mainFrame.show();
		
	}
	
	class calculateButtonHandler implements ActionListener
	{
		public static calculateButtonHandler chandler;

		public void actionPerformed(ActionEvent e)
		{
			DecimalFormat num = new DecimalFormat(",###.##");
			double width;
			double length;
			double area;
			String instring;
			
			instring = lengthField.getText();
			if (instring.equals(""))
			{
				instring = "0";
				lengthField.setText("0");
			}
			length = Double.parseDouble(instring);
			
			instring = widthField.getText();
			if (instring.equals(""))
			{
				instring = "0";
				widthField.setText("0");
			}
			width = Double.parseDouble(instring);
			area = length * width;
			areaField.setText(num.format(area));
		}
	}

	class ExitButtonHandler implements ActionListener
	{
		public static ExitButtonHandler ehandler;

		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
	
	class FocusHandler implements FocusListener
	{
		public void focusGained(FocusEvent e)
		{
			if(e.getSource() == lengthField || e.getSource() == widthField)
			{
				areaField.setText("");
			}
			else if (e.getSource() == areaField)
			{
				areaField.setNextFocusableComponent(calculateButton);
				calculateButton.grabFocus();
			}
		}
	
	public void focusLost(FocusEvent e)
	{
		if(e.getSource() == widthField)
		{
			widthField.setNextFocusableComponent(calculateButton);
		}
	}
	
	
	
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new OfficeAreaCalculator();
	}

}

my problem is that I can't get the error
"The field chandler cannot be declared static; static fields can only be declared in static or top level types"

I'm unsure how to correct this, can anyone help me here?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by rapture
0

This part of the code

class calculateButtonHandler implements ActionListener
	{
		public static calculateButtonHandler chandler;

is declaring an inner class, which cannot have static types.

Declare this class outside of the class definition for OfficeAreaCalculator.

P.S. Not that I think that that is the only problem with your code, but then again, I didn't really even look at it.

0

Thanks,
I got it - here it is

//calculateButtonHandler.chandler = new calculateButtonHandler();

calculateButton.addActionListener(new calculateButtonHandler());

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.