setMnemonic()并按键调用方法

时间:2014-06-08 17:43:09

标签: java swing key pressed mnemonics

按键(Alt + H)时,我必须手动运行方法()。

if("The key pressed==(Alt+H)"){
    callMethod();
}

public void callMethod(){
    //Some codes here
}

我如何在Java中实际执行此操作。请给我一个简单的方法来做到这一点。

3 个答案:

答案 0 :(得分:5)

这里值得一读的是关于Oracle Tutorial - Enabling Keyboard Operation的详细解释以及样本。

详细了解Oracle Tutorial - How to Use Key Bindings

直接来自上述教程的一些例子:

//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
                     KeyEvent.VK_H);

//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_H);

//Setting the accelerator:
menuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_H, ActionEvent.ALT_MASK));

在此处阅读更多Oracle Tutorial - How to Use Buttons, Check Boxes, and Radio Buttons

示例代码:(Alt-H会点击中间按钮)

JButton b2 = new JButton("Middle button", middleButtonIcon);
b2.setMnemonic(KeyEvent.VK_H);

答案 1 :(得分:1)

如果使用菜单,则可以使用setMnemonic(),有关示例,请参阅How to Use Menus。另一种选择是使用Key Bindings。例如:

import java.awt.event.*;
import javax.swing.*;

public class TestKeys {
    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("Keys");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.ALT_MASK);

        Action testAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                 JOptionPane.showMessageDialog(frame, "Alt-H pressed");
            }
        };

        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "TestAction");
        frame.getRootPane().getActionMap().put("TestAction", testAction);       

        JLabel label = new JLabel("Hit Alt-H");
        label.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        frame.add(label);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

答案 2 :(得分:0)

最好的方法是使用setMnemonic(),因为它是最简单的。

查看此文章了解更多http://www.herongyang.com/Swing/JMenuBar-Set-Keyboard-Mnemonics-on-Menu-Items.html

private JMenu getColorMenu() {
  JMenu myMenu = new JMenu("Color");
  ButtonGroup myGroup = new ButtonGroup();

  JRadioButtonMenuItem myItem = new JRadioButtonMenuItem("Red");
  myItem.setSelected(true);
  myItem.setMnemonic(KeyEvent.VK_R);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  myItem = new JRadioButtonMenuItem("Green");
  myItem.setMnemonic(KeyEvent.VK_G);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  myItem = new JRadioButtonMenuItem("Blue");
  myItem.setMnemonic(KeyEvent.VK_B);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  return myMenu;