Java - How to print message when a JFrame is closed?

I'm working through an assignment for a beginner Java class. We're only about halfway through the semester and just learned about JFrames this week. Our assignment had us create a GUI for a program to change colors on screen from a previous week and asks us to print a message to the console saying that the GUI has closed when the user exits the GUI. My current code prints this message as the GUI is opened instead of when it is closed. This creation of a JFrame takes place inside a public class called ColorChanger where javax.swing.JFrame is imported.

ColorChanger Class (at the bottom of main under user prompts for colors):

JFrame application = new JFrame();
application.setLocation(250, 250);
application.setSize(400, 150);
application.setTitle("Color Changer");
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocationRelativeTo(null);
ColorPanel panel = new ColorPanel(colorAmountRed, colorAmountGreen, colorAmountBlue);
application.add(panel);

application.setVisible(true);
System.out.println("The GUI has closed.");

The constructor for panel is located inside a class called ColorPanel:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class ColorPanel extends JPanel {
    private int red;    
    private int green;
    private int blue;

    public ColorPanel(int red, int green, int blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.setBackground(new Color (this.red, this.green, this.blue));    
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);    // Calls parent paintComponent (JPanel itself)

        int fontSize = 26;  // Font Size sent as a parameter
        g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

        int redContrast = 0;
        if (red >= 0 && red <= 123) {
            redContrast = 255;
        }

        int greenContrast = 0;
        if (green >= 0 && green <= 123) {
            greenContrast = 255;
        }

        int blueContrast = 0;
        if (blue >= 0 && blue <= 123) {
            blueContrast = 255;
        }
        this.setForeground(new Color (redContrast, greenContrast, blueContrast));
        g.drawString("Java GUI One", getWidth() / 2 - 75, getHeight() / 2 + 10 );

    }
}   

I'm fairly certain that a println is the way to go, but I'm unsure of where it needs to go in order to appear AFTER the GUI is closed. This is my first time ever working with Java (or StackOverflow, for that matter). Apologies for any formatting issues, etc.


With the line

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

you tell to Java to close itself when the frame closes. To print a messsage after the GUI closes, you should not close Java when your GUI closes:

application.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

and then add an "exit listener":

application.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        System.out.println("GUI has been closed");
        //THEN you can exit the program
        System.exit(0);
    }
});