【问题标题】:Responding to Button using KeyBIndings使用 KeyBIndings 响应按钮
【发布时间】:2012-05-14 22:02:43
【问题描述】:

我想制作一个具有这些目标的程序:

1) 制作一个 JButton 2) 使用 KeyBindings 将按钮附加到键(“A”键) 3) 单击“A”时执行一些代码

这是我目前的代码:

// Imports

Public class Test{

JButton button = new JButton();

//...

Test(){

button.getInputMap().put(KeyStroke.getKeyStroke("A"), "Pressed");


//...

}

// Where do I add the code that responds when button is pressed?
}

现在我在哪里添加我希望它在按下按钮时执行的代码?

【问题讨论】:

    标签: java swing jbutton key-bindings


    【解决方案1】:

    我能想到的两种方法:

    • 让 JButton 和 Key 绑定共享相同的 AbstractAction,或者更好
    • 只需通过键绑定在按钮上调用doClick()

    KeyBindingEg.java

    import java.awt.event.*;
    import javax.swing.*;
    
    public class KeyBindingEg extends JPanel {
       private JButton btnA = new JButton();
    
       public KeyBindingEg() {
          Action btnAction = new ActionOne("A");
          Action keyBindingAction = new ActionTwo();
    
          int condition = JLabel.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputmap = btnA.getInputMap(condition);
          ActionMap actionmap = btnA.getActionMap();
    
          final String aKeyPressed = "a key pressed";
          inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), aKeyPressed );
    
          actionmap.put(aKeyPressed, keyBindingAction);
          // actionmap.put(aKeyPressed, btnAction); // one or the other, your choice
    
          btnA.setAction(btnAction);
          add(btnA);
       }
    
       private class ActionOne extends AbstractAction {
          public ActionOne(String text) {
             super(text);
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             sharedMethod();
          }
       }
    
       private class ActionTwo extends AbstractAction {
          @Override
          public void actionPerformed(ActionEvent e) {
             btnA.doClick();
          }
       }
    
       private void sharedMethod() {
          System.out.println("Method called by either key bindings or action listener");
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("KeyBindingEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new KeyBindingEg());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    【讨论】:

      【解决方案2】:

      您需要添加一个动作侦听器,特别是针对 actionPerformed。在你的构造函数中声明这个:

      import java.awt.event.ActionEvent;
      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.JButton;
      import javax.swing.JComponent;
      import javax.swing.KeyStroke;
      
      public class Main {
          public static void main(String[] argv) throws Exception {
      
              JButton component = new JButton();
              MyAction action = new MyAction();
              component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"),
                  action.getValue(Action.NAME));
          }
      }
      
      class MyAction extends AbstractAction {
          public MyAction() {
              super("my action");
          }
      
          public void actionPerformed(ActionEvent e) {
              //Here goes the code where the button does something
              System.out.println("hi");//In this case we print hi
          }
      }
      

      在本例中,如果我们按下 F2,则相当于按下按钮。

      【讨论】:

      • 如果我有多个按钮怎么办?我是否使用相同的 actionPerformed 方法?
      • 创建另一个类,在这种情况下它的名称是 MyAction,我们将其实例化为动作,我们可以为每个按钮创建一个新类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2021-01-13
      • 1970-01-01
      • 2010-09-25
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多