
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Vector;
import java.lang.*;

// Hat alle wichtige Listener im JFrame implementiert

// Hat alle wichtige Listener im JFrame implementiert


public class Listener_Bsp03 extends JFrame {

    JPanel pn1 = new JPanel();
    JPanel pn2 = new JPanel();
    JPanel pn3 = new JPanel();
    JPanel [] panels = new JPanel[3];
    JPanel MouseMovePanel=null;


  //Frame konstruieren
  public Listener_Bsp03() {
    this.setSize(new Dimension(500, 300));
    this.setTitle("JFrame mit Key und Mouse-Listener, JFrame_Bsp02");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }


  //Initialisierung der Komponente
  private void setGUI()  {
    int i;
    this.getContentPane().setLayout( new GridBagLayout() );
    pn1.setBackground(Color.red);
    pn2.setBackground(Color.blue);
    pn3.setBackground(Color.yellow);
    panels[0]=pn1;
    panels[1]=pn2;
    panels[2]=pn3;
    getContentPane().add(pn1,
         new GridBagConstraints( 0, 0, 1, 1, 100.0, 33.0
            ,GridBagConstraints.CENTER,
            GridBagConstraints.BOTH,
            new Insets(10,10,10,10), 0, 0));

    getContentPane().add(pn2,
         new GridBagConstraints( 0, 1, 1, 1, 100.0, 100.0
            ,GridBagConstraints.CENTER,
            GridBagConstraints.BOTH,
            new Insets(10,10,10,10), 0, 0));

    getContentPane().add(pn3,
         new GridBagConstraints( 0, 2, 1, 1, 100.0, 33.0
            ,GridBagConstraints.CENTER,
            GridBagConstraints.BOTH,
            new Insets(10,10,10,10), 0, 0));

    this.addWindowListener(new java.awt.event.WindowAdapter() {

      public void windowActivated(WindowEvent e) {
        this_windowActivated(e);
      }

      public void windowClosed(WindowEvent e) {
        this_windowClosed(e);
      }

      public void windowClosing(WindowEvent e) {
        this_windowClosing(e);
      }

      public void windowDeactivated(WindowEvent e) {
        this_windowDeactivated(e);
      }

      public void windowDeiconified(WindowEvent e) {
        this_windowDeiconified(e);
      }

      public void windowIconified(WindowEvent e) {
        this_windowIconified(e);
      }

      public void windowOpened(WindowEvent e) {
        this_windowOpened(e);
      }
    });  // addWindowListener

   // Panel 1 bis 3
    for (i=0; i<panels.length; i++) {    
       panels[i].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
   
         public void mouseDragged(MouseEvent e) {
           // this_mouseDragged(e);
         }
   
         public void mouseMoved(MouseEvent e) {
           pn13_mouseMoved(e);
         }
       });
       panels[i].addMouseListener(new java.awt.event.MouseAdapter() {
   
         public void mouseClicked(MouseEvent e) {
           pn13_mouseClicked(e);
         }
   
         public void mouseEntered(MouseEvent e) {
           pn13_mouseEntered(e);
         }
   
         public void mouseExited(MouseEvent e) {
           pn13_mouseExited(e);
         }
   
         public void mousePressed(MouseEvent e) {
           pn13_mousePressed(e);
         }
   
         public void mouseReleased(MouseEvent e) {
           pn13_mouseReleased(e);
         }
       });  // pn1
   
       panels[i].addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyPressed(KeyEvent e) {
           pn13_keyPressed(e);
         }
   
         public void keyReleased(KeyEvent e) {
           pn13_keyReleased(e);
         }
   
         public void keyTyped(KeyEvent e) {
           pn13_keyTyped(e);
         }
       });
    } // for panels

    // ---------------------------

  }  // setGUI



  void this_windowActivated(WindowEvent e) {
    System.out.println("pn1: windowActivated");
  }

  void this_windowClosed(WindowEvent e) {
    System.out.println("pn1: windowClosed");
  }


  void this_windowClosing(WindowEvent e) {
    System.out.println("windowClosing");
  }

  void this_windowDeactivated(WindowEvent e) {
    System.out.println("windowDeactivated");
  }

  void this_windowDeiconified(WindowEvent e) {
    System.out.println("windowDeiconified");
  }

  void this_windowIconified(WindowEvent e) {
    System.out.println("windowIconified");
  }

  void this_windowOpened(WindowEvent e) {
    System.out.println("windowOpened");
  }


  String getName(Object pn0) {
   JPanel pn = (JPanel) pn0;
   if ( pn.equals(pn1) )
     return "Panel1";
   else if (pn.equals(pn2))
          return "Panel2";
        else if (pn.equals(pn3))
               return "Panel3";
             else
               return "Fehlerhaftes Panels";
  }
  

  void pn13_keyPressed(KeyEvent e) {
    System.out.println("pn13: keyPressed: "+getName( e.getSource()) );
  }

  void pn13_keyReleased(KeyEvent e) {
    System.out.println("pn13: keyReleased: "+getName(e.getSource()) );
  }

  void pn13_keyTyped(KeyEvent e) {
    System.out.println("pn13: keyTyped: "+getName(e.getSource())  );
  }



  void pn13_mouseClicked(MouseEvent e) {
    System.out.println("pn13: mouseClicked: "+getName(e.getSource())  );
  }



  void pn13_mouseEntered(MouseEvent e) {
    System.out.println("pn13: mouseEntered: "+getName(e.getSource())  );
  }

  void pn13_mouseExited(MouseEvent e) {
    System.out.println("pn13: mouseExited: "+getName(e.getSource())  );
  }

   // Nur einmal ausgeben
  void pn13_mouseMoved(MouseEvent e) {
    JPanel pn = (JPanel) e.getSource();
    if ( (MouseMovePanel==null)  || (!pn.equals(MouseMovePanel)) )
      System.out.println("pn13: mouseMoved: "+getName(e.getSource())  );
    MouseMovePanel=pn;
  }

  void pn13_mousePressed(MouseEvent e) {
    System.out.println("pn1: mousePressed: "+getName(e.getSource())  );
  }

  void pn13_mouseReleased(MouseEvent e) {
    System.out.println("pn13: mouseReleased: "+getName(e.getSource()) );
  }



  public static void main(String[] args) {
    Listener_Bsp03 frame = new Listener_Bsp03();
    frame.setVisible(true);
  }

}
