Porque no funciona ?

Diogenes
24 de Octubre del 2002

He intentando por todos los medios de capturar una dato a partir
de un TexField, pero no he tenido exito. Es mas me da el siguiente
error cuando compilo

Method getText() not found in class java.lang.String
lolo = tfname.getText();

Esta ultima linea es la asignacion a una variable del valor del campo

a continuacion entrego parte del codigo del programa

porque no funciona ?




import java.awt.*;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.FontMetrics;

public class SoLong extends java.applet.Applet {


String lolo,tfname;

Frame window;

void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;

}
public void init() {

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);


//barra con titulo
setFont(tit);
buildConstraints(constraints, 0, 0, 1, 1, 20, 35);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
Label label4 = new Label("ESCUELA SANTA MARIA DE IQUIQUE", Label.LEFT);
gridbag.setConstraints(label4, constraints);
add(label4);


//barra con nada
buildConstraints(constraints, 1, 0, 1, 1, 60, 0);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
Label label3 = new Label("", Label.LEFT);
gridbag.setConstraints(label3, constraints);
add(label3);


//barra con nada
buildConstraints(constraints, 2, 0, 1, 1, 20, 0);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
Label label5 = new Label("", Label.LEFT);
gridbag.setConstraints(label5, constraints);
add(label5);

//etiqueta de nombre
buildConstraints(constraints, 0, 1, 1, 1, 0, 25);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
Label label1 = new Label("Nombre :", Label.LEFT);
gridbag.setConstraints(label1, constraints);
add(label1);


//campo de texto de nombre
buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
TextField tfname = new TextField(20);
gridbag.setConstraints(tfname, constraints);
add(tfname);

//etiqueta de contrase¤a
buildConstraints(constraints, 0, 2, 1, 1, 0, 25);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
Label label2 = new Label("Password :", Label.LEFT);
gridbag.setConstraints(label2, constraints);
add(label2);

//campo de texto de contrase¤a
buildConstraints(constraints, 1, 2, 1, 1, 0, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
TextField tfpass = new TextField();
tfpass.setEchoChar('*');
gridbag.setConstraints(tfpass, constraints);
add(tfpass);

//boton OK
buildConstraints(constraints, 0, 3, 3, 1, 0, 15);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
Buttonokb = new Button(" Aceptar ");
gridbag.setConstraints(Buttonokb, constraints);
add(Buttonokb);


window = new BaseFrame1("A Pop Up Window");
window.resize(150,150);

}

public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
String label = (String)arg;
lolo = tfname.getText();
if (lolo == "lolo") setBackground(Color.blue);
else setBackground(Color.red);
repaint();
if (label.equals(" Aceptar "))
if (!window.isShowing()) {
window.show();
} else {
if (window.isShowing())
window.hide();
}
return true;
} else { setBackground(Color.black);
repaint();
return false;

}

}

}





delia
24 de Octubre del 2002
Hola,
Creo que tu problema está en la declaración.
El método getText() existe en el objeto TextField y no en String.
En tu clase, estás declarando como variable de clase:
String tfname;
y después de manera local:
TextField tfname = new TextField(20);
Deberías declarar como variable de clase:
TextField tfname;

Saludos.