Related Topics

JAVA Programing
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends Frame implements WindowStateListener {
public MyWindow() {
// Set up the window properties and add the window state listener
setTitle("My Window");
setSize(300, 200);
addWindowStateListener(this);
}
public void windowStateChanged(WindowEvent e) {
// Handle the window state change
int newState = e.getNewState();
if ((newState & Frame.ICONIFIED) != 0) {
// Window has been minimized
System.out.println("Window has been minimized");
} else if ((newState & Frame.MAXIMIZED_BOTH) != 0) {
// Window has been maximized
System.out.println("Window has been maximized");
} else {
// Window has been restored
System.out.println("Window has been restored");
}
}
public static void main(String[] args) {
// Create and show the window
MyWindow window = new MyWindow();
window.setVisible(true);
}
}
In this example, the MyWindow
class extends the Frame
class and implements the WindowStateListener
interface. The windowStateChanged()
method is overridden to handle the window state change event, and the addWindowStateListener()
method is called to register the MyWindow
instance as a listener for window state events.
When the window state changes, the windowStateChanged()
method is called with a WindowEvent
parameter. The method uses the getNewState()
method of the WindowEvent
to determine the new state of the window and then takes appropriate action based on the state.
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends Frame implements WindowFocusListener {
public MyWindow() {
// Set up the window properties and add the window focus listener
setTitle("My Window");
setSize(300, 200);
addWindowFocusListener(this);
}
public void windowGainedFocus(WindowEvent e) {
// Handle the window gaining focus
System.out.println("Window has gained focus");
}
public void windowLostFocus(WindowEvent e) {
// Handle the window losing focus
System.out.println("Window has lost focus");
}
public static void main(String[] args) {
// Create and show the window
MyWindow window = new MyWindow();
window.setVisible(true);
}
}
In this example, the MyWindow
class extends the Frame
class and implements the WindowFocusListener
interface. The windowGainedFocus()
and windowLostFocus()
methods are overridden to handle the corresponding events, and the addWindowFocusListener()
method is called to register the MyWindow
instance as a listener for window focus events.
When the window gains or loses focus, the corresponding method is called with a WindowEvent
parameter. The method then takes appropriate action based on the event.
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends Frame implements WindowListener {
public MyWindow() {
// Set up the window properties and add the window listener
setTitle("My Window");
setSize(300, 200);
addWindowListener(this);
}
public void windowOpened(WindowEvent e) {
// Handle the window opening event
System.out.println("Window has been opened");
}
public void windowClosing(WindowEvent e) {
// Handle the window closing event
System.out.println("Window is closing");
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {
// Handle the window closed event
System.out.println("Window has been closed");
}
public void windowIconified(WindowEvent e) {
// Handle the window iconified event
System.out.println("Window has been minimized");
}
public void windowDeiconified(WindowEvent e) {
// Handle the window deiconified event
System.out.println("Window has been restored");
}
public void windowActivated(WindowEvent e) {
// Handle the window activated event
System.out.println("Window has been activated");
}
public void windowDeactivated(WindowEvent e) {
// Handle the window deactivated event
System.out.println("Window has been deactivated");
}
public static void main(String[] args) {
// Create and show the window
MyWindow window = new MyWindow();
window.setVisible(true);
}
}
In this example, the MyWindow
class extends the Frame
class and implements the WindowListener
interface. The windowOpened()
, windowClosing()
, windowClosed()
, windowIconified()
, windowDeiconified()
, windowActivated()
, and windowDeactivated()
methods are overridden to handle the corresponding events, and the addWindowListener()
method is called to register the MyWindow
instance as a listener for window events.
When a window event occurs, the corresponding method is called with a WindowEvent
parameter. The method then takes appropriate action based on the event.
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends Frame implements WindowListener {
public MyWindow() {
addWindowListener(this);
// add other components to the window
}
// implement the WindowListener interface methods
public void windowActivated(WindowEvent e) {
// handle window activation event
}
public void windowDeactivated(WindowEvent e) {
// handle window deactivation event
}
// other methods of the WindowListener interface
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
}
In this example, MyWindow
class extends the Frame
class and implements the WindowListener
interface. The addWindowListener(this)
statement registers the MyWindow
object as a listener for window events. The windowActivated()
and windowDeactivated()
methods are then overridden to handle the activation and deactivation events, respectively.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.