A good answer might be:

No---an application with many open frames would only close the frame that contained the button (or it might do something completely different; it depends on the application.)

Complete GUI Program

The second step in setting up a program to respond to events is to register the event listener with the component that generates the events (or that contains the component that generates the events). With a Frame component, use addWindowListener() to register a listener. Here is a complete GUI program, including the listener object. The parts in blue are additions to the example program of the previous chapter:

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

class myFrame extends Frame
{
  public void paint ( Graphics g )
  {
    g.drawString("Click the close button", 10, 50 );  
  }
}

class WindowQuitter extends WindowAdapter
{
  public void windowClosing( WindowEvent e )
  {
    System.exit( 0 );  // what to do for this event--exit the program
  }
}

public class GUItester
{
  public static void main ( String[] args )
  {
    myFrame frm = new myFrame();                // construct a myFrame object
    
    WindowQuitter wquit = new WindowQuitter();  // construct a listener for the frame
    frm.addWindowListener( wquit );             // register the listener with the frame
    
    frm.setSize( 150, 100 );     
    frm.setVisible( true );      

  }
}

Notice especially how the program registers the listener object with the event-generating GUI object. You can think of registering a listener object as establishing a channel of communication between the GUI object and the listener.

QUESTION 5:

Does registering a listener establish two-way communications?

Click Here after you have answered the question