JTextField的最大长度,只接受数字? [英] Maximum length of JTextField and only accepts numbers?
本文介绍了JTextField的最大长度,只接受数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望用户输入最多8个号码,因为它是移动号码的字段。
这是我的JTextField。
txtMobile = new JTextField
txtMobile.setColumns(10);
txtMobile.setBounds(235,345,145,25);
add(txtMobile);
当我们处理它时,如何检查无效字符,例如 >>在
JTextField
?
$ b <$ p <$ c> <$ c> $ c> 1)最大长度
2)只接受数字
3)检查无效字符
4)检查它是否是有效的电子邮件地址
请帮助:D
解决方案
看看这个示例,它只接受数字作为输入,作为第二个要求的答案。
code> public class InputInteger
{
private JTextField tField;
private JLabel label = new JLabel();
private MyDocumentFilter documentFilter;
private void displayGUI()
{
JFrame frame = new JFrame(Input Integer Example);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5,5,5,5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument())。setDocumentFilter(
new MyDocumentFilter());
contentPane.add(tField);
contentPane.add(label);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String [] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new InputInteger()。displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
类MyDocumentFilter扩展DocumentFilter {
private static final long serialVersionUID = 1L;
@Override
public void insertString(FilterBypass fb,int off
,String str,AttributeSet attr)
throws BadLocationException
{
// remove non-数字
fb.insertString(off,str.replaceAll(\\D ++,),attr);
}
@Override
public void replace(FilterBypass fb,int off
,int len,String str,AttributeSet attr)
throws BadLocationException
{
//删除非数字
fb.replace(off,len,str.replaceAll(\\D ++,),attr);
}
}
I want the user to input a maximum of 8 numbers as it is a field for Mobile number. This is my JTextField.
txtMobile = new JTextField();
txtMobile.setColumns(10);
txtMobile.setBounds(235, 345, 145, 25);
add(txtMobile);
While we're at it, how do I check for invalid characters like >> '^%$*
in a JTextField
?
1)Maximum Length
2)Accepts only numbers
3)Check for invalid characters
4)Check if it's a valid email address
Please help :D
解决方案
Look at this sample example it will only accepts numbers as an input, as an answer to your second requirement.
public class InputInteger
{
private JTextField tField;
private JLabel label=new JLabel();
private MyDocumentFilter documentFilter;
private void displayGUI()
{
JFrame frame = new JFrame("Input Integer Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter());
contentPane.add(tField);
contentPane.add(label);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new InputInteger().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class MyDocumentFilter extends DocumentFilter{
private static final long serialVersionUID = 1L;
@Override
public void insertString(FilterBypass fb, int off
, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.insertString(off, str.replaceAll("\\D++", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off
, int len, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.replace(off, len, str.replaceAll("\\D++", ""), attr);
}
}
这篇关于JTextField的最大长度,只接受数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文