Hotkeys creation for java swing form

回眸只為那壹抹淺笑 提交于 2019-12-08 14:19:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!