JavaFX Spinner - 与更多微调器相同的关键事件

时间:2017-11-17 09:04:03

标签: java javafx

我有点问题。我想将这个代码用于两个微调器,我无法处理它。谢谢你的帮助

        spinner1.getEditor().setOnKeyPressed(event -> {
        switch (event.getCode()) {
            case UP:
                spinner1.increment(1);
                break;
            case DOWN:
                spinner1.decrement(1);
                break;
            default:
        }
    });
        spinner2.getEditor().setOnKeyPressed(event -> {
        switch (event.getCode()) {
            case UP:
                spinner2.increment(1);
                break;
            case DOWN:
                spinner2.decrement(1);
                break;
            default:
        }
    });

1 个答案:

答案 0 :(得分:1)

使用单个EventHandler实例

 EventHandler eh = (EventHandler<KeyEvent>) event -> {
      Spinner sp = (Spinner) event.getSource();
      switch (event.getCode()) {
          case UP:
              sp.increment(1);
              break;
          case DOWN:
              sp.decrement(1);
              break;
          default:
      }
  };

 spinner1.getEditor().setOnKeyPressed(eh);
 spinner2.getEditor().setOnKeyPressed(eh);