...

View Full Version : Problem with setText() method



ZivonHunter
01-08-2008, 08:26 AM
Below is a basic Calculator program I created in high school. I recently modified it to include buttons for sine, cosine, and tangent, but when I go to compile the program with the new code, I get the following errors:

Calculator.java:213: cannot resolve symbol
symbol : method setText (double)
location: class java.awt.TextField
lcd.setText(sine);
^
Calculator.java:217: cannot resolve symbol
symbol : method setText (double)
location: class java.awt.TextField
lcd.setText(cosine);
^
Calculator.java:221: cannot resolve symbol
symbol : method setText (double)
location: class java.awt.TextField
lcd.setText(tangent);



//imports all packages needed for this program to run correctly
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import java.text.*;

public class Calculator extends Frame implements ActionListener, KeyListener
{
//declare objects
private Button keys[] = new Button[24]; //Declares an array of buttons numbered 0-23
//Creates a panel object and sets its layout
private Panel keypad = new Panel(new GridLayout(6,4,10,10)); //Used to hold the button array
private TextField lcd = new TextField("",20);
private double opl = 0.0, memory = 0.0;
//flag variables
private boolean first = true, clearText = true, foundKey;
private int lastOp = 0;
private DecimalFormat calcPattern = new DecimalFormat("########.########");

public Calculator() //Constructor method
{
//Creates a menu bar with three menus
MenuBar mnuBar = new MenuBar();
this.setMenuBar(mnuBar);

Menu mnuFile = new Menu("File", true);
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
mnuFile.add(mnuFileExit);

Menu mnuEdit = new Menu("Edit", true);
mnuBar.add(mnuEdit);
MenuItem mnuEditClear = new MenuItem("Clear");
mnuEdit.add(mnuEditClear);
mnuEdit.insertSeparator(1);
MenuItem mnuEditCopy = new MenuItem("Copy");
mnuEdit.add(mnuEditCopy);
MenuItem mnuEditCut = new MenuItem("Cut");
mnuEdit.add(mnuEditCut);
MenuItem mnuEditPaste = new MenuItem("Paste");
mnuEdit.add(mnuEditPaste);

Menu mnuAbout = new Menu("About", true);
mnuBar.add(mnuAbout);
MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
mnuAbout.add(mnuAboutCalculator);

//adds ActionListeners to each menu command
mnuFileExit.addActionListener(this);
mnuEditClear.addActionListener(this);
mnuEditCopy.addActionListener(this);
mnuEditCut.addActionListener(this);
mnuEditPaste.addActionListener(this);
mnuAboutCalculator.addActionListener(this);

//sets ActionCommands for each menu command
mnuFileExit.setActionCommand("Exit");
mnuEditClear.setActionCommand("Clear");
mnuEditCopy.setActionCommand("Copy");
mnuEditCut.setActionCommand("Cut");
mnuEditPaste.setActionCommand("Paste");
mnuAboutCalculator.setActionCommand("About");

lcd.setEditable(false);

//sets caption for Buttons 0-9
for(int i=0; i<=9; i++) keys[i] = new Button(String.valueOf(i));

//sets captions for operators
keys[10] = new Button("/");
keys[11] = new Button("*");
keys[12] = new Button("-");
keys[13] = new Button("+");
keys[14] = new Button("=");
keys[15] = new Button(".");
//sets captions for other command buttons
keys[16] = new Button("M+");
keys[17] = new Button("M");
keys[18] = new Button("Bksp");
keys[19] = new Button("CLR");
keys[20] = new Button("M-");
keys[21] = new Button("sin");
keys[22] = new Button("cos");
keys[23] = new Button("tan");

this.setBackground(Color.white);
this.setLayout(new BorderLayout());

//Button components are added in appropriate order
for(int i=7; i<=10; i++) keypad.add(keys[i]);

for(int i=4; i<=6; i++) keypad.add(keys[i]);

keypad.add(keys[11]);

for(int i=1; i<=3; i++) keypad.add(keys[i]);

keypad.add(keys[12]);
keypad.add(keys[0]);

for(int i=15; i>=13; i--) keypad.add(keys[i]);

for(int i=16; i<=19; i++) keypad.add(keys[i]);

for(int i=20; i<=23; i++)keypad.add(keys[i]);

for(int i=0; i<keys.length; i++) keys[i].addActionListener(this);

this.add(lcd, BorderLayout.NORTH);
this.add(keypad, BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we)
{System.exit(0);}});
}//end of constructor method

public void actionPerformed(ActionEvent ae)
{
String arg = ae.getActionCommand();

if (arg == "Exit") System.exit(0);

if (arg == "Clear") Clear();

if (arg == "Copy") Copy();

if (arg == "Cut") Cut();

if (arg == "Paste") Paste();

if (arg == "About") About();

foundKey = false;

for (int i=0; i<keys.length && !foundKey; i++)
{
if (ae.getSource() == keys[i])
{
foundKey = true;
switch(i)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
case 9: case 15:
if (clearText)
{
lcd.setText("");
clearText = false;
}
lcd.setText(lcd.getText() + keys[i].getLabel());
break;
case 10: case 11: case 12: case 13: case 14:
clearText = true;
if (first)
{
if (lcd.getText().length() == 0) opl = 0.0;
else opl = Double.parseDouble(lcd.getText());

first = false;
clearText = true;
lastOp = i;
}
else //second operand
{
switch(lastOp)
{
case 10: //divide button
opl /= Double.parseDouble(lcd.getText());
break;
case 11: //multiply button
opl *= Double.parseDouble(lcd.getText());
break;
case 12: //minus button
opl -= Double.parseDouble(lcd.getText());
break;
case 13: //add button
opl += Double.parseDouble(lcd.getText());
break;
}
lcd.setText(calcPattern.format(opl));
clearText = true;
if (i == 14) first = true;
else lastOp = i;
}
break;
case 16: //M+
memory = Double.parseDouble(lcd.getText());
keys[16].setForeground(Color.red);
break;
case 17: //M
lcd.setText(lcd.getText() + memory);
break;
case 18: //Backspace
lcd.setText(lcd.getText().substring(0, lcd.getText().length() - 1));
break;
case 19: //Clear
Clear();
break;
case 20: //M-
memory = 0.0;
keys[16].setForeground(Color.black);
break;
case 21: //sin
double sine = Math.sin(Double.parseDouble(lcd.getText()));
lcd.setText(sine);
break;
case 22: //cos
double cosine = Math.cos(Double.parseDouble(lcd.getText()));
lcd.setText(cosine);
break;
case 23: //tan
double tangent = Math.tan(Double.parseDouble(lcd.getText()));
lcd.setText(tangent);
break;
}
}//end of if
}//end of for loop
}//end of actionPerformed()


public static void main(String[] args)
{
Calculator frame = new Calculator();
frame.setTitle("Calculator Application, Duncan Conley");
frame.setBounds(200,200,300,300);
frame.setVisible(true);

Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gif");
frame.setIconImage(icon);
}

public void Copy() //copies lcd's contents to the system clipboard
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection contents = new StringSelection(lcd.getText());
cb.setContents(contents, null);
}

public void Cut() //moves lcd's contents to the system clipboard
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection contents = new StringSelection(lcd.getText());
cb.setContents(contents, null);
lcd.setText("");
}

public void Paste() //pastes a number from the clipboard to lcd
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = cb.getContents(this);
try
{
String s = (String)content.getTransferData(DataFlavor.stringFlavor);
lcd.setText(calcPattern.format(Double.parseDouble(s)));
}
catch(Throwable exc)
{
lcd.setText("");
}
}

public void About() //displays information on this program
{
JOptionPane.showMessageDialog(this,
"Calculator v. 1.0\nOpenExhibit Software\nCopyright 2004\nAll rights reserved",
"About Calculator",JOptionPane.INFORMATION_MESSAGE);
}

protected void Clear() //resets program
{
clearText = true;
first = true;
opl = 0.0;
keys[16].setForeground(Color.black);
lcd.setText("");
lcd.requestFocus();
}
}

shyam
01-08-2008, 12:18 PM
java.awt.TextField setText (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/TextField.html#setText(java.lang.String))method is defined as


void setText(String t)

not as

void setText(double t)

so u gotta use


lcd.setText(Double.toString(sine));



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum