在我的应用程序中,我想将数字(金额)输入到特定限制,因此使用了JFormattedTextField。限制为“12345678.99”,即“。”之前的8位数。和“。”之后的2。等等。这是我的实现代码,但它没有按预期结果。
startBalTxt.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("########.##"))));
startBalTxt.setText(resourceMap.getString("startBalTxt.text")); // NOI18N
startBalTxt.setFont(Utility.getTextFont());
startBalTxt.setName("startBalTxt"); // NOI18N
INPUT RESULT
"12345678905.99" => "12345678906" ==> Should give "12345678.99" or "12345679.99"
"12345678.555" => "12345678.56" ==> CORRECT
"1234567890123456" => "1234567890123456" ==> Absolutely wrong in all aspects
我哪里错了?以及如何使这个工作正如我期待的那样。
StanislavL建议的更新:
numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMaximumIntegerDigits(8);
numberFormat.setMinimumFractionDigits(0);
numberFormat.setMinimumIntegerDigits(2);
nfr = new NumberFormatter(numberFormat);
initComponents();
myParent = parent;
this.startBalTxt.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(nfr));
Results -> 4562147896.45 == > 62,147,896.45
它遵守了真实的限制,但它消除了以前的数字而不是以后的数字。我的意思是在4562147896.45而不是“45”“96”不应该被淘汰。
答案 0 :(得分:6)
将DecimalFormat传递给JFormattedTextField
构造函数。它有以下方法
setMaximumIntegerDigits
setMinimumIntegerDigits
setMaximumFractionDigits
setMinimumFractionDigits
答案 1 :(得分:3)
即使JFormattedTextField
实施DecimalFormat
和NumberFormat
,最好使用DocumentListener
,
1)通过使用实现setMinimum()和setMaximum()
,使用Big Figures
或DecimalFormat
简单NumberFormat
来实现User-non-Acceptable
之间的跳跃效果不佳
2)JTextComponents
默认情况下实现插入文本,然后任何变通方法由User-non-Acceptable
实现JFormattedTextField
DecimalFormat
或NumberFormat
实现setMinimum( )和setMaximum()
3)它非常舒适的使用DocumentListener
允许添加任何数量,但突出显示范围
4)或JSpinner
与SpinnerNumberModel
一起使用,可以设置Formatter和Number Instance
InternationalFormatter
和DocumentListener
的示例
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public static void main(String args[]) {
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) {
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(1000.00);
return formatter;
}
});
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
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(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
private DocumentListenerAdapter() {
}
}