JMenuItem set shortcut keys

Source: Internet
Author: User

SetAccelerator (Keystroke.getkeystroke (mnemonic, modifier)); Use of VB know press F5 is run form, using Photoshop know to press TAB is to hide all the panels, your Java program also want to have this function. So hopefully this article will help you. Submenu Class JMenuItem There is a way to not know if you have noticed: SetAccelerator. Open the Help file the description of this method is to set a key combination that directly invokes the action listener of the menu item without having to display the hierarchy of the menus. So we can use this method to make the shortcuts we want. The Help file tells us that what is passed to the SetAccelerator method is a keystroke, and the following is the class: Keystoke under the javax.swing package, his role binds us to the key, To the above mentioned SetAccelerator and other methods to use, the Keystoke class provides a few "bundle" static overload method, here are a few examples, you have to try it: F5:KeyStroke.getKeyStroke ( KEYEVENT.F5, 0); Ctrl+q:keystroke.getkeystroke (Keyevent.vk_q, Inputevent.ctrl_mask); A:keystroke. Getkeystroke (' a '); Alt+shift+c:getkeystroke (Keyevent.vk_c, Inputevent.alt_mask | Inputevent.shift_mask, True); Of course, if you don't want to introduce some such as keyevent or inputevent, you can simplify him with the Keystroke.getkeystroke (String s) method:         f5:keystroke.getkeystroke ("F5");  ctrl+q:keystroke.getkeystroke ("Control Q"); A:keystroke.getkeystroke ("typed a"); Alt+shift+c:getkeystroke ("alt Shift released X"); Add a shortcut key for the submenu "Open" (named: Meuopen) Ctrl+o:meuopen.setacceLerator (Keystroke.getkeystroke ("Control O")); The above is the submenu shortcut key settings, now next to the other control shortcut key settings: To open the Help file, into the Javax.swing.AbstractButton, you can find setmnemonic (int mnemonic), This is the AbstractButton subclass set shortcut keys, such as a button btn, set the shortcut key is: Btn.setmnemonic (KEYEVENT.A), its shortcut is alt+a, it has a cosmetic method Setdisplayedmnemonicindex (int index), the role is to set the title of the first few words alphanumeric underline to identify shortcut keys, such as Btn.settext ("Open"); Btn.setdisplayedmnemonicindex (0), the title of the btn becomes open. You are wise to bypass the class, you can certainly succeed. However, note the following: 1, when using Keystroke's Getkeystroke (String s) method, you should pay attention to the usage such as CTRL with control, alt for ALT, and uppercase if you add letters, but if you have only one key like a, you should use lowercase: "Type a". 2, Keystroke's Getkeystroke (String s) method is convenient, but if the use of illegal syntax is not up to the goal, then you can try the keystroke of other static methods. 3. Shortcut keys for AbstractButton subclasses use the Setmnemonic method to set only the keys specified by ALT +.

From

Http://blog.csdn.net/taizhoula/archive/2008/03/07/2156869.aspx

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.