问题
How to Create hot keys for form which is made by java swing?for example the form is create for student details means if I press ALT+N means the cursor will goto that name entering field.ALT+R means the cursor will goto Reg.no entering Field.same like as mark1(m1),mark(2) and so on.At that same time the form contains save,exit buttons if I press CTRL+S means the Save button will select.CTRL+X means the exit button will select.how to do this?
回答1:
See How to Use Key Bindings, then use that knowledge in conjunction with requestFocusInWindow().
CTRL+X means the exit button will select.
See also setMnemonic(char) for buttons and setAccelerator(KeyStroke) for menu items. Or more generally, constructing those controls using an Action that has the values configured.
回答2:
Please refer the following link
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html It might be a good place to start.
回答3:
means if I press ALT+N means the cursor will goto that name entering field
This is generally done by using JLabel/JTextField pairs. Something like:
JLabel firstNameLabel = new JLabel("First Name");
JTextField firstNameTextField(15);
firstNameLabel.setLabelFor( firstNameTextField );
firstNameLabel.setDisplayedMnemonic( 'F' );
panel.add( firstNameLabel );
panel.add( firstNameTextField );
Then using Alt-F will set focus on the text field.
The Key Bindings will be done automatically for you.
来源:https://stackoverflow.com/questions/8584998/hotkeys-creation-for-java-swing-form