Eventos
Ejemplos de Oyentes
FORMAS DE IMPLEMENTAR LOS OYENTES A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getActionCommand.
import java.awt.*; import java.awt.event.*; public class VentanaOyente extends Frame{ private Button boton1; private Button boton2; private int tipoCara; private Oyente oyente; publicVentanaOyente(){ super("Ventana con oyente"); setSize(300,300); setBackground(Color.cyan); addComponentes(); addEventos(); setVisible(true); } public void setTipoCara(int tipoCara){ this.tipoCara = tipoCara; } public int getTipoCara(){ return tipoCara; } public void addComponentes(){ setLayout(new FlowLayout()); boton1 = new Button("Carita Feliz"); boton2 = new Button("Carita Triste"); add(boton1);add(boton2); } public void addEventos(){ oyente = new Oyente(this); boton1.addActionListener(oyente); boton2.addActionListener(oyente); } public void paint(Graphics x){ if(tipoCara!=0){ x.setColor(Color.YELLOW); x.fillOval(40,60,200,200); x.setColor(Color.BLACK); x.fillOval(90,120,20,20); x.fillOval(170,120,20,20);
Rafael Rivera López
1
Tópicos Selectos de Programación
Ejemplos de Oyentes} } }
if(tipoCara==1) x.drawArc(80,100,120,120,180,180); else x.drawArc(80,160,120,120,0,180);
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); elseventana.setTipoCara(2); ventana.repaint();
} }
B).- El oyente en una clase independiente con el constructor con dos argumentos
MiPaint.java y OyenteBoton.java import java.awt.*; public class MiPaint extends Frame{ private int tipoCara; private Button boton1; private Button boton2; public MiPaint(){ super("Uso del método paint"); setSize(300,300); setBackground(Color.cyan); addComponentes(); addEventos();setVisible(true); } public void addComponentes(){ setLayout(new FlowLayout()); boton1 = new Button("Carita Feliz"); boton2 = new Button("Carita Triste"); add(boton1);
Rafael Rivera López
2
Tópicos Selectos de Programación
Ejemplos de Oyentes
add(boton2); } public void setTipoCara(int tipoCara){ this.tipoCara = tipoCara; } public void addEventos(){ OyenteBoton aBoton1 = newOyenteBoton(1,this); OyenteBoton aBoton2 = new OyenteBoton(2,this); boton1.addActionListener(aBoton1); boton2.addActionListener(aBoton2); } public void paint(Graphics x){ if(tipoCara!=0){ x.setColor(Color.YELLOW); x.fillOval(40,60,200,200); x.setColor(Color.BLACK); x.fillOval(90,120,20,20); x.fillOval(170,120,20,20); if(tipoCara==1) x.drawArc(80,100,120,120,180,180); else x.drawArc(80,160,120,120,0,180); }} } import java.awt.event.*; public class OyenteBoton implements ActionListener{ private int valor; private MiPaint f; public OyenteBoton(int valor,MiPaint f){ this.valor = valor; this.f = f; } public void actionPerformed(ActionEvent e){ f.setTipoCara(valor); f.repaint(); } }
Rafael Rivera López
3
Tópicos Selectos de Programación
Ejemplos de Oyentes
C).- La ventana es el oyenteimport java.awt.*; import java.awt.event.*; public class VentanaOyente extends Frame implements ActionListener{ private Button boton1; private Button boton2; private int tipoCara; public VentanaOyente(){ super("Ventana como oyente"); setSize(300,300); setBackground(Color.cyan); addComponentes(); addEventos(); setVisible(true); } public void addComponentes(){ setLayout(new FlowLayout()); boton1 =new Button("Carita Feliz"); boton2 = new Button("Carita Triste"); add(boton1); add(boton2); } public void addEventos(){ boton1.addActionListener(this); boton2.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==boton1) tipoCara=1; else tipoCara=2; repaint(); } public void paint(Graphics x){ if(tipoCara!=0){ x.setColor(Color.YELLOW); x.fillOval(40,60,200,200);...
Regístrate para leer el documento completo.