Program with two Frames

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.

Program with two Frames

This program creates (and displays) two frames. One frame is the "master" frame. Click its close button, and everything closes down. The other frame is temporary. Click its close button, and only it is closed.

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

public class TwoTestFrames
{
  public static void main ( String[] args )
  {
    int height=100, width=200;
    JFrame master = new JFrame("Click to Close Everything");
    JFrame temp = new JFrame("Click to Close Just This");

    master.setVisible( true );
    master.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    master.setSize( 400, 300 );
    
    temp.setVisible( true );
    temp.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE  );
    temp.setSize( 300, 200 );

  }
}

Of course, in a sensible program the frames would be doing something more than just displaying themselves. We will get to this in a few chapters.

QUESTION 10:

Is it possible to define a class that uses JFrame as a base (a parent) class?