我有点问题。我想将这个代码用于两个微调器,我无法处理它。谢谢你的帮助
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:
}
});
答案 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);