Pages

Close JFrame using mouse click

Source code below will show you, how to close JFrame when click on it.

*****************************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameUsingMouseClick.java
*****************************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseClick
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone click on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK IN THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("CLICK IN THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


*****************************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************************
Related Posts Plugin for WordPress, Blogger...