简体   繁体   English

如何检查文本是否仅包含 Java 中的 int 值?

[英]How to check if a text contains only int values in Java?

I want to determine if JTextField.getText() contains only numbers between 0-9, where anything else is not accepted such as letters and other characters.我想确定JTextField.getText()是否仅包含 0-9 之间的数字,其中不接受任何其他内容,例如字母和其他字符。 What's a good way to do this?有什么好方法可以做到这一点? thanks.谢谢。

If you are using NetBeans to build your GUI, you just need to put regular JTextFields in your GUI and in the creation code, you will specify the constructor of IntegerField.如果您使用 NetBeans 来构建您的 GUI,您只需要在您的 GUI 和创建代码中放置常规 JTextFields,您将指定 IntegerField 的构造函数。

OR或者

Use JFormattedTextField capabilities.使用 JFormattedTextField 功能。

numberField = new JFormattedTextField(NumberFormat.getInstance()); . .

Have a look at example( http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html ).看看例子( http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM