Creating a frame in JAVA and closing it by clicking the close button


For, creating a frame and closing frame following code should be followed.


What is Windows Listener?

Windows Listener: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest).

ADVERTISEMENT


Code:

import java.awt.*;
import java.awt.event.*

class MyFrame extends Frame {
public static void main(String[] args) {
//creating a class

MyFrame f = new MyFrame();

//set a title for myframe

f.setTitle("My First Frame");

//set the size of frame
f.setSize(400,350);

//displaying the frame
f.setVisible(true);

//close the frame
f.addWindowListener(new Myclass());
}
}
class Myclass implements WindowListener
{
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}


Output:



Also read:
Previous Post Next Post

Contact Form