Pages

Close JFrame when cursor exit from JFrame

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


import javax.swing.JFrame;

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

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

//Create JFrame with title ( GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY )
JFrame frame=new JFrame("GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY");

//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 : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

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


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