我有一个使用以下掩码格式化程序的JFormattedTextField:
private MaskFormatter maskForInput;
maskForInput=new MaskFormatter("####"); // I have tried using stars (*) too.
maskForInput.setValidCharacters("0123456789");
javax.swing.JFormattedTextField playerRaiseField=new javax.swing.JFormattedTextField(maskForInput);这应该允许用户输入最多4位数字作为输入。然而,当我试图Integer.parseInt(playerRaiseField.getText())从这个字段返回的String时,我总是得到一个NumberFormatException,这可能是由于用户输入中留下的空白。为了澄清这一点:
例如,如果用户输入560,就会留下一个拖尾空间,所以我正在读取的字符串是560( ),当试图将该String解析为int时,会抛出该异常。有什么解决办法吗?如何修改掩码格式化程序以接受1到4位数字,而不总是4位固定的??
注意:奇怪的是,在返回的String上使用trim()仍然没有删除额外的空格字符.
发布于 2013-01-22 03:17:38
你的期望值有很多问题.
首先,您似乎认为格式化字段可以有任意数量的字符。这不是真(虽然您可以获得文本,但字段仍然处于它认为无效的状态)。格式化字段要求必须填充掩码的所有位置。
其次,您应该使用JFormattedTextField#getValue方法返回字段的值,而不是getText。
第三,返回的文本中填充了掩码的占位符字符(MaskFormatter#getPlaceholder),该字符可能是空格,也可能不是空格,因此String#trim可能对.
如果您希望用户只能输入一个数字值,这个数值可能由0-n个字符组成,那么您确实应该考虑使用应用于普通DocumentFilter的JTextField。
public class TestField {
public static void main(String[] args) {
new TestField();
}
public TestField() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class NumericDocumentFilter extends DocumentFilter {
private int maxChars;
public NumericDocumentFilter(int maxChars) {
this.maxChars = maxChars;
}
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr)
throws BadLocationException {
StringBuilder buffer = new StringBuilder(text);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch)) {
buffer.deleteCharAt(i);
}
}
text = buffer.toString();
if (fb.getDocument().getLength() + text.length() > maxChars) {
int remaining = maxChars - fb.getDocument().getLength();
text = text.substring(0, remaining);
}
if (text.length() > 0) {
super.insertString(fb, offset, text, attr);
}
}
@Override
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) {
fb.remove(offset, length);
}
insertString(fb, offset, string, attr);
}
}
public class TestPane extends JPanel {
private JTextField field;
public TestPane() {
setLayout(new GridBagLayout());
field = new JTextField(4);
((AbstractDocument) field.getDocument()).setDocumentFilter(new NumericDocumentFilter(4));
add(field);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(field.getText());
}
});
}
}
}发布于 2013-01-22 01:51:17
由于我不确定您是如何使用trim的,所以我建议您尝试下面的代码行来删除空格:正如前面所讨论的,下面的代码应该工作:
String playerBet=playerRaiseField.getText();
// if playerBet is not null
String playerBetWithoutSpaces=playerBet.trim();
Integer.parseInt(playerBetWithoutSpaces);发布于 2013-01-22 02:09:31
java文档
1
public void setMask(String mask)
throws ParseException2你说你有4个空格。这意味着没有默认值集,也没有使用无效字符。查找上面的文档。
为了使它具有可伸缩性(1-4),您必须确定输入的长度,然后使用IFs通过适当的掩码运行它。您还可以使用默认值填充剩余的掩码,请参阅文档。
编辑:见您在下面张贴了更多代码。您不对现有字符串执行getText。您可以使用它从字段中获取输入。你必须用另一种方式来做。
https://stackoverflow.com/questions/14450230
复制相似问题