Ascoltatori per finestre #2

Esistono altri due ascoltatori per lo stato e il fuoco della finestra

  • WindowFocusListener
  • WindowStateListener

Inoltre si può indagare sulle variazioni di posizione, dimensione e visibilità tramite l’ascoltatore generico

  • ComponentListener

Interagisci con l’applicazione e i messaggi corrispondenti appariranno nella console

import java.awt.Frame;
import java.awt.event.*;
 
public class WindowTest extends    Frame
                        implements WindowListener,
                                   WindowStateListener,
                                   WindowFocusListener,
                                   ComponentListener
{
    public WindowTest()
    {
       this.setTitle("Prova i listener di Frame!");
       
       this.addWindowListener(this);      
       this.addWindowFocusListener(this);
       this.addWindowStateListener(this);
       
       this.addComponentListener(this);
       
       this.setSize(400, 400);
       this.setVisible(true); 
    }
 
    public void windowActivated  (WindowEvent we)   { System.out.println("Activated"   ); } 
    public void windowDeactivated(WindowEvent we)   { System.out.println("Deactivated" ); }
    public void windowOpened     (WindowEvent we)   { System.out.println("Opened"      ); }
    public void windowClosing    (WindowEvent we)   { System.out.println("Closing"     ); }
    public void windowClosed     (WindowEvent we)   { System.out.println("Closed"      ); }
    public void windowIconified  (WindowEvent we)   { System.out.println("Iconified"   ); }
    public void windowDeiconified(WindowEvent we)   { System.out.println("Deiconified" ); }   
    
    public void windowGainedFocus(WindowEvent we)   { System.out.println("GainedFocus" ); }
    public void windowLostFocus  (WindowEvent we)   { System.out.println("LostFocus"   ); }   
    
    public void windowStateChanged(WindowEvent we)  { System.out.println("StateChanged"); }    
    
    public void componentMoved  (ComponentEvent ce) { System.out.println("Moved"       ); } 
    public void componentResized(ComponentEvent ce) { System.out.println("Resized"     ); } 
    public void componentHidden (ComponentEvent ce) { System.out.println("Hidden"      ); } 
    public void componentShown  (ComponentEvent ce) { System.out.println("Shown"       ); } 
 
    public static void main(String args[])
    { 
       WindowTest wt=new WindowTest(); 
    } 
}