验证JTextField值,使其以“RA”开头。然后有8位数 [英] Validate JTextField value so that it starts with "RA" then has 8 digits
本文介绍了验证JTextField值,使其以“RA”开头。然后有8位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 JTextField
用户必须在哪里输入数据。它的值必须始终以 RA
开头,并且后面必须有8位数字。所以,它的长度总是10。例如, RA12345678
。
I have a JTextField
where in the user has to input the data. Its value has to always start with RA
and must have exactly 8 digits after it. So, its length will be 10 always. for e.g., RA12345678
.
我如何用Java做到这一点?
How do I do this in Java?
我尝试使用 MaskFormatter
和 JFormattedTextField
但是,没有达到效果。我需要一起验证输入的长度。
I tried using MaskFormatter
and JFormattedTextField
but, did not achieve the results. I need to validate the input with length together.
推荐答案
我使用 JSpinner
为此,只需在数字前加上 RA
。 EG
I'd use a JSpinner
for this, and simply prefix RA
to the number. E.G.
RA8007006
代码
Code
import java.awt.*;
import javax.swing.*;
class CaptureRA {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
String prefix = "RA";
JPanel gui = new JPanel(new FlowLayout(4));
gui.add(new JLabel(prefix));
SpinnerModel ints = new SpinnerNumberModel(
1000000,1000000,99999999,1);
JSpinner spinner = new JSpinner(ints);
gui.add(spinner);
JOptionPane.showMessageDialog(null, gui);
System.out.println(prefix + ints.getValue());
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
这篇关于验证JTextField值,使其以“RA”开头。然后有8位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文