如何检查 JFormattedTextField 的值是否符合我所需的要求?

huangapple go评论57阅读模式
英文:

How can i check the value of JFormattedTextField is what i need?

问题

MaskFormatter mf = new MaskFormatter("(0###) ### - ## - ##"); // 格式
JFormattedTextField formattedTextField = new JFormattedTextField(mf);

使用这段代码,我可以从用户那里获取一个电话号码。例如:"(0444) 111 - 12 - 34"。

如果用户没有输入任何内容,我会得到一个字符串,类似于:"(0 ) - - "。我该如何检查它是否为空,以及用户是否输入了所有的数字。


<details>
<summary>英文:</summary>

MaskFormatter mf = new MaskFormatter("(0###) ### - ## - ##"); // format
JFormattedTextField formattedTextField = new JFormattedTextField(mf);

With this code i get a phone number from user. For example &quot;(0444) 111 - 12 - 34&quot;.

If user write nothing i get a string like this &quot;(0   )     -    -   &quot;.How can i check it if it is empty or not. And did the user write all numbers.

</details>


# 答案1
**得分**: 0

```java
String text = formattedTextField.getText().toString();
Boolean flag1 = Character.isDigit(text.charAt(2));

if(flag1){
   System.out.println("NUMBER");
}else{
   System.out.println("NOT NUMBER");
}

我可以使用这段代码进行检查。

英文:
String text = formattedTextField.getText().toString();
Boolean flag1 = Character.isDigit(text.charAt(2));

if(flag1){
   System.out.println(&quot;NUMBER&quot;);
}else{
   System.out.println(&quot;NOT NUMBER&quot;);
}

I can check with this code.

huangapple
  • 本文由 发表于 2020年8月21日 02:36:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定