martes, 21 de febrero de 2012

frame con 3 eventos: windowlistener,mousemotionlistener,mouselistener.

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
public class Ejerciciofocus {
Frame f=new Frame();
Button contar;
private int cont=0;
 public Ejerciciofocus()
 {
  f.setTitle("hola Mund90");
  f.setSize(300,200);
  f.setBackground(Color.white);
  f.setLayout(new FlowLayout());
  f.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
  f.addWindowFocusListener(new WindowFocusListener(){
   @Override
   public void windowGainedFocus(WindowEvent arg0) {
    f.setBackground(Color.black);
   
   }
   @Override
   public void windowLostFocus(WindowEvent arg0) {
    f.setBackground(Color.red);
   
   }
  
  
  });
 
  contar=new Button("contar");
  contar.setSize(50,25);
  contar.addMouseListener(new MouseListener(){
   @Override
   public void mouseClicked(MouseEvent e) {
   
    System.out.println("Ha pulsado el boton "+cont+" veces");
    cont++;
   
   }
   @Override
   public void mouseEntered(MouseEvent e) {
   // cont++;
   // System.out.println("HA entrado al boton "+cont+"veces");
   
   }
   @Override
   public void mouseExited(MouseEvent e) {
   // cont++;
    //System.out.println("Ha salido del boton "+cont+"veces");
   
   }
   @Override
   public void mousePressed(MouseEvent e) {
    cont++;
    System.out.println("Ha presionado en el boton "+cont+"veces");
   
   }
   @Override
   public void mouseReleased(MouseEvent e) {
    cont++;
    //System.out.println("Ha releaseado "+cont+" veces");
   
   }
  
  });
  contar.addMouseMotionListener(new MouseMotionListener(){
   @Override
   public void mouseDragged(MouseEvent arg0) {
   
   
   }
   @Override
   public void mouseMoved(MouseEvent arg0) {
    System.out.println("se esta moviendo el mouse");
    cont++;
    System.out.println("Se ha movido "+cont+" veces dentro del boton ");
   }
  });
  f.add(contar);
  f.setVisible(true);
 }
public static void main(String[] args) {
 Ejerciciofocus ef=new Ejerciciofocus();

}
}

No hay comentarios:

Publicar un comentario