如何更改ActionListener的焦点JButton的默认键绑定?

时间:2019-01-10 14:59:52

标签: java swing key-bindings keystroke

调用焦点JButton的ActionListener的默认键是Space,但是如何将其更改为另一个键?

2 个答案:

答案 0 :(得分:3)

这不是很简单,需要:

  • 为JButton使用正确的InputMap,它与JComponent.WHEN_FOCUSED条件相关联
  • 然后,您需要获取并删除与空格键关联的按钮的InputMap和ActionMap,对于 *两者键,请按 密钥被释放。
  • 然后将两个动作与您感兴趣的键相关联,再次按下并释放两个键。

示例:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class AlterSpaceBinding extends JPanel {
    private JButton myButton = new JButton("My Button -- associate with \"B\" key");

    public AlterSpaceBinding() {
        myButton = alterDefaultButtonAction(myButton, KeyEvent.VK_B);
        myButton.addActionListener(l -> {
            System.out.println("button pressed");
        });
        add(myButton);
        add(new JButton("Second Button -- no change"));

    }

    public static JButton alterDefaultButtonAction(JButton button, int desiredKeyCode) {

        // get the correct InputMap
        int condition = JComponent.WHEN_FOCUSED;
        InputMap inputMap = button.getInputMap(condition);
        ActionMap actionMap = button.getActionMap();

        // empty action that does nothing
        Action emptyAction = new AbstractAction() {            
            @Override
            public void actionPerformed(ActionEvent e) {
                // This does NOTHING
            }
        };

        // get both key strokes for space key, but pressed and released
        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        // get input map key for pressed and released
        String keyForSpacePressed = (String) inputMap.get(spaceKeyPressed);
        String keyForSpaceReleased = (String) inputMap.get(spaceKeyReleased);

        // get actions for press and release
        Action actionForSpacePressed = actionMap.get(keyForSpacePressed);        
        Action actionForSpaceReleased = actionMap.get(keyForSpaceReleased);

        // substitute empty action
        actionMap.put(keyForSpacePressed, emptyAction);
        actionMap.put(keyForSpaceReleased, emptyAction);

        // key stroke for desired key code
        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        // put in the original actions to where wanted
        inputMap.put(desiredKeyPressed, desiredKeyPressed.toString());
        actionMap.put(desiredKeyPressed.toString(), actionForSpacePressed);
        inputMap.put(desiredKeyReleased, desiredKeyReleased.toString());
        actionMap.put(desiredKeyReleased.toString(), actionForSpaceReleased);

        return button;
    }

    private static void createAndShowGui() {
        AlterSpaceBinding mainPanel = new AlterSpaceBinding();

        JFrame frame = new JFrame("AlterSpaceBinding");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

答案 1 :(得分:3)

您需要:

  1. 更新组件的InputMap,以添加新的KeyStroke指向现有的Action

  2. 防止KeyStroke中现有的InputMap调用现有的Action。如果您希望能够同时使用两个KeyStroke来调用默认的Action,则此步骤是可选的。

注意:

  1. 您需要为“已按下”和“释放”键更新InputMap
  2. 必须按照上述顺序完成对InputMap的更新。

您可以执行以下操作修改Hovercrafts示例中的代码:

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

@SuppressWarnings("serial")
public class AlterSpaceBinding extends JPanel {
    private JButton myButton = new JButton("My Button -- associate with \"B\" key");

    public AlterSpaceBinding() {
        myButton = alterDefaultButtonAction(myButton, KeyEvent.VK_B);
        myButton.addActionListener(l -> {
            System.out.println("button pressed");
        });
        add(myButton);
        add(new JButton("Second Button -- no change"));

    }

    public static JButton alterDefaultButtonAction(JButton button, int desiredKeyCode) {

        // get the correct InputMap
        InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);

        // get both key strokes for space key, but pressed and released
        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        // key stroke for desired key code
        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        // share the Action with desired KeyStroke
        inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
        inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));

        // disable original KeyStrokes (optional)
        inputMap.put(spaceKeyPressed, "none");
        inputMap.put(spaceKeyReleased, "none");

        return button;
    }

    private static void createAndShowGui() {
        AlterSpaceBinding mainPanel = new AlterSpaceBinding();

        JFrame frame = new JFrame("AlterSpaceBinding");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

上面的代码不需要:

  1. 创建一个虚拟Action
  2. 操纵ActionMap

请查看Using Key Bindings,以了解操纵组件的InputMapActionMap的更多示例。

注意:

更可能的情况是,您想更新应用程序中所有按钮的绑定,以使您拥有通用的LAF。在这种情况下,您可以从UIManager更新所有JButton共享的“ focusInputMap”:

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

public class SSCCE extends JPanel
{
    SSCCE()
    {
        add( new JButton("Button 1" ) );
        add( new JButton("Button 2" ) );
        add( new JButton("Button 3" ) );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );

        // Update shared InputMap

        InputMap inputMap = (InputMap)UIManager.get("Button.focusInputMap");
        int desiredKeyCode = KeyEvent.VK_B; // type "b" to invoke Action on button

        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        // key stroke for desired key code
        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        // share the Action with desired KeyStroke
        inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
        inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));

        // optionally disable original KeyStrokes
        inputMap.put(spaceKeyPressed, "none");
        inputMap.put(spaceKeyReleased, "none");
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}