我这里有一个滑块,在滑块上显示一个数字。每当我拖动滑块时,数字会递增或递减。我希望拇指显示整数而不是双精度。
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);
}
}
当我从"%。1f"更改asString()的参数时到了"%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;
}
到目前为止的当前输出:
答案 0 :(得分:2)
这个问题最终几乎与以下内容重复:
因此,您无法使用%d
格式化双精度值,您必须使用%f
。如果您只想显示double值的整数部分,可以让%.0f
(如Hypnic Jerk的注释中所推荐的那样)舍入到零小数位(不会显示小数点)。根据{{3}},使用formatting javadoc。
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("%.0f").concat(" °"));
p.getChildren().add(l);
ps.setScene(scene);
ps.show();
}
public static void main(String[] args) {
launch(args);
}
}