|
import Hisiuin.*;
public class Adivinacion extends Wwindow{
public Adivinacion(){ } public void init(){
int borde[] = {5,5,5,5}; // Ponemos los widgets.
putLabel(1, 1, "Escribe un numero entre 0 y 10"); put(2, 1, new Wtextfield(1), "visor");
putButton( 1, 2, "Adivina" );
putLabel(2, 2, " ");
set( ALL, ALL, BORDER, borde ); set( 2, 2, LENGTH, 100 );
set( ALL, ALL, LOCATION, WEST ); } public void widgetAction( Wevent e){
// Manejo de eventos if (e.mSource.equals("Adivina")){
Wlabel tmp = (Wlabel)get(2,2); // Generamos numero Aleatorio
int random = (int)Math.round( Math.random() * 10 );
// Obtenemos el campo y lo transformamos a un tipo int.
int comparacion = ( new Integer(((Wtextfield)get(2,1)).getField())).intValue();
//Hacemos comparacion if ( comparacion > 10 || comparacion <= -1 )
tmp.setText(" Error: numero " + comparacion + " no valido " );
else if ( random == comparacion )
tmp.setText("Acertaste !!!" );
else if ( random < comparacion )
tmp.setText(" Te pasaste: Mi numero es --> " + random );
else
tmp.setText(" Te falto: Mi numero es --> " + random );
// Se hace para refrescar el tamaño de la pantalla reorder();
} }
public static void main( String args[]){ Adivinacion ejemplo = new Adivinacion(); }
}
El siguiente es el resultado del programa: |