FORMAS DE IMPLEMENTAR LOS OYENTES A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand. public class VentanaOyente extends Frame{ private Oyente oyente; public VentanaOyente(){ super("ventana con oyente"); public void settipocara(int tipocara){ this.tipocara = tipocara; public int gettipocara(){ return tipocara; oyente = new Oyente(this); boton1.addactionlistener(oyente); boton2.addactionlistener(oyente); Rafael Rivera López 1
class Oyente implements ActionListener{ private VentanaOyente ventana; public Oyente(VentanaOyente origen){ this.ventana = origen; public void actionperformed(actionevent e) { String origen = e.getactioncommand(); if(origen.equals("carita Feliz")) ventana.settipocara(1); ventana.settipocara(2); ventana. B).- El oyente en una clase independiente con el constructor con dos argumentos MiPaint.java y OyenteBoton.java public class MiPaint extends Frame{ public MiPaint(){ super("uso del método paint"); Rafael Rivera López 2
public void settipocara(int tipocara){ this.tipocara = tipocara; OyenteBoton aboton1 = new OyenteBoton(1,this); OyenteBoton aboton2 = new OyenteBoton(2,this); boton1.addactionlistener(aboton1); boton2.addactionlistener(aboton2); public class OyenteBoton implements ActionListener{ private int valor; private MiPaint f; public OyenteBoton(int valor,mipaint f){ this.valor = valor; this.f = f; f.settipocara(valor); f. Rafael Rivera López 3
C).- La ventana es el oyente public class VentanaOyente extends Frame implements ActionListener{ public VentanaOyente(){ super("ventana como oyente"); boton1.addactionlistener(this); boton2.addactionlistener(this); if(e.getsource()==boton1) tipocara=1; tipocara=2; Rafael Rivera López 4
D).- El oyente es una clase interna public class VentanaOyenteInterno extends Frame{ public VentanaOyenteInterno(){ super("ventana con Oyente Interno"); OyenteBoton aboton1 = new OyenteBoton(1); OyenteBoton aboton2 = new OyenteBoton(2); boton1.addactionlistener(aboton1); boton2.addactionlistener(aboton2); Rafael Rivera López 5
class OyenteBoton implements ActionListener{ private int valor; public OyenteBoton(int valor){ this.valor = valor; tipocara = valor; E).- La ventana es el oyente de varios eventos public class VentanaOyente extends Frame implements ActionListener, WindowListener{ public VentanaOyente(){ super("ventana como oyente"); boton1.addactionlistener(this); boton2.addactionlistener(this); addwindowlistener(this); Rafael Rivera López 6
if(e.getsource()==boton1) tipocara=1; tipocara=2; public void windowopened(windowevent e) { public void windowclosing(windowevent e) { System.exit(0); public void windowclosed(windowevent e) { public void windowiconified(windowevent e) { public void windowdeiconified(windowevent e) { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { F).- El oyente es una clase interna anónima public class VentanaOyente extends Frame{ public VentanaOyente(){ super("ventana como oyente"); Rafael Rivera López 7
boton1.addactionlistener(new ActionListener(){ tipocara=1; ); boton2.addactionlistener(new ActionListener(){ tipocara=2; ); addwindowlistener(new WindowListener(){ public void windowopened(windowevent e) { public void windowclosing(windowevent e) { System.exit(0); public void windowclosed(windowevent e) { public void windowiconified(windowevent e) { public void windowdeiconified(windowevent e) { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { ); Rafael Rivera López 8
G).- El oyente a través de clases Adaptadoras public class VentanaOyente extends Frame{ public VentanaOyente(){ super("ventana como oyente"); boton1.addactionlistener(new ActionListener(){ tipocara=1; ); boton2.addactionlistener(new ActionListener(){ tipocara=2; Rafael Rivera López 9
); addwindowlistener(new WindowAdapter(){ public void windowclosing(windowevent e) { System.exit(0); ); Rafael Rivera López 10