简体   繁体   English

关于setMnemonic的一些问题

[英]some questions about setMnemonic

I have the code: 我有代码:

public class MenuBar extends JFrame {

    public MenuBar() {
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();

        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);

        JMenuItem eMenuItem = new JMenuItem("Exit");
        JMenuItem oMenuItem = new JMenuItem("Open Another");
        eMenuItem.setMnemonic(KeyEvent.VK_E);
        oMenuItem.setMnemonic(KeyEvent.VK_O);
        eMenuItem.setToolTipText("Exit application");
        oMenuItem.setToolTipText("Open another Window");
        eMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        oMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                main(null);
            }
        });

        file.add(eMenuItem);
        file.add(oMenuItem);
        menubar.add(file);

        setJMenuBar(menubar);

        setTitle("Simple menu");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MenuBar ex = new MenuBar();
                ex.setVisible(true);
            }
        });
    }
}

it works fine but I had a question about setMnemonic. 它工作正常,但我有一个关于setMnemonic的问题。 How would you go about making the Mnemonic for eMenuItem just to press E, as opposed to Alt + E? 你会如何为eMenuItem制作助记符只是为了按E而不是Alt + E? Thanks for any and all help! 感谢您的帮助! (Please not I left imports out intentially, for length issues) (请注意,对于长度问题,我不会完全保留进口)

From the docs of setMnemonic: 来自setMnemonic的文档

The mnemonic is the key which when combined with the look and feel's mouseless modifier (usually Alt) will activate this button if focus is contained somewhere within this button's ancestor window. 助记符是与外观和无鼠标修改器(通常为Alt)结合使用的键,如果焦点包含在此按钮的祖先窗口中的某个位置,则会激活此按钮。

Thus using setMnemonic to do this is impossible. 因此使用setMnemonic来做这件事是不可能的。

However, you can use the setAccelerator method defined for JMenuItem s, passing a keystroke like KeyStroke.getKeyStroke('e'); 但是,您可以使用为JMenuItem定义的setAccelerator方法,传递KeyStroke.getKeyStroke('e');类的击键KeyStroke.getKeyStroke('e');

Alternatively, you could, as Joop Eggen pointed out in the comments to this answer use a MenuKeyListener which allows for greater control over the specific events that the action is performed on. 或者,您可以像Joop Eggen在本答案的评论中指出的那样使用MenuKeyListener ,它允许更好地控制执行操作的特定事件

I don't know if this would work but you can try it out. 我不知道这是否有用,但你可以尝试一下。 From this ( http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_E ) we see that the ASCII binding for VK_E is 69. This is caps E . 从这里( http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_E )我们看到VK_E的ASCII绑定是69.这是上限E To get the one for small e its 101, which corresponds to VK_NUMPAD5 . 获得小e的那个101,它对应于VK_NUMPAD5 I might be wrong though, its just a guess. 我可能错了,只是一个猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM