我们可以通过在 JavaFX 中修改Slider的Minor和MajorTicks来创建预先创建的Minor 和 MajorTicks。但我想让用户只能选择我预先配置的值,比如. 我们有snapToTicks来解决一个问题,但是2, 4, 8, 16, 32
如何仅启用特定刻度线或禁用其他刻度线?
我可能只能从valueProperty中过滤出想要的值,但是是否有更智能的解决方案或本地方法?
由于 JavaFX 2.2 中的一个错误,无法格式化 SliderClass 中的标签,因此您需要使用 JavaFX 8。这些值仍然可能被计算出来,只是显示错误。
我为 JavaFX 2.2 添加了一个修改版本
功能滑块.java
公共类 FunctionalSlider 扩展 Slider { private ReadOnlyDoubleWrapper functionValue = new ReadOnlyDoubleWrapper();
public FunctionalSlider() {
this.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
functionValue.set(Math.pow(2, getValue()));
}
});
this.setLabelFormatter(new StringConverter<Double>() {
@Override
public Double fromString(String string) {
return 0.0;
}
@Override
public String toString(Double object) {
return String.format("%1$.0f", Math.pow(2, object));
}
});
}
public double getFunctionValue() {
return functionValue.get();
}
public ReadOnlyDoubleProperty functionValueProperty() {
return functionValue.getReadOnlyProperty();
}
}
功能滑块示例
public class FunctionalSliderSample extends Application {
private final ListView<String> startLog = new ListView<String>();
private final ListView<String> endLog = new ListView<String>();
@Override public void start(Stage stage) throws Exception {
Pane logsPane = createLogsPane();
Slider slider = createMonitoredSlider();
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(10));
layout.getChildren().setAll(
slider,
logsPane
);
VBox.setVgrow(logsPane, Priority.ALWAYS);
stage.setTitle("Slider Value Change Logger");
stage.setScene(new Scene(layout));
stage.show();
}
private Slider createMonitoredSlider() {
final FunctionalSlider slider = new FunctionalSlider();
slider.setMin(0);
slider.setValue(1);
slider.setMax(5);
slider.setMajorTickUnit(1);
slider.setMinorTickCount(0);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setSnapToTicks(true);
slider.setMinHeight(Slider.USE_PREF_SIZE);
slider.valueChangingProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
slider.setValue(Math.round(slider.getValue()));
String valueString = String.format("%1$.0f", slider.getFunctionValue());
if (slider.valueChangingProperty().get()) {
startLog.getItems().add(valueString);
}
else {
endLog.getItems().add(valueString);
}
}
});
return slider;
}
private HBox createLogsPane() {
HBox logs = new HBox(10);
logs.getChildren().addAll(
createLabeledLog("Start", startLog),
createLabeledLog("End", endLog)
);
return logs;
}
public Pane createLabeledLog(String logName, ListView<String> log) {
Label label = new Label(logName);
label.setLabelFor(log);
VBox logPane = new VBox(5);
logPane.getChildren().setAll(
label,
log
);
logPane.setAlignment(Pos.TOP_LEFT);
return logPane;
}
public static void main(String[] args) { launch(args); }
}