Java - verificar datos de TextField

 
Vista:

verificar datos de TextField

Publicado por Rexixtente (9 intervenciones) el 03/04/2007 01:28:59
buenas.... estoy necesitando verificar los datos de un textfield..
exactamente verificar si se ingresaron solo numero para poder realizar la operacion
si alguien supiera le agradeceria mucho que me ayudara:

aca les dejo el code:

import java.awt.*;
class nota extends Frame
{
Panel pnl1,pnl2,pnlp;
Label lbn1,lbn2,lbn3,lbnd;
TextField tfn1,tfn2,tfn3,tfnd;
Button btc,btl,bts;

nota()
{
lbn1=new Label("NOTA 1: ",Label.LEFT);
lbn2=new Label("NOTA 2: ",Label.LEFT);
lbn3=new Label("NOTA 3: ",Label.LEFT);
lbnd=new Label("NOTA DEFINITIVA: ",Label.CENTER);

btc=new Button("CALCULAR");
btl=new Button("LIMPIAR");
bts=new Button("SALIR");

tfn1=new TextField("",3);
tfn2=new TextField("",3);
tfn3=new TextField("",3);
tfnd=new TextField("",10);

pnl1=new Panel();
pnl2=new Panel();
pnlp=new Panel();

pnl1.setLayout(new GridLayout(4,2,5,3));
pnl2.setLayout(new GridLayout(1,3,2,3));
pnlp.setLayout(new GridLayout(2,1,5,3));

pnl1.add(lbn1);
pnl1.add(tfn1);
pnl1.add(lbn2);
pnl1.add(tfn2);
pnl1.add(lbn3);
pnl1.add(tfn3);
pnl1.add(lbnd);
pnl1.add(tfnd);

pnl2.add(btl);
pnl2.add(btc);
pnl2.add(bts);

pnlp.add(pnl1);
pnlp.add(pnl2);

diseño();
add(pnlp);
move(200,150);
pack();
show();
}
public static void main(String a[])
{
nota objn=new nota();
}

public boolean action(Event evt,Object obj)
{
if(evt.target==btc)
calcular();
if(evt.target==btl)
limpiar();
if(evt.target==bts)
{
dispose();
System.exit(0);
}
return true;
}

public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)
{
dispose();
System.exit(0);
}
return super.handleEvent(evt);
}

void calcular()
{
double n1,n2,n3,nd;
Double x;
x=new Double(tfn1.getText());
n1=x.doubleValue();
x=new Double(tfn2.getText());
n2=x.doubleValue();
x=new Double(tfn3.getText());
n3=x.doubleValue();
nd=(n1+n2+n3)/3;
tfnd.setText("" + nd);
}

void limpiar()
{
tfn1.setText("");
tfn2.setText("");
tfn3.setText("");
tfnd.setText("");
tfn1.requestFocus();
}

private void diseño()
{
lbn1.setBackground(Color.black);
lbn1.setForeground(Color.yellow);
lbn1.setFont(new Font("tahoma",Font.BOLD,13));
lbn2.setBackground(Color.black);
lbn2.setForeground(Color.yellow);
lbn2.setFont(new Font("tahoma",Font.BOLD,13));
lbn3.setBackground(Color.black);
lbn3.setForeground(Color.yellow);
lbn3.setFont(new Font("tahoma",Font.BOLD,13));
lbnd.setBackground(Color.black);
lbnd.setForeground(Color.yellow);
lbnd.setFont(new Font("tahoma",Font.BOLD,13));
btc.setForeground(Color.blue);
btc.setFont(new Font("tahoma",Font.BOLD,12));
btl.setForeground(Color.blue);
btl.setFont(new Font("tahoma",Font.BOLD,12));
bts.setForeground(Color.blue);
bts.setFont(new Font("tahoma",Font.BOLD,12));

//btcalcular.setFont(new Font("Impact",Font.BOLD,15));
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:verificar datos de TextField

Publicado por rossana (4 intervenciones) el 03/04/2007 01:56:51
Lo haces usando un try y un catch que va dentro de tu metodo calcular, de esta manera:


try{x= Double.parseDouble(tfn1.getText());}
catch(Exception ex)
{
//aqui colocas lo que va a hacer el programa si no es un
//double, esto podria ser
tfn1.setText("");
}
Espero que te sirva de algo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:verificar datos de TextField

Publicado por guimox (20 intervenciones) el 03/04/2007 02:01:39
import javax.swing.*;
import java.awt.*;
class nota extends Frame
{
Panel pnl1,pnl2,pnlp;
Label lbn1,lbn2,lbn3,lbnd;
TextField tfn1,tfn2,tfn3,tfnd;
Button btc,btl,bts;

nota()
{
lbn1=new Label("NOTA 1: ",Label.LEFT);
lbn2=new Label("NOTA 2: ",Label.LEFT);
lbn3=new Label("NOTA 3: ",Label.LEFT);
lbnd=new Label("NOTA DEFINITIVA: ",Label.CENTER);

btc=new Button("CALCULAR");
btl=new Button("LIMPIAR");
bts=new Button("SALIR");

tfn1=new TextField("",3);
tfn2=new TextField("",3);
tfn3=new TextField("",3);
tfnd=new TextField("",10);

pnl1=new Panel();
pnl2=new Panel();
pnlp=new Panel();

pnl1.setLayout(new GridLayout(4,2,5,3));
pnl2.setLayout(new GridLayout(1,3,2,3));
pnlp.setLayout(new GridLayout(2,1,5,3));

pnl1.add(lbn1);
pnl1.add(tfn1);
pnl1.add(lbn2);
pnl1.add(tfn2);
pnl1.add(lbn3);
pnl1.add(tfn3);
pnl1.add(lbnd);
pnl1.add(tfnd);

pnl2.add(btl);
pnl2.add(btc);
pnl2.add(bts);

pnlp.add(pnl1);
pnlp.add(pnl2);

diseño();
add(pnlp);
move(200,150);
pack();
show();
}
public static void main(String a[])
{
nota objn=new nota();
}

public boolean action(Event evt,Object obj)
{
if(evt.target==btc)
calcular();
if(evt.target==btl)
limpiar();
if(evt.target==bts)
{
dispose();
System.exit(0);
}
return true;
}

public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)
{
dispose();
System.exit(0);
}
return super.handleEvent(evt);
}

void calcular()
{
try{
double n1,n2,n3,nd;
Double x;
x=new Double(tfn1.getText());
n1=x.doubleValue();
x=new Double(tfn2.getText());
n2=x.doubleValue();
x=new Double(tfn3.getText());
n3=x.doubleValue();
nd=(n1+n2+n3)/3;
tfnd.setText("" + nd);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"Asegurese de ingresar solo numeros");
}
}

void limpiar()
{
tfn1.setText("");
tfn2.setText("");
tfn3.setText("");
tfnd.setText("");
tfn1.requestFocus();
}

private void diseño()
{
lbn1.setBackground(Color.black);
lbn1.setForeground(Color.yellow);
lbn1.setFont(new Font("tahoma",Font.BOLD,13));
lbn2.setBackground(Color.black);
lbn2.setForeground(Color.yellow);
lbn2.setFont(new Font("tahoma",Font.BOLD,13));
lbn3.setBackground(Color.black);
lbn3.setForeground(Color.yellow);
lbn3.setFont(new Font("tahoma",Font.BOLD,13));
lbnd.setBackground(Color.black);
lbnd.setForeground(Color.yellow);
lbnd.setFont(new Font("tahoma",Font.BOLD,13));
btc.setForeground(Color.blue);
btc.setFont(new Font("tahoma",Font.BOLD,12));
btl.setForeground(Color.blue);
btl.setFont(new Font("tahoma",Font.BOLD,12));
bts.setForeground(Color.blue);
bts.setFont(new Font("tahoma",Font.BOLD,12));

//btcalcular.setFont(new Font("Impact",Font.BOLD,15));
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:verificar datos de TextField

Publicado por Rexixtente (9 intervenciones) el 04/04/2007 18:09:53
holas creo que tengo un error para compilar ese code

el error es este:
Package javax.swing not found in import

que por lo que entiendo es que no encuentra esa libreria.....

se me olvidaba comentar que utilizo el JDK 1.1.6
no se si ese compilador contiene esa libreria..
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar