Los eventos de windows ocurren cuando un usuario abre o cierra el objeto windows como por ejemplo el JFrame o un JWindow.
Digamos que cualquier componente puede generar estos eventos pero una clase debe tener implementada la interface WindowListener para soportarlo

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

public class EjWin extends JPanel implements WindowListener,
WindowFocusListener,
WindowStateListener {
final static String space = ” “;
final static String newline = “\n”;
static JFrame fr;
JTextArea txtar;

public EjWin() {
super(new BorderLayout());
txtar = new JTextArea();
txtar.setEditable(false);
JScrollPane scrollPane = new JScrollPane(txtar);
scrollPane.setPreferredSize(new Dimension(500, 450));
add(scrollPane, BorderLayout.CENTER);

fr.addWindowListener(this);
fr.addWindowFocusListener(this);
fr.addWindowStateListener(this);

ver();
}

public void ver() {
Toolkit tk = frame.getToolkit();
if (!(tk.isFrameStateSupported(Frame.ICONIFIED))) {
displayMessage(
“Su window no soporta ICONIFIED.”);
}
if (!(tk.isFrameStateSupported(Frame.MAXIMIZED_VERT))) {
displayMessage(
“Su window no soporta MAXIMIZED_VERT.”);
}
if (!(tk.isFrameStateSupported(Frame.MAXIMIZED_HORIZ))) {
displayMessage(
“Su window no soporta MAXIMIZED_HORIZ.”);
}
if (!(tk.isFrameStateSupported(Frame.MAXIMIZED_BOTH))) {
displayMessage(
“Su window no soporta MAXIMIZED_BOTH.”);
} else {
displayMessage(
“Su window soporta MAXIMIZED_BOTH.”);
}
}

public void windowClosing(WindowEvent e) {
displayMessage(“WindowListener method called: windowClosing.”);

ActionListener task = new ActionListener() {
boolean dis = false;
public void actionPerformed(ActionEvent e) {
if (!dis) {
dis = true;
fr.dispose();
} else {
System.exit(0);
}
}
};
Timer tim = new Timer(500, task);
tim.setInitialDelay(2000);
tim.start();
}

public void windowClosed(WindowEvent e) {

displayMessage(“WindowListener method called: windowClosed.”);
}

public void windowOpened(WindowEvent e) {
displayMessage(“WindowListener method called: windowOpened.”);
}

public void windowIconified(WindowEvent e) {
displayMessage(“WindowListener method called: windowIconified.”);
}

public void windowDeiconified(WindowEvent e) {
displayMessage(“WindowListener method called: windowDeiconified.”);
}

public void windowActivated(WindowEvent e) {
displayMessage(“WindowListener method called: windowActivated.”);
}

public void windowDeactivated(WindowEvent e) {
displayMessage(“WindowListener method called: windowDeactivated.”);
}

public void windowGainedFocus(WindowEvent e) {
displayMessage(“WindowFocusListener method called: windowGainedFocus.”);
}

public void windowLostFocus(WindowEvent e) {
displayMessage(“WindowFocusListener method called: windowLostFocus.”);
}

public void windowStateChanged(WindowEvent e) {
displayStateMessage(
“WindowStateListener method called: windowStateChanged.”, e);
}

void displayMessage(String msj) {
txtar.append(msj + newline);
System.out.println(msj);
}

void displayStateMessage(String prefix, WindowEvent e) {
int estado = e.getNewState();
int oldState = e.getOldState();
String msj = prefix
+ newline + space
+ “New v: ”
+ convertStateToString(estado)
+ newline + space
+ “Old estado: ”
+ convertStateToString(oldState);
txtar.append(msj + newline);
System.out.println(msj);
}

String convertStateToString(int estado) {
if (estado == Frame.NORMAL) {
return “NORMAL”;
}
if ((estado & Frame.ICONIFIED) != 0) {
return “ICONIFIED”;
}

if ((estado & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) {
return “MAXIMIZED_BOTH”;
}
if ((estado & Frame.MAXIMIZED_VERT) != 0) {
return “MAXIMIZED_VERT”;
}
if ((estado & Frame.MAXIMIZED_HORIZ) != 0) {
return “MAXIMIZED_HORIZ”;
}
return “UNKNOWN”;
}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

fr = new JFrame(“Ejemplo window event”);
fr.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

JComponent njp = new EjWin();
njp.setOpaque(true); //content panes must be opaque
fr.setContentPane(njp);

fr.pack();
fr.setVisible(true);
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}