我创建了一个带有值的 Integer Spinner
min (5)、max (15) 和 initialValue (12)
和 wrapAround (true)
。
一旦微调器在递增期间达到max (15)
值,而不是像documentation 中所说的那样将值重置为min (5)
, 它被重置为值 10 (max (15) - min (5))
public final void setWrapAround(boolean value)
Sets the value of the property wrapAround.
Property description:
The wrapAround property is used to specify whether the value factory should be circular. For example, should an integer-based value model increment from the maximum value back to the minimum value (and vice versa).
注意:递减正常工作,一旦达到 min (5)
值,Spinner 值会自动设置为 max (15)
public class IntSpinnerTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
var spinner = new Spinner<Integer>();
var factory = new SpinnerValueFactory.IntegerSpinnerValueFactory(5, 15, 12);
factory.setWrapAround(true);
spinner.setValueFactory(factory);
stage.setScene(new Scene(new BorderPane(spinner), 400, 200));
stage.setTitle("IntSpinnerTest");
stage.centerOnScreen();
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
这是一个已知错误:JDK-8193286 .提交者提供了一个解决方法——子类化 IntegerSpinnerValueFactory
:
package sample;
import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory;
public final class IntSpinnerValueFactory extends IntegerSpinnerValueFactory {
public IntSpinnerValueFactory(final int min, final int max) {
super(min, max);
}
public IntSpinnerValueFactory(final int min, final int max, final int initialValue) {
super(min, max, initialValue, 1);
}
@Override
public void increment(final int steps) {
final int min = getMin();
final int max = getMax();
final int currentValue = getValue();
final int newIndex = currentValue + steps * getAmountToStepBy();
setValue(newIndex <= max ? newIndex : (isWrapAround() ? (newIndex - min) % (max - min + 1) + min : max));
}
}
注意:解决方法已根据建议略作修改。
关于JavaFX Integer Spinner (IntegerSpinnerValueFactory) 不会将值环绕到最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53950933/