Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cannot find method : method getText?

807591Apr 23 2008 — edited Apr 23 2008
Hello once again,
I've decided to try and get my code done early. Thus far I have the code pretty much set. The only thing that's holding me back is for some reason it's not letting me set the text in a TextField because it says
D:\JAVA\Week15\RectangleApp.java:170: cannot find symbol
symbol  : method setText(java.lang.Double)
location: class java.awt.TextField
					areaAnswer.setText(area);
I put that in code so it stands out a little easier. Anyway, the program I have is simple. Take the input from the length and width textFields, do some calculations for area and perimeter, then put those seperate answers in their respective textArea's. This is all executed once the "Calculate" button is clicked. The error area is near the end in my actionPerformed(), It's been commented to stand out a bit.
//packages to import
import java.awt.*;//gives us access to color, draw methods, other GUI components (frame, text field)
import java.awt.event.*;//to listen for clicks of buttons and menu items
import java.awt.datatransfer.*;//to transfer data in and out the clipboard
import java.text.DecimalFormat;//to format output
import javax.swing.JOptionPane;//to display dialog boxes

public class RectangleApp extends Frame implements ActionListener
{
	//declare global components as private
	private Button calcButton;
	private Button exitButton;
	private TextField lengthTextField;
	private TextField widthTextField;
	private TextField areaAnswer;
	private TextField perimeterAnswer;
	private boolean clearText;
	private Panel topPanel;
	private Double length;
	private Double width;
	private Double area;
	private Double perimeter;
	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);

		public RectangleApp()
		{
			//create an instance of the MenuBar
			MenuBar mnuBar = new MenuBar();
			setMenuBar(mnuBar); //displays the previously construct MenuBar at the top of our Frame

			//construct and popluate the File Menu
			Menu mnuFile = new Menu("File", true); //true indicates it will be a tear-off menu
			mnuBar.add(mnuFile);
				MenuItem mnuFileExit = new MenuItem("Exit");
			mnuFile.add(mnuFileExit);

			//construct and populate the Edit Menu
			Menu mnuEdit = new Menu("Edit", true);
			mnuBar.add(mnuEdit);
				MenuItem mnuEditClear = new MenuItem("Clear");
			mnuEdit.add(mnuEditClear);

			//listen for exit and clear menu clicks
			mnuFileExit.addActionListener(this);
			mnuEditClear.addActionListener(this);

			//set what happens when the menu item is chosen
			mnuFileExit.setActionCommand("Exit");
			mnuEditClear.setActionCommand("Clear");

			//construct and initialize components
			lengthTextField = new TextField(10);
			widthTextField = new TextField(10);
			areaAnswer = new TextField(10);
				areaAnswer.setEditable(false);
			perimeterAnswer = new TextField(10);
				perimeterAnswer.setEditable(false);
			calcButton = new Button("Calculate");
			exitButton = new Button("Exit the Program");
			clearText = true;
			topPanel = new Panel();
			length = 0.0;
			width = 0.0;
			area = 0.0;
			perimeter = 0.0;


			//set the overall Layout to grid
			setLayout(new BorderLayout());
			topPanel.setLayout(new GridLayout(5,2,5,5));

			//add components to the topPanel for viewing
			topPanel.add(lengthLabel);
				topPanel.add(lengthTextField);
			topPanel.add(widthLabel);
				topPanel.add(widthTextField);
			topPanel.add(areaLabel);
				topPanel.add(areaAnswer);
			topPanel.add(perimeterLabel);
				topPanel.add(perimeterAnswer);
			topPanel.add(calcButton);
				calcButton.addActionListener(this);
			topPanel.add(exitButton);
				exitButton.addActionListener(this);

			//setAction to be performed on calc and exit Button click
			calcButton.setActionCommand("Calculate");
			exitButton.setActionCommand("Exit");

			//add the topPanel itself to make it visible
			add(topPanel, BorderLayout.CENTER);

			//close the application when the X is clicked
			addWindowListener(
				new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
						{
							System.exit(0);
						}//end windowClosing()
				}//end WindowAdapter()
				);

		}//end RectangleApp

		public static void main(String args[])
		{
			//construct an instance of the Frame
			RectangleApp x = new RectangleApp();

			//set additional properties
			x.setTitle("Area and Perimeter of a Rectangle");
			x.setSize(350, 200);
			x.setLocationRelativeTo(null);
			x.setVisible(true);

		}//end main()

		public void actionPerformed(ActionEvent e)
		{
			//test for MenuItem clicks
			//retrieve the associated String
			String arg = e.getActionCommand();
			boolean done = false;

			DecimalFormat answerFormat = new DecimalFormat("###,###,###.##");

			//exit was clicked
			if(arg.equalsIgnoreCase("EXIT"))
			System.exit(0);

			if(arg.equalsIgnoreCase("CLEAR"))
			{
				clearText = true;
				lengthTextField.setText("");
					lengthTextField.requestFocus();
				widthTextField.setText("");
				areaAnswer.setText("");
				perimeterAnswer.setText("");
			}//end if CLEAR


			//perform calculations when the calculate button is clicked
			
			//(---------------------------Problem Area------------------------------)
			if(arg.equalsIgnoreCase("CALCULATE"))
			{
				while(!done)
				{
					if(lengthTextField.getText().length() == 0) length = 0.0;
					else length = Double.parseDouble(lengthTextField.getText());

					if(widthTextField.getText().length() == 0) width = 0.0;
					else width = Double.parseDouble(widthTextField.getText());

					while(length > 0.0 && width > 0.0)
					{
						area = length * width;
						perimeter = 2 * (length + width);
					}//end while

					areaAnswer.setText(area);
					perimeterAnswer.setText(perimeter);
					done = true;
				}//end while
			}//end if CALCULATE
			//(------------------------End Problem Area------------------------------)
		}//end actionPerformed()


}//end RectangleApp class
This is probably a really simple error, but I've been coding now for close to 3 hours and my brain is little more dull than it was. Thanks for any help ahead of time.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2008
Added on Apr 23 2008
7 comments
463 views