我正在尝试将我的菜单合并到两个类似的应用程序中,它们共享大量的类。部分原因是我正试图将我所能做的一切付诸行动。我遇到的问题是,我想要相同的加速器为菜单项。我是否可以在操作中设置它,这样就不必重复设置加速器的代码了吗?
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调用移到我的action类中。
有办法这样做吗?
发布于 2013-08-22 20:04:44
加速器键有一个值,比如简短的描述,对于你来说,它是这样写的:
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));如果您使用Eclipse作为IDE,它应该在键入putValue并按自动完成快捷方式时告诉您可用的键。
https://stackoverflow.com/questions/18389674
复制相似问题