To practically close the window, you must first make it invisible to the user and then free all the resources it occupies.
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame
{
public class CloseWindowEvent extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
//Make the frame invisible
MyFrame.this.setVisible(false);
//Free all resources
MyFrame.this.dispose();
//If you also want to close the program,uncomment bellow
//System.exit(0);
}
}
public MyFrame(String title)
{
super(title);
this.add(new Label("Click X to Close"));
//Adds the listener to the class
this.addWindowListener(new CloseWindowEvent());
this.pack();
}
}
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!