[英]NumberFormatter not behaving as expected
I didn't find any java question that helped me solve my problem, so here I come. 我没有找到任何可以帮助我解决问题的Java问题,所以我来了。
I'm currently trying to use a NumberFormatter with a JFormattedTextField to format a price in a GUI as the user types it in. 我目前正在尝试将NumberFormatter与JFormattedTextField结合使用,以在用户键入时在GUI中格式化价格。
But I'm getting strange results after typing 2 digits in the textfield. 但是在文本字段中输入2位数字后,我得到了奇怪的结果。
Here the code I use to test (Netbeans 8.0 + JDK 1.7.0_51): 这是我用来测试的代码(Netbeans 8.0 + JDK 1.7.0_51):
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(' ');
symbols.setCurrencySymbol("EUR");
DecimalFormat format = new DecimalFormat("¤ #,##0.00", symbols);
format.setMaximumFractionDigits(2);
format.setGroupingUsed(true);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum( 0.00);
formatter.setMaximum(9_999.99);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
JFormattedTextField field = new JFormattedTextField(formatter);
field.setColumns(10);
field.setValue(0.33);
frame.add(field);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
What I expect : 我期望的是:
// Step Text in TextField
// 1: GUI started EUR <caret>0.33
// 2: '1' pressed EUR 1<caret>.33
// 3: '2' pressed EUR 12<caret>.33
What I get : 我得到的是:
// Step Text in TextField
// 1: start GUI EUR <caret>0.33 [OK]
// 2: press '1' EUR 1<caret>.33 [OK]
// 3: press '2' EUR 1 2<caret>33.00 [NOK, see expected result above]
To me it looks like the Formatter does (for step 3) : 在我看来,它就像Formatter一样(对于第3步):
Is this the default behavior for the NumberFormatter? 这是NumberFormatter的默认行为吗? If yes, am I missing something in the setup of the formatter or do I need to write a custom one?
如果是,我是否在格式化程序的设置中缺少某些内容,还是需要编写一个自定义的? If not, what am I doing wrong?
如果没有,我在做什么错?
Regards, 问候,
Xan. an
seems like as I'm haven't am, described issue, to test the setting for InputVerifier as is described in API , 好像我还没有描述的那样 , 按照API所述测试InputVerifier的设置,
I'd suggest to use plain JTextField
with DocumentFilter
and the ISO name for currency to wrap by using NavigationFilter, eg excelent code example by camickr 我建议通过使用NavigationFilter将普通的
JTextField
与DocumentFilter
和ISO名称用于货币包装, 例如camickr的出色代码示例
. 。
import java.awt.*;
import java.math.*;
import java.text.*;
import javax.swing.*;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public DocumentListenerAdapter() {
JFrame frame = new JFrame("AbstractTextField Test");
final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(' ');
symbols.setCurrencySymbol("EUR");
DecimalFormat format = new DecimalFormat("¤ #,##0.00", symbols);
format.setMaximumFractionDigits(2);
format.setGroupingUsed(true);
//NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.0);
formatter.setMaximum(9000.00);
return formatter;
}
});
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMaximumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
final JFormattedTextField textField2 = new JFormattedTextField(numberFormat);
textField2.setValue(new Float(10.01));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DocumentListenerAdapter();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.