: : : : : hello,
: : : : : want my menuitem to show the text "New(N) Ctrl+N",and when i press the combination key ctrl and N the system will create an Event,which has the same effect with I click the menuitem.
: : : : : My code is:
: : : : :
: : : : :
: : : : : javax.swing.KeyStroke stroke;
: : : : : int event=java.awt.event.KeyEvent.VK_N;
: : : : : int inputEvent=java.awt.event.InputEvent.CTRL_DOWN_MASK;
: : : : : stroke=javax.swing.KeyStroke.getKeyStroke(event, inputEvent);
: : : : : jMenuItemNew.setAccelerator(stroke);
: : : : :
: : : : :
: : : : : but when i press the combination key Ctrl and N,there is no response.
: : : : : Did i do wrong?
: : : : : Any suggestions would be welcom!
: : : : : thanks in advance!
: : : : :
: : : : Why so elaborate? The KeyStoke class defines a static method called getKeystroke(), which creates the necessary key stroke object:
: : : :
: : : : jMenuItemNew.setAccelerator(KeyStroke.getKeyStroke(Character.valueOf("n"),
: : : : java.awt.event.InputEvent.CTRL_MASK));
: : : :
: : : : Also check the constants you're using.
: : : :
: : :
: : : can you help me with the method setMnemonic?
: : : my code is:
: : : jMenuItemNew.setMnemonic(java.awt.event.KeyEvent.VK_N);
: : : and i want there is a line under the letter N,how can i realize it?
: : : thanks
: : :
: : When you set the Mnemonic the line will appear automatically. It did in my program. You don't need to perform any other programming.
: :
:
:
: the line didn't appear in my program,though when i press the combination key Alt and N, it has the same effect with i click the menuitem .
:
It might be that the line is not visible due to the setting "Hide indicator until alt is presses" in windows. I have that setting turned off.