JFormattedTextField: 格式化文本到没有空格的数字
JFormattedTextField: the formatted text to numbers without spaces
我在使用 JFormattedTextField
时遇到了一个小问题:我想保存和检索从 1000 到 65535 的数字。但是当我从 JFormattedTextField
检索值 (7000) 时,它有一个 space 就像 7 000,当我将值解析为 Integer
(Integer.parseInt(formattedTextField.getText()))
时,它失败了。
java.lang.NumberFormatException: For input string: "7 000"
如果我用 MaskFormatter()
和 .setMask("#####")
这样做没问题,但我想用 NumberFormatter()
.
如何在没有附加 space 的情况下设置 JFormattedTextField
?
NumberFormatter nfsoc = new NumberFormatter();
nfsoc.setMaximum(Short.MAX_VALUE*2 - 1);
nfsoc.setMinimum(1);
nfsoc.setAllowsInvalid(false);
formattedTextField = new JFormattedTextField(nfsoc);
formattedTextField.setText("7000");
int socket = Integer.parseInt(formattedTextField.getText())
//java.lang.NumberFormatException: For input string: "7 000"
我预计Integer.parseInt(tfServerSocket.getText())
的输出是7000,但实际输出是//java.lang.NumberFormatException: For input string: "7 000"
我发现它不是 space 而是特殊字符 \p{Zs}
tfServerSocket.getText().replaceAll("\p{Zs}", "") = 7000 // 没有任何附加字符!
有两种解析整数的方法。
- NumberFormatter 使用本地化的 NumberFormat,这意味着它会根据您的语言环境进行格式化和解析 (country/region)。
- Integer.parseInt 不关心语言环境。它总是期望与 Java 源代码使用的格式相同的数字,即“[±]ddd…”(所有 ASCII 数字,可选地在前面加上一个符号)。
使用 JFormattedTextField 的 getValue() 方法。它专门用于执行您正在尝试执行的操作:获取 JFormattedTextField 的值。
它的另一个优点是它可以让您的代码在所有语言环境中工作,而不仅仅是您的语言环境。例如,在美国,您的示例值写为 7,000。在德国,它写成 7.000。
Number socketValue = (Number) formattedTextField.getValue();
if (socketValue == null) {
JOptionPane.showMessageDialog(
formattedTextField.getTopLevelAncestor(),
"A valid port valid is required.",
"Cannot create connection",
JOptionPane.ERROR_MESSAGE);
return;
}
int socket = socketValue.intValue();
去掉加法space:
NumberFormatter nfsoc = new NumberFormatter();
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false); // It removes any separator
nfsoc.setFormat(nf);
nfsoc.setMaximum(Short.MAX_VALUE*2 - 1);
nfsoc.setMinimum(1);
nfsoc.setAllowsInvalid(false);
我在使用 JFormattedTextField
时遇到了一个小问题:我想保存和检索从 1000 到 65535 的数字。但是当我从 JFormattedTextField
检索值 (7000) 时,它有一个 space 就像 7 000,当我将值解析为 Integer
(Integer.parseInt(formattedTextField.getText()))
时,它失败了。
java.lang.NumberFormatException: For input string: "7 000"
如果我用 MaskFormatter()
和 .setMask("#####")
这样做没问题,但我想用 NumberFormatter()
.
如何在没有附加 space 的情况下设置 JFormattedTextField
?
NumberFormatter nfsoc = new NumberFormatter();
nfsoc.setMaximum(Short.MAX_VALUE*2 - 1);
nfsoc.setMinimum(1);
nfsoc.setAllowsInvalid(false);
formattedTextField = new JFormattedTextField(nfsoc);
formattedTextField.setText("7000");
int socket = Integer.parseInt(formattedTextField.getText())
//java.lang.NumberFormatException: For input string: "7 000"
我预计Integer.parseInt(tfServerSocket.getText())
的输出是7000,但实际输出是//java.lang.NumberFormatException: For input string: "7 000"
我发现它不是 space 而是特殊字符 \p{Zs}
tfServerSocket.getText().replaceAll("\p{Zs}", "") = 7000 // 没有任何附加字符!
有两种解析整数的方法。
- NumberFormatter 使用本地化的 NumberFormat,这意味着它会根据您的语言环境进行格式化和解析 (country/region)。
- Integer.parseInt 不关心语言环境。它总是期望与 Java 源代码使用的格式相同的数字,即“[±]ddd…”(所有 ASCII 数字,可选地在前面加上一个符号)。
使用 JFormattedTextField 的 getValue() 方法。它专门用于执行您正在尝试执行的操作:获取 JFormattedTextField 的值。
它的另一个优点是它可以让您的代码在所有语言环境中工作,而不仅仅是您的语言环境。例如,在美国,您的示例值写为 7,000。在德国,它写成 7.000。
Number socketValue = (Number) formattedTextField.getValue();
if (socketValue == null) {
JOptionPane.showMessageDialog(
formattedTextField.getTopLevelAncestor(),
"A valid port valid is required.",
"Cannot create connection",
JOptionPane.ERROR_MESSAGE);
return;
}
int socket = socketValue.intValue();
去掉加法space:
NumberFormatter nfsoc = new NumberFormatter();
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false); // It removes any separator
nfsoc.setFormat(nf);
nfsoc.setMaximum(Short.MAX_VALUE*2 - 1);
nfsoc.setMinimum(1);
nfsoc.setAllowsInvalid(false);