Java learning notes swing-Basic swing Program (episode -- accelerator key)

Source: Internet
Author: User

I have nothing to do with reading Java swing, 2nd edition. It is quite interesting to see that there is an accelerator key. It seems that it can only be used with alt. A lot of things are the meaning of the original article only after reading the program. This is an episode and is not within the scope of mapseditor. By the way, three to four people joined the group at noon, which was quite fun. Even if you are a beginner, it is important for you to communicate and speak out your thoughts. If you are wrong, you will be wrong. Who is right ......

For accelerator keys, it is important to do GUI work. At least people who thought they didn't use the mouse were very good at it, in Windows, it's okay to use it. The program was moved down from the book and found it interesting. You can check it out.

// 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 ("first 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. setdefaclocloseoperation (jframe. exit_on_close );
F. setcontentpane (P );
F. Pack ();
F. setvisible (true );
}
}

 

If you have a Java environment, you can copy it, compile it, run it, and press Alt + F, ALT + I, and ALT + L to convert the foucs between the three textfields. The key is firstlabel. setdisplayedmnemonic ('F') and firstlabel. setlabelfor (firstfield), the former setdisplayedmnemonic (char) selects which character is the shortcut key, just like the "&" function in VC, And the setlabelfor (COMP) it indicates the control to which the focus is located after you press the corresponding shortcut key (the acceleration key) (of course, the focus control should be included ). It should be noted that the second shortcut key is set to "middlelabel. setdisplayedmnemonicindex (7);", which requires sdk1.4 support. This function is to set the displayed underline to a few characters. Obviously, for "I", there are two "I" in "middle initial", which is partially case-sensitive. Therefore, according to the first or third practice, by default, the program will underline the first character of the text in the jlabel, that is, the "M"IDdle initial. What we want is "MiddleITherefore, set the underline to the seventh character "I. There is nothing to do with other things. You should be able to find out more about this. Inception ~~

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.