Two Buttons in a 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:

Sure. A frame can hold many GUI components.

Two Buttons in a Frame

two buttons in a green frame

Let's develop a new program:

  • A frame will hold two buttons, one labeled "Red" and the other "Green."
  • The FlowLayout layout manager will be used so the buttons will be nicely positioned in the frame.
  • When the button marked "Red" is clicked, the background will turn red.
  • When the button marked "Green" is clicked, the background will turn green.
  • There will be one event listener that listens to the clicks from both buttons.
  • When the "close button" of the frame is clicked, the application quits.

This is very close to a previous example program. It would be sensible to copy that file and edit it for the new application. However, as a review of these somewhat confusing GUI implementation details, let us start from scratch.

QUESTION 3:

Read the description again and decide on the following:

  1. How many frames are there?
  2. What GUI components will it contain?
  3. What objects generate events?
  4. What objects receive events?