nos permite tener un oyente para estar ala espera si se ha precionado un boton, etc para luego ejecutar actionPerformed() que es un metodo de la interfas ActionListener
componentes del actionListener
e.getSource():Nos devuelve el objeto que hace la llamada al oyente
PARA MAS INFORMACION VISITAR LA PAGINA DE LA API DE JAVA
EJEMPLO
package graficos;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author USUARIO
*/
public class primeraClaseEventosMouseClicked {
public static void main(String[] args) {
marcoEvent marco = new marcoEvent();
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
marco.setVisible(true);
}
}
class marcoEvent extends JFrame {
public marcoEvent() {
setTitle("Prueba de colores");
setSize(500, 500);
setLocation(200, 200);
setVisible(true);
laminaEvent lamina = new laminaEvent();
add(lamina);
}
}
class laminaEvent extends JPanel implements ActionListener {
boolean verificar = false;
Button boton = new Button("Nuevo");
Button boton1 = new Button("boton1");
TextField texto1 = new TextField("Texto 1");
public laminaEvent() {
add(boton);
add(boton1);
add(texto1);
boton1.addActionListener(this);
boton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(boton)) {
setBackground(Color.GREEN);
} else if (e.getSource().equals(boton1)) {
setBackground(Color.RED);
} else if (e.getSource().equals(texto1)) {
texto1.setText("hisistes click aqui");
}
}
}






0 comentarios:
Publicar un comentario