【发布时间】:2017-03-01 22:58:27
【问题描述】:
我在这里有一个滑块,它在滑块拇指上显示一个数字。每当我拖动滑块时,数字都会增加或减少。我希望拇指显示整数而不是双精度。
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class SliderWithLabeledThumb extends Application {
public void start(Stage ps) {
Slider s = new Slider();
StackPane root = new StackPane(s);
root.setPadding(new Insets(5));
s.setOrientation(Orientation.VERTICAL);
s.setMin(49);
s.setMax(99);
s.setValue(51);
s.setMinorTickCount(0);
s.setMajorTickUnit(1);
Scene scene = new Scene(root);
s.applyCss();
s.layout();
Pane p = (Pane) s.lookup(".thumb");
Label l = new Label();
l.textProperty().bind(s.valueProperty().asString("%.1f").concat(" °"));
p.getChildren().add(l);
ps.setScene(scene);
ps.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我将 asString() 的参数从“%.1f”更改为“%d”时,出现异常
l.textProperty().bind(s.valueProperty().asString("%d").concat(" °"));
错误信息:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at com.sun.javafx.binding.StringFormatter$4.computeValue(StringFormatter.java:196)
at javafx.beans.binding.StringBinding.get(StringBinding.java:152)
at com.sun.javafx.binding.StringFormatter.format(StringFormatter.java:207)
at javafx.beans.binding.Bindings.format(Bindings.java:4846)
at javafx.beans.binding.NumberExpressionBase.asString(NumberExpressionBase.java:319)
at embeddedwebview.SliderWithLabeledThumb.start(SliderWithLabeledThumb.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
这是我的 CSS 文件:
.slider .track {
-fx-pref-height: 10px;
-fx-pref-width: 20px;
}
.slider .thumb {
-fx-pref-height: 30;
-fx-prefer-width: 30;
}
目前的输出:
【问题讨论】:
-
可能与这种情况类似? stackoverflow.com/questions/38681664/javafx-slider-integer-only 还是只希望显示值是 int 而实际值是 double?
-
当你使用
%.0f时发生了什么? -
您的 css 中有一个小错字:
-fx-prefer-width: 30;应该是-fx-pref-width: 30; -
谢谢大家!你的提示真的很有帮助!
标签: css javafx binding integer slider