我有两个双重属性price1和price2.我知道我可以将它绑定到这样的标签:
Locale locale = new Locale("en", "UK");
fxLabel.textProperty().bind(Bindings.format("price1/price2: %.3f/%.3f",.price1Property(),price2Property()));
Run Code Online (Sandbox Code Playgroud)
但显示的数字没有任何逗号分隔符(即显示123456.789而不是123,456.789).理想情况下,我希望做以下事情:
String pattern = "###,###.###;-###,###.###";
DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
df.applyPattern(pattern);
df.setMinimumFractionDigits(3);
df.setMaximumFractionDigits(10);
// bind df.format(value from price1 and price 2 property) to the label
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在房产上做这件事.我怎么解决这个问题?
使用JavaFX高级绑定API,您可以更改格式字符串并将语言环境传递给Binding.format:
Locale locale = new Locale("en", "UK");
fxLabel.textProperty().bind(Bindings.format(locale, "price1/price2: %,.3f/%,.3f", price1Property(), price2Property()));
Run Code Online (Sandbox Code Playgroud)
在此示例中,','标志用于格式字符串(java.util.Formatter API文档中记录的所有选项和可能性).
您还可以使用低级绑定API:
StringBinding stringBinding = new StringBinding() {
private final static Locale LOCALE = new Locale("en", "UK");
private final static DecimalFormat DF;
static {
String pattern = "###,###.###;-###,###.###";
DF = (DecimalFormat) NumberFormat.getNumberInstance(LOCALE);
DF.applyPattern(pattern);
DF.setMinimumFractionDigits(3);
DF.setMaximumFractionDigits(10);
}
public StringBinding() {
super.bind(price1Property(), price2Property());
}
@Override
protected String computeValue() {
return "price1/price2 " + DF.format(price1Property().get()) + "/" + DF.format(price2Property().get());
}
};
fxLabel.bind(stringBinding);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5167 次 |
| 最近记录: |