Pattern p = Pattern.compile("^[0-9]{0,3}$");
Matcher m = p.matcher(in);
if (m.find()) {
JOptionPane.showMessageDialog(null, " 4 integers please");
}
这是一个添加数字的按钮
尝试在对话框中创建限制数字数量的异常,它将检测数字是否在限制范围内,但不会停止程序。
您可以简单地将其in.matches("\\d{4}")
用作条件,并且仅当此条件为时才添加到文本区域true
。
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
String in = JOptionPane.showInputDialog("Number: ");
if (in.matches("\\d{4}")) {
// ...Code to add the value to the textarea
} else {
JOptionPane.showMessageDialog(null, "Only 4 digits please.");
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句