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

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

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

问题

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

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

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

  1. <details>
  2. <summary>英文:</summary>

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

  1. With this code i get a phone number from user. For example &quot;(0444) 111 - 12 - 34&quot;.
  2. 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.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. ```java
  7. String text = formattedTextField.getText().toString();
  8. Boolean flag1 = Character.isDigit(text.charAt(2));
  9. if(flag1){
  10. System.out.println("NUMBER");
  11. }else{
  12. System.out.println("NOT NUMBER");
  13. }

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

英文:
  1. String text = formattedTextField.getText().toString();
  2. Boolean flag1 = Character.isDigit(text.charAt(2));
  3. if(flag1){
  4. System.out.println(&quot;NUMBER&quot;);
  5. }else{
  6. System.out.println(&quot;NOT NUMBER&quot;);
  7. }

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:

确定