To close an AWT window in Java, you can use the dispose()
method. This method releases all of the native screen resources used by the Window, its subcomponents, and all of its owned children. Here is an example code snippet that shows how to close an AWT window:
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class CloseWindowExample { public static void main(String[] args) { Frame frame = new Frame("My AWT Window"); // Add a WindowAdapter to handle window close event frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // Call dispose() method to close the window ((Frame)e.getWindow()).dispose(); } }); // Set the size and visibility of the frame frame.setSize(400, 300); frame.setVisible(true); } }
In this example, we create a new Frame with the title “My AWT Window” and add a WindowAdapter to handle the window closing event. When the user clicks the close button on the window, the windowClosing()
method of the WindowAdapter will be called, and we can call the dispose()
method to close the window. Finally, we set the size and visibility of the frame to make it visible on the screen.
Different ways to override windowClosing() method:
In Java, there are different ways to override the windowClosing()
method, which is called when the user clicks on the close button of a window. Here are a few examples:
- Using a WindowAdapter class:
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // Code to handle window closing event } });
- Implementing the WindowListener interface:
frame.addWindowListener(new WindowListener() { public void windowClosing(WindowEvent e) { // Code to handle window closing event } // Other methods of WindowListener interface });
- Extending the WindowAdapter class:
class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent e) { // Code to handle window closing event } } frame.addWindowListener(new MyWindowAdapter());
- Overriding the method in a subclass of Frame or JFrame:
class MyFrame extends Frame { public void windowClosing(WindowEvent e) { // Code to handle window closing event } } MyFrame frame = new MyFrame();
Note that in all these examples, we are implementing or overriding the windowClosing()
method of the WindowListener
or WindowAdapter
interface. When the user clicks on the close button of the window, the windowClosing()
method of the registered listener will be called, and we can put our code to handle the window closing event in this method.
Close AWT Window Example 1: Anonymous class:
Here is an example of how to close an AWT window using an anonymous class in Java:
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class CloseWindowExample { public static void main(String[] args) { Frame frame = new Frame("My AWT Window"); // Add a window listener to handle window close event frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // Call dispose() method to close the window ((Frame)e.getWindow()).dispose(); } }); // Set the size and visibility of the frame frame.setSize(400, 300); frame.setVisible(true); } }
In this example, we create a new Frame
with the title “My AWT Window” and add a window listener using an anonymous class that overrides the windowClosing()
method. When the user clicks the close button on the window, the windowClosing()
method of the anonymous class will be called, and we can call the dispose()
method to close the window.
Note that in this example, we do not need to create a separate class that implements the WindowListener
interface or extends the WindowAdapter
class. Instead, we define the implementation of the windowClosing()
method directly in the anonymous class. This makes the code more concise and easier to read.
Close AWT Window Example 2: extending WindowAdapter:
Here is an example of how to close an AWT window by extending the WindowAdapter
class in Java:
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class CloseWindowExample extends WindowAdapter { private Frame frame; public CloseWindowExample(Frame frame) { this.frame = frame; } public void windowClosing(WindowEvent e) { // Call dispose() method to close the window frame.dispose(); } public static void main(String[] args) { Frame frame = new Frame("My AWT Window"); // Create a new CloseWindowExample instance and add it as a window listener CloseWindowExample listener = new CloseWindowExample(frame); frame.addWindowListener(listener); // Set the size and visibility of the frame frame.setSize(400, 300); frame.setVisible(true); } }
In this example, we create a new CloseWindowExample
class that extends the WindowAdapter
class and overrides the windowClosing()
method. The constructor of this class takes a Frame
object as a parameter and stores it in an instance variable. When the windowClosing()
method is called, we can access the Frame
object through this instance variable and call the dispose()
method to close the window.
In the main()
method, we create a new Frame
with the title “My AWT Window” and create a new instance of the CloseWindowExample
class, passing the Frame
object as a parameter. We then add this instance as a window listener to the Frame
. Finally, we set the size and visibility of the frame to make it visible on the screen.
This approach allows us to define the implementation of the windowClosing()
method in a separate class, which can be reused in different parts of our code.
Close AWT Window Example 3: implementing WindowListener:
Here is an example of how to close an AWT window by implementing the WindowListener
interface in Java:
import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CloseWindowExample implements WindowListener { private Frame frame; public CloseWindowExample(Frame frame) { this.frame = frame; } public void windowClosing(WindowEvent e) { // Call dispose() method to close the window frame.dispose(); } // Other methods of WindowListener interface public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public static void main(String[] args) { Frame frame = new Frame("My AWT Window"); // Create a new CloseWindowExample instance and add it as a window listener CloseWindowExample listener = new CloseWindowExample(frame); frame.addWindowListener(listener); // Set the size and visibility of the frame frame.setSize(400, 300); frame.setVisible(true); } }
In this example, we create a new CloseWindowExample
class that implements the WindowListener
interface and overrides the windowClosing()
method. The constructor of this class takes a Frame
object as a parameter and stores it in an instance variable. When the windowClosing()
method is called, we can access the Frame
object through this instance variable and call the dispose()
method to close the window.
We also need to implement the other methods of the WindowListener
interface, even if we don’t need to use them in this example.
In the main()
method, we create a new Frame
with the title “My AWT Window” and create a new instance of the CloseWindowExample
class, passing the Frame
object as a parameter. We then add this instance as a window listener to the Frame
. Finally, we set the size and visibility of the frame to make it visible on the screen.
This approach allows us to handle all the events defined in the WindowListener
interface, not just the windowClosing()
event.