Interfaccia ComponentListener AWT

La classe che elabora ComponentEvent dovrebbe implementare questa interfaccia. L'oggetto di quella classe deve essere registrato con un componente. L'oggetto può essere registrato utilizzando il metodo addComponentListener (). Gli eventi dei componenti vengono generati solo a scopo informativo.

Dichiarazione dell'interfaccia

Di seguito è riportata la dichiarazione per java.awt.event.ComponentListener interfaccia:

public interface ComponentListener
   extends EventListener

Metodi di interfaccia

SN Metodo e descrizione
1

void componentHidden(ComponentEvent e)

Richiamato quando il componente è stato reso invisibile.

2

void componentMoved(ComponentEvent e)

Richiamato quando la posizione del componente cambia.

3

void componentResized(ComponentEvent e)

Richiamato quando le dimensioni del componente cambiano.

4

void componentShown(ComponentEvent e)

Richiamato quando il componente è stato reso visibile.

Metodi ereditati

Questa interfaccia eredita i metodi dalle seguenti interfacce:

  • java.awt.EventListener

Esempio di ComponentListener

Crea il seguente programma java usando qualsiasi editor di tua scelta, diciamo D:/ > AWT > com > tutorialspoint > gui >

AwtListenerDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtListenerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtListenerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtListenerDemo  awtListenerDemo = new AwtListenerDemo();  
      awtListenerDemo.showComponentListenerDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
   
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showComponentListenerDemo(){
      headerLabel.setText("Listener in action: ComponentListener");      

      ScrollPane panel = new ScrollPane();      
      panel.setBackground(Color.magenta);            

      Label msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
      panel.add(msglabel);

      msglabel.addComponentListener(new CustomComponentListener());      
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }

   class CustomComponentListener implements ComponentListener {

      public void componentResized(ComponentEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " resized. ");
      }

      public void componentMoved(ComponentEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " moved. ");
      }

      public void componentShown(ComponentEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " shown. ");
      }

      public void componentHidden(ComponentEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " hidden. ");
      }
   }
}

Compilare il programma utilizzando il prompt dei comandi. Vai aD:/ > AWT e digita il seguente comando.

D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

Se non viene visualizzato alcun errore, significa che la compilazione è riuscita. Esegui il programma utilizzando il seguente comando.

D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verificare il seguente output