The Frame

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

  1. How many frames are there?
    • Just one.
  2. What GUI components will it contain?
    • The "Red" button and the "Green" button.
  3. What objects generate events?
    • Both buttons.
  4. What objects receive events?
    • A listener for button events.

Actually, there are many events the GUI may generate, but other than button clicks, most of those events are ignored. There are other events, such as clicking on the "close" button on the frame, that are not ignored, but are handled by the frame.

The Frame

You might have said that each button needs its own listener. That would work, but we can use just one listener. First write a class definition for the container frame. Here is a skeleton:

import java.awt.*; 
import java.awt..*;
import javax.swing.*; 

public class TwoButtons extends   implements  
{


  . . . . more code will go here . . . . 

  public static void main ( String[] args )
  {
    TwoButtons demo  = new new TwoButtons( "Click a Button" ) ;
    
    demo.( 200, 150 );     
    demo.setVisible( true );      

  }
}

Notice that this program includes main() inside the TwoButtons class.

For now, just decide what class this new one extends and what interface it will implement. Then fill in the constructor in main()

QUESTION 4:

Fill in the blanks.