Understand the accelerator Key in Java Swing

Source: Internet
Author: User
Tags requires

Foreword: Nothing is Watching "Java Swing, 2nd Edition", see something is still quite interesting to say, is accelerator key, hehe, seems to cooperate with ALT can play a role. Quite a lot of things are seen in the program before the meaning of the original text found. This is an episode, not within the bounds of Mapseditor. By the way, it's nice to have 3 to 4 people at noon. Master or beginner, everyone is important to communicate, boldly speak their own ideas, wrong is wrong, who did not miss Ah ...

About the accelerator key, in the GUI when it is more important bar, at least the previous thought that those who do not use the mouse is a good master, hehe, they will also deliberately remember some of the commonly used shortcuts, in Windows with the use of a make up that, hehe. The program is moved down from the book, it is very interesting, we can see.

//Mnemoniclabels.java


//


import javax.swing.*;


import java.awt.*;





//Shows how displayedmnemonic and Labelfor properties work together


public class mnemoniclabels ... {


public static void Main (string[] args) ... {





JTextField Firstfield = new JTextField (10);


JTextField Middlefield = new JTextField (10);


JTextField Lastfield = new JTextField (10);





//Create labels and mnemonics.


JLabel firstlabel = new JLabel ("A-Name", jlabel.right);


firstlabel.setdisplayedmnemonic (' F ');


firstlabel.setlabelfor (Firstfield);





JLabel middlelabel = new JLabel ("Middle Initial", jlabel.right);


middlelabel.setdisplayedmnemonic (' I ');


middlelabel.setdisplayedmnemonicindex (7); Requires 1.4


middlelabel.setlabelfor (Middlefield);





JLabel lastlabel = new JLabel ("Last Name", jlabel.right);


lastlabel.setdisplayedmnemonic (' L ');


lastlabel.setlabelfor (Lastfield);





//Layout and display


JPanel p = new JPanel ();


p.setlayout (New GridLayout (3, 2, 5, 5));


p.add (Firstlabel);


p.add (Firstfield);


p.add (Middlelabel);


p.add (Middlefield);


p.add (LastLabel);


p.add (Lastfield);





JFrame f = new JFrame ("Mnemoniclabels");


f.setdefaultcloseoperation (jframe.exit_on_close);


F.setcontentpane (p);


f.pack ();


f.setvisible (true);


}


}

The Java environment can be copied and then compiled and run to see, then press Alt+f, Alt+i, Alt+l,foucs, respectively, will be converted between three TextField. The key is Firstlabel.setdisplayedmnemonic (' F ') and Firstlabel.setlabelfor (Firstfield), the previous one setdisplayedmnemonic (char) Select which character is the shortcut key, like VC inside "&" the same function, and the back of this setlabelfor (comp) is that you press the corresponding shortcut key (accelerator) after the focus on which control (of course, the focus of the control). Note that the second shortcut key is set "Middlelabel.setdisplayedmnemonicindex (7);" This requires the support of the SDK1.4, which is to set the underline of the display to the first few characters. Obviously, we notice that for "I", "There are two" I "in the Middle Initial", and this is partially case, so the first or third procedure will be the default JLabel the first word in the text to meet the alphanumeric underline, that is to say. " Middle Initial ", we want the" middle Initial "effect, so the underline set to the seventh character" I "above. The other is no fun, except this should be able to explore understand it, hehe.

Related Article

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.