按下某个键时增加 JSpinner 上的步长

Increase step size on JSpinner when a certain key is pressed

我希望能够在按下某个键时增加 JSpinner 上的步长。到目前为止我尝试的是将 KeyListener 放在我的微调器上,当按下某个键时,更改步长的值。释放密钥后,它应该 return 为默认值。

我想我不必直接将 KeyListener 放到 JSpinner 上,而是放在它的按钮上。

老实说,我不知道如何实现。令我困惑的是双重 Listener.

这是我为 KeyListener 编写的代码:

public class SpinnerKeyIncrement implements KeyListener {

    JSpinner spinner;
    SpinnerNumberModel spinnerModel;

    public SpinnerKeyIncrement(JSpinner s) {
        this.spinner = s;
        if(spinnerModel == null)
            spinnerModel = (SpinnerNumberModel) spinner.getModel();
    }


    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
            spinnerModel.setStepSize(.10);
        }
        else if(e.getKeyCode() == KeyEvent.VK_SHIFT) {
            spinnerModel.setStepSize(1);
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        spinnerModel.setStepSize(0.01);

    }

    @Override
    public void keyTyped(KeyEvent e) {}

    }
}

同样,我会使用键绑定来解决这个问题,并且我会使用绑定到 JSpinner 的 InputMap 的绑定,它在获得焦点时处于活动状态:

  InputMap inputMap = spinner.getInputMap(JComponent.WHEN_FOCUSED);

一个有效的小示例程序可能如下所示:

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

@SuppressWarnings("serial")
public class ChangeStepSize extends JPanel {
   private static final String DOUBLE_STEP = "double step";
   public static final int SMALL_STEP_SIZE = 1;
   public static final int BIG_STEP_SIZE = 10;

   // bind to the "d" key, but you could use any key you'd like
   private static final int SPECIAL_KEY = KeyEvent.VK_D;
   private SpinnerNumberModel numberModel = new SpinnerNumberModel(50, 0, 100, 1);
   private JSpinner spinner = new JSpinner(numberModel);

   public ChangeStepSize() {
      add(spinner);

      // set up key bindings. First get InputMap and ActionMap
      InputMap inputMap = spinner.getInputMap(JComponent.WHEN_FOCUSED);
      ActionMap actionMap = spinner.getActionMap();

      // next set bindings for when key is pressed
      boolean onKeyRelease = false;
      KeyStroke keyStroke = KeyStroke.getKeyStroke(SPECIAL_KEY, 0, onKeyRelease);
      inputMap.put(keyStroke, DOUBLE_STEP + onKeyRelease);
      actionMap.put(DOUBLE_STEP + onKeyRelease, new DoubleStepAction(onKeyRelease));

      // next set bindings for when key is released
      onKeyRelease = true;
      keyStroke = KeyStroke.getKeyStroke(SPECIAL_KEY, 0, onKeyRelease);
      inputMap.put(keyStroke, DOUBLE_STEP + onKeyRelease);
      actionMap.put(DOUBLE_STEP + onKeyRelease, new DoubleStepAction(onKeyRelease));
   }

   // our Action is passed in a parameter to indicate which state it
   // gets activated in: key press or key release
   private class DoubleStepAction extends AbstractAction {
      private boolean onKeyRelease;

      public DoubleStepAction(boolean onKeyRelease) {
         this.onKeyRelease = onKeyRelease;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (onKeyRelease) {
            numberModel.setStepSize(SMALL_STEP_SIZE);
         } else {
            numberModel.setStepSize(BIG_STEP_SIZE);
         }
      }
   }

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

      JFrame frame = new JFrame("ChangeStepSize");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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