Está en la página 1de 2

FRAMES

PRESIONAR BOTONES

Para realizar acciones en los botones se utiliza la función

La función recibe 2 parámetros: un


evento e y un objeto o

public boolean action(Event e,Object o)


{ e.target : tiene el nombre del botón que se presiono
if(e.target==bsuma)

{ Nombre del botón que posiblemente se presiono

// ACCIONES DEL BOTON

}
return true; Como es una función Booleana debe devolver true
}

A continuación mostramos un ejemplo:

import java.awt.*;
class sumaframe extends Frame
{
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Label l=new Label();
Button bsuma=new Button("sumar");
Button bresta=new Button("restar");
Button b=new Button("salir");
public static void main(String args[])
{
new sumaframe();
}

public sumaframe()
{
t1.setSize(90,20);
t1.setLocation(50,50);
add(t1);

t2.setSize(90,20);
t2.setLocation(50,100);
add(t2);
t3.setSize(90,20);
t3.setLocation(250,80);
add(t3);

bsuma.setSize(90,20);
bsuma.setLocation(150,50);
add(bsuma);

bresta.setSize(90,20);
bresta.setLocation(150,100);
add(bresta);

b.setSize(90,20);
b.setLocation(150,300);
add(b);

add(l);

setSize(400,400);
show();
}
public boolean action(Event e,Object o)
{
if(e.target==bsuma)
{
int a=Integer.parseInt(t1.getText()); Acciones que se realizan si se presiona
int b=Integer.parseInt(t2.getText());
el botón bsuma
int c=a+b;|
t3.setText(c+"");
}
if(e.target==bresta)
{
int a=Integer.parseInt(t1.getText()); Acciones que se realizan si se presiona
int b=Integer.parseInt(t2.getText()); el botón bresta
int c=a-b;
t3.setText(c+"");
}
return true;
}
}

También podría gustarte