Atitit. Form listening ESC exit This form Java Swing C #. NET PHP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.