Interfaccia AWT ContainerListener

introduzione

L'interfacciaContainerListenerviene utilizzato per ricevere eventi contenitore. La classe che elabora gli eventi del contenitore deve implementare questa interfaccia.

Dichiarazione di classe

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

public interface ContainerListener
extends EventListener

Metodi di interfaccia

SN Metodo e descrizione
1

void componentAdded(ContainerEvent e)

Richiamato quando un componente è stato aggiunto al contenitore.

2

void componentRemoved(ContainerEvent e)

Richiamato quando un componente è stato rimosso dal contenitore.

Metodi ereditati

Questa classe eredita i metodi dalle seguenti interfacce:

  • java.awt.event.EventListener

Esempio di ContainerListener

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.showContainerListenerDemo();
   }

   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 showContainerListenerDemo(){
      headerLabel.setText("Listener in action: ContainerListener");      

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

      Label msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
      panel.add(msglabel);
   
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }

   class CustomContainerListener implements ContainerListener {
      public void componentAdded(ContainerEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " added. ");
      }

      public void componentRemoved(ContainerEvent e) {
         statusLabel.setText(statusLabel.getText() 
         + e.getComponent().getClass().getSimpleName() + " removed. ");
      }
   }
}

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