如何使用 JSpinner 格式化初始值?
我想更改 JSpinner 的默认小数格式,指定小数位数,但保留其他特定的格式设置。
但是,如果我按照我在 Sun 示例代码中发现的那样执行此操作
@Test public void fails() {
JSpinner spinner = new JSpinner();
NumberEditor editor = new JSpinner.NumberEditor(spinner);
editor.getFormat().setMinimumFractionDigits(3);
spinner.setEditor(editor);
assertEquals(0, spinner.getValue());
assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
// FAIL HERE with "0"
spinner.setValue(0.01);
assertEquals("0.010", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}
,则初始格式化值为“0”,因为尚未检测到格式的更改。
我可以明确设置格式图片
@Test public void delMe() {
JSpinner spinner = new JSpinner();
spinner.setEditor(new JSpinner.NumberEditor(spinner, "0.000"));
assertEquals(0, spinner.getValue());
assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}
,但这不尊重逗号等的区域设置。
I want to change the default decimal format for a JSpinner, specifying a number of decimal places but leaving other formatting locale specific.
But if I do this
@Test public void fails() {
JSpinner spinner = new JSpinner();
NumberEditor editor = new JSpinner.NumberEditor(spinner);
editor.getFormat().setMinimumFractionDigits(3);
spinner.setEditor(editor);
assertEquals(0, spinner.getValue());
assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
// FAIL HERE with "0"
spinner.setValue(0.01);
assertEquals("0.010", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}
as I think I found in Sun example code, the initial formatted value is "0", as the changes to the format have not been detected.
I can set the format picture explicitly
@Test public void delMe() {
JSpinner spinner = new JSpinner();
spinner.setEditor(new JSpinner.NumberEditor(spinner, "0.000"));
assertEquals(0, spinner.getValue());
assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}
but this does not respect locale settings with regards to commas etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论