我对Swing相当陌生,并且在理解KeyBinding概念时遇到麻烦。
我试图将JButton添加到我的JPanel中,然后设置其操作命令和KeyBinding。这是我的代码:

JButton b = new JButton("1");
MyAction myaction = new MyAction("1");
b.setAction(myaction);
b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "one");
b.getActionMap().put("one", myaction);
b.setActionCommand("one");


MyAction这里是一个内部类

class MyAction extends AbstractAction {
    public MyAction(String text) {
        super(text);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Action command is: " + e.getActionCommand());
    }
}


现在,如果按下按钮,我将得到以下输出:

Action command is: one


但是,如果我在键盘上按“ 1”,则会得到以下信息:

Action command is: 1


为什么呢?而且,无论该操作是通过按下按钮还是通过键盘绑定触发的,我应该怎么做才能获得相同的操作命令?

最佳答案

考虑在AbstractAction本身中设置Action命令键:

class MyAction extends AbstractAction {
    public MyAction(String text) {
        super(text);
        putValue(ACTION_COMMAND_KEY, "one"),
    }
    //...


例如:

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

public class Foo2 extends JPanel {
   private static final String[] NUMBER_TEXTS = {
      "one", "two", "three", "four", "five"
   };


   public Foo2() {

      for (int i = 0; i < NUMBER_TEXTS.length; i++) {
         String numberString = String.valueOf(i + 1);
         Action numberBtnAction = new NumberBtnAction(numberString, NUMBER_TEXTS[i]);
         JButton btn = new JButton(numberBtnAction);
         InputMap inMap = btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
         ActionMap actionMap = btn.getActionMap();

         KeyStroke keyStroke = KeyStroke.getKeyStroke(numberString.charAt(0));
         inMap.put(keyStroke, NUMBER_TEXTS[i]);
         actionMap.put(NUMBER_TEXTS[i], numberBtnAction);

         add(btn);
      }

   }

   private class NumberBtnAction extends AbstractAction {
      public NumberBtnAction(String numberString, String numberText) {
         super(numberString);
         putValue(ACTION_COMMAND_KEY, numberText);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         System.out.println(e.getActionCommand());
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Foo2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Foo2());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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




编辑
或者,您可以执行单独的按键绑定操作,只需按一下按钮即可:

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

public class Foo2 extends JPanel {
   private static final String[] NUMBER_TEXTS = {
      "one", "two", "three", "four", "five"
   };


   public Foo2() {

      for (int i = 0; i < NUMBER_TEXTS.length; i++) {
         String numberString = String.valueOf(i + 1);
         Action numberBtnAction = new NumberBtnAction(numberString, NUMBER_TEXTS[i]);
         JButton btn = new JButton(numberBtnAction);
         Action pressBtnAction = new PressButtonAction(btn);
         InputMap inMap = btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
         ActionMap actionMap = btn.getActionMap();

         KeyStroke keyStroke = KeyStroke.getKeyStroke(numberString.charAt(0));
         inMap.put(keyStroke, NUMBER_TEXTS[i]);
         //!! actionMap.put(NUMBER_TEXTS[i], numberBtnAction);
         actionMap.put(NUMBER_TEXTS[i], pressBtnAction);

         add(btn);
      }

   }

   private class NumberBtnAction extends AbstractAction {
      public NumberBtnAction(String numberString, String numberText) {
         super(numberString);
         putValue(ACTION_COMMAND_KEY, numberText);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         System.out.println(e.getActionCommand());
      }
   }

   private class PressButtonAction extends AbstractAction {
      private AbstractButton btn;

      public PressButtonAction(AbstractButton btn) {
         this.btn = btn;
      }


      @Override
      public void actionPerformed(ActionEvent e) {
         btn.doClick();
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Foo2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Foo2());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

关于java - JButton KeyBinding和setActionCommand,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25923060/

10-10 06:04