将JFormattedTextField中的多个值分配给多个变量

时间:2012-10-14 15:45:49

标签: java swing textfield jformattedtextfield

如果我有这样的JFormattedTextField

MaskFormatter formatter = new MaskFormatter("#,#");
JFormattedTextField textField = new JFormattedTextField(formatter);

如果我有变量

int x = 0;
int y = 0;

如何将文本字段中的第一个数字存储到x,将第二个数字存储到y

2 个答案:

答案 0 :(得分:1)

掩码不会改变内部值的存储方式,它只是告诉如何表示/输入它。

所以你仍然有.getText()以你选的格式返回一个字符串。根据您的需要处理字符串(split()StringTokenizer)。

答案 1 :(得分:1)

假设第一个&第二个数字是,中逗号JFormattedTextField的任意一侧,您可以这样做:

String[] numbers = textField.getText().split(",");
int x = Integer.parseInt(numbers[0]);
int y = Integer.parseInt(numbers[1]);