我正试图将我的大部分菜单整合到两个共享大量类的类似应用程序中。这其中的一部分是我正在努力把我能做的一切付诸行动。我遇到的问题是,我希望菜单项使用相同的加速器。有没有一种方法可以在操作中设置它,这样我就不必重复设置加速器的代码?
package com.protocase.viewer.actions;
import com.protocase.viewer.DesignerApplication;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.MNEMONIC_KEY;
import static javax.swing.Action.SHORT_DESCRIPTION;
/**
*
* @author davidh
*/
public class NewEnclosureAction extends AbstractAction{
private DesignerApplication app;
public NewEnclosureAction(DesignerApplication app) {
super();
this.app = app;
putValue(SHORT_DESCRIPTION, "New");
putValue(AbstractAction.NAME, "New");
putValue(MNEMONIC_KEY, KeyEvent.VK_N);
}
@Override
public void actionPerformed(ActionEvent e) {
app.OnNew();
}
}
JMenuItem newMit = new JMenuItem(new NewEnclosureAction(this));
newMit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
newMit.getAccessibleContext().setAccessibleDescription("New Enclosure from template");
fileMenu.add(newMit);
我希望将setAccelerator调用移动到我的操作类中。
有办法做到这一点吗?
快捷键有一个值,比如简短的描述,在您的情况下,它会写成这样:
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
如果您使用Eclipse作为IDE,那么在键入putValue
并点击自动完成快捷键时,它应该会告诉您可用的键。