Atitit. form Listening ESC exit this form java Swing C #. NET PHP
1. Monitor ESC key 1
1.1. Listening on a component 1
1.2. Monitoring added to form 1
2. Close Form 2
1. MonitoringESCKey
1.1. Listen for a component 1.2. Monitor added toformon
Author :: Old Wow's paw attilax ayron, email:[email protected]
Reprint please indicate source: Http://blog.csdn.net/attilax
Final Toolkit Toolkit = Toolkit.getdefaulttoolkit ();
Toolkit.addawteventlistener (New Awteventlistener () {
@Override public void eventdispatched (AWTEvent e) {
if (e.getid () = = keyevent.key_pressed) {
KeyEvent evt = (keyevent) e;
if (evt.getkeycode () = = Keyevent.vk_escape) {
Frame.dispose ();
System.exit (0);
}
}
}
},awtevent.key_event_mask);
Java Code
Toolkit.getdefaulttoolkit (). Addawteventlistener (New Awteventlistener () {
.............
});
This is the best way. Listens for all events. No matter on which componet, Button,form and so on.
This does not work, possibly focus to one component on the bar. Three, this only event tied to a frm on the blue.
Frame.addkeylistener (New Keyadapter () {
@Override
public void keypressed (KeyEvent e) {
if (Keyevent.vk_escape = = E.getkeycode ()) {
System.exit (0);
// System.out.println ("---colose...");
// frame.dispose ();
// }
// }
// });
2. Close the form
Just set this.setdefaultcloseoperation (dispose_on_close) in each Frame ; can be.
Exit_on_close, close the program. (All windows and processes will be closed)
Dispose_on_close, close this window only.
Hide_on_close, hide this window only, do not close.
Do_nothing_on_close, do nothing, click Close Invalid.
This is simple, as long as you call the Dispose method. Hide is setvisible (false). Close the program is system.exit (0);
Frame . setdefaultcloseoperation (javax.swing.WindowConstants. Dispose_on_close );
if (evt. getKeyCode () = = KeyEvent. Vk_escape ) {
Frame. Dispose ();
//system.exit (0);
}
Reference
Java how to close a jframe_ by button Baidu knows. htm
Atitit. Form listening ESC exit This form Java Swing C #. NET PHP